将android布局转换为PDF文件

时间:2017-07-19 10:29:11

标签: android android-layout pdf

如何将android布局转换为PDF文件。可能吗?。
如果可能的话,如何继续将android布局转换为PDF。
欢迎提出建议。提前谢谢。

4 个答案:

答案 0 :(得分:3)

我尝试了很多方法。
终于得到了答案使用此库https://mvnrepository.com/artifact/com.itextpdf/itextpdf/5.0.6
      图像布局并将其置于pdf


org.ajoberstar.grgit.auth.hardcoded.allow

答案 1 :(得分:2)

您可以使用自定义库,例如https://github.com/HendrixString/Android-PdfMyXml 但是还有另一种解释方式 -  How to convert Android View to PDF - 生成包含布局位图的pdf

答案 2 :(得分:1)

上面的答案是正确的,它会在下一行引发异常错误。

 bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

这行代码的Bcoz返回null

  Bitmap bm = relativeLayout.getDrawingCache();

所以我做了一些关于Bitmap的研究。我使用这个方法首先将视图转换为Image。然后你可以使用上面的函数,即imageToPDF(),它运作良好。下面是我的方法。

 public void convertCertViewToImage()
 {

        scrollView.setDrawingCacheEnabled(true);
        scrollView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        scrollView.layout(0, 0, scrollView.getMeasuredWidth(), scrollView.getMeasuredHeight());
        scrollView.buildDrawingCache();
        Bitmap bm = Bitmap.createBitmap(scrollView.getDrawingCache());
        scrollView.setDrawingCacheEnabled(false); // clear drawing cache
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/jpg");

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

        File f = new File(getExternalFilesDir(null).getAbsolutePath() + File.separator + "Certificate" + File.separator + "myCertificate.jpg");

        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());

} 

答案 3 :(得分:0)

我做了library以实现这一目标。

主要代码段是-

 PdfGenerator.getBuilder()
                        .setContext(context)
                        .fromLayoutXMLSource()
                        .fromLayoutXML(R.layout.layout_print,R.layout.layout_print)
            /* "fromLayoutXML()" takes array of layout resources.
             * You can also invoke "fromLayoutXMLList()" method here which takes list of layout resources instead of array. */
                        .setDefaultPageSize(PdfGenerator.PageSize.A4)
            /* It takes default page size like A4,A5. You can also set custom page size in pixel
             * by calling ".setCustomPageSize(int widthInPX, int heightInPX)" here. */
                        .setFileName("Test-PDF")
            /* It is file name */
                        .setFolderName("FolderA/FolderB/FolderC")
            /* It is folder name. If you set the folder name like this pattern (FolderA/FolderB/FolderC), then
             * FolderA creates first.Then FolderB inside FolderB and also FolderC inside the FolderB and finally
             * the pdf file named "Test-PDF.pdf" will be store inside the FolderB. */
                        .openPDFafterGeneration(true)
            /* It true then the generated pdf will be shown after generated. */
                        .build(new PdfGeneratorListener() {
                            @Override
                            public void onFailure(FailureResponse failureResponse) {
                                super.onFailure(failureResponse);
                /* If pdf is not generated by an error then you will findout the reason behind it
                 * from this FailureResponse. */
                            }

                            @Override
                            public void showLog(String log) {
                                super.showLog(log);
                /*It shows logs of events inside the pdf generation process*/ 
                            }

                            @Override
                            public void onSuccess(SuccessResponse response) {
                                super.onSuccess(response);
                /* If PDF is generated successfully then you will find SuccessResponse 
                 * which holds the PdfDocument,File and path (where generated pdf is stored)*/
                
                            }
                        });