如何将活动另存为带有数据的PDF文件

时间:2019-09-25 03:50:15

标签: java android

我如何将一项活动及其所有数据另存为pdf文件?

我做了一个简历创建者应用,我需要最终的活动由用户以pdf格式下载到手机中。我能够以pdf格式获取布局,但是我丢失了所有添加到活动中的数据。有什么帮助吗? 这是传输到活动的数据的代码:

 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);

下面是使用活动创建pdf文档的代码:

   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();

最终结果只给了我没有数据的活动。

2 个答案:

答案 0 :(得分:1)

使用更好。 Apache PDFbox并以DIN A4或US Letter格式呈现CV。

否则将是带有屏幕截图的PDF,这是机器无法读取的。

如果只有标准化的CV XML格式,

PDF / A-3也可能是一个选项。

答案 1 :(得分:1)

由于您尚未指定生成pdf的方式,因此可以查看android sdk随附的PdfDocument类。

确保您拥有android.permission.WRITE_EXTERNAL_STORAGE权限

将父视图传递到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 {
            //..
        }

    }