Reputation: 149
how can i save an activity as a pdf file to the phone storage with all its data?
I did an cv creator app and i need the final activity to be downloaded to the phone by the user as pdf. I am able to get the layout in pdf format but i lost all added data to the activity. any help please? Here is the code for the data transfered to the activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_template1);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
cvPhoto = findViewById(R.id.photo);
cvName = findViewById(R.id.user_name);
cvName2 = findViewById(R.id.user_name2);
cvBirthDate = findViewById(R.id.user_date);
cvAddress = findViewById(R.id.user_address);
cvNationality = findViewById(R.id.user_nationality);
cvPhone = findViewById(R.id.user_contact);
cvEmail = findViewById(R.id.user_email);
cvSchool = findViewById(R.id.user_school);
cvSpecialty = findViewById(R.id.user_specialty);
cvCertificate = findViewById(R.id.user_certificate);
cvYear = findViewById(R.id.user_years);
cvCompany= findViewById(R.id.user_company);
cvPost = findViewById(R.id.user_post);
cvDuration= findViewById(R.id.user_duration);
cvReference= findViewById(R.id.user_reference);
cvLanguage = findViewById(R.id.user_language);
cvOthers= findViewById(R.id.user_skill);
cvDate = findViewById(R.id.current_date);
Date date = new Date();
String modifiedDate = new SimpleDateFormat(" dd MMM, yyyy", Locale.getDefault()).format(new Date());
cvDate.setText(getString(R.string.done_on) + " " + modifiedDate);
Intent intent = getIntent();
Bundle bundle = getIntent().getExtras();
final String tname =bundle.getString("user_name");
final String tbirtDate =bundle.getString("user_date_of_birth");
final String taddress =bundle.getString("user_address");
final String tnationality =bundle.getString("user_nationality");
final String temail =bundle.getString("user_email");
final String tphone =bundle.getString("user_phone");
final String tschool =bundle.getString("user_school");
final String tspecialty =bundle.getString("user_specialty");
final String tcertificate =bundle.getString("user_certificate");
final String tyears =bundle.getString("user_years");
final String tcompany =bundle.getString("user_company");
final String tpost =bundle.getString("user_post");
final String tduration =bundle.getString("user_duration");
final String treference =bundle.getString("user_reference");
final String tlanguages =bundle.getString("user_languages");
final String totherSkills =bundle.getString("user_other_skills");
Bitmap resized = intent.getParcelableExtra("BitmapImage");
cvPhoto.setImageBitmap(resized);
cvName.setText(getString(R.string.full_name) +" "+ tname);
cvBirthDate.setText(getString(R.string.date_of_birth) +" "+ tbirtDate);
cvAddress.setText(getString(R.string.address) +" "+ taddress);
cvNationality.setText(getString(R.string.nationality) +" "+ tnationality);
cvEmail.setText(getString(R.string.email) +" "+ temail);
cvPhone.setText(getString(R.string.phone_number) +" "+ tphone);
cvSchool.setText(getString(R.string.school_attended) +"\n "+ tschool);
cvSpecialty.setText(getString(R.string.specialty_course) +"\n "+ tspecialty);
cvCertificate.setText(getString(R.string.certificates_obtained) +"\n "+ tcertificate);
cvYear.setText(getString(R.string.years) +"\n "+ tyears);
cvCompany.setText(getString(R.string.company_organisation_institution) +"\n "+ tcompany);
cvPost.setText(getString(R.string.phone_number) +"\n "+ tpost);
cvDuration.setText(getString(R.string.duration) +"\n "+ tduration);
cvReference.setText(getString(R.string.reference) +"\n "+ treference);
cvLanguage.setText(tlanguages);
cvOthers.setText(totherSkills);
cvName2.setText(tname);
And here is the code to create the pdf document with the activity:
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(2250, 1400, 1).create();
// start a page
PdfDocument.Page page = document.startPage(pageInfo);
// add something on the page
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View content = inflater.inflate(R.layout.activity_template1, null);
content.measure(1400, 2250);
content.layout(0,0, 2250, 1400);
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
// add more pages
// write the document content
String directory_path = Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
File file = new File(directory_path);
if (!file.exists()) {
file.mkdirs();
}
String targetPdf = directory_path+"My CV.pdf";
File filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
Toast.makeText(Template1Activity.this, "Done", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("main", "error "+e.toString());
Toast.makeText(Template1Activity.this, "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
}
// close the document
document.close();
The end result only gives me the activity without the data.
Upvotes: 0
Views: 1594
Reputation: 6277
Since you've not specified how you want to generate your pdf, you can look into PdfDocument class that comes with the android sdk.
Make sure you have android.permission.WRITE_EXTERNAL_STORAGE
permission
Pass the parent view to createPdfFromView
private void createPdfFromView(View view, String fileName, int pageWidth, int pageHeight, int pageNumber) {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File file = new File(path, fileName.concat(".pdf"));
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if (file.exists()) {
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(pageWidth, pageHeight, pageNumber).create();
PdfDocument.Page page = document.startPage(pageInfo);
view.draw(page.getCanvas());
document.finishPage(page);
try {
Toast.makeText(this, "Saving...", Toast.LENGTH_SHORT).show();
document.writeTo(fOut);
} catch (IOException e) {
Toast.makeText(this, "Failed...", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
document.close();
/*Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);*/
} else {
//..
}
}
Upvotes: 1
Reputation: 76669
Better use eg. Apache PDFbox and render the CV in DIN A4 or US Letter format.
Else that would be a PDF with a screenshot inside, which is not machine-readable.
PDF/A-3 might also be an option, if only there was a standardized CV XML format.
Upvotes: 1