Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert android layout to PDF file

How to convert android layout to PDF file. Is it possible?.
If that is possible how to proceed to convert the android layout to PDF.
suggestions are welcome. Thanks in advance.

like image 984
Thirumal Govindaraj Avatar asked Jul 19 '17 10:07

Thirumal Govindaraj


People also ask

Can you create a PDF on an Android?

Create a PDF on iOS and Android Android and iOS include similar options to create PDF files. In Android, open the Share menu, then use the Print option. Choose Save as PDF as your printer. In iOS, tap the Share button in an app, then tap the Options panel at the top.


2 Answers

I made a library to achieve this objective.

The main code snippet is -

 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)*/
                
                            }
                        });
like image 54
Gk Mohammad Emon Avatar answered Oct 21 '22 07:10

Gk Mohammad Emon


The above answer is correct, it throws an Exception error at the following line.

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

Bcoz of this line of code return null

  Bitmap bm = relativeLayout.getDrawingCache();

So I have done some more research on Bitmap coming null.I use this method which first converts view to Image. Then you can use above function i.e imageToPDF() which works well.Below is my method.

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

} 
like image 38
Baiju Sharma Avatar answered Oct 21 '22 08:10

Baiju Sharma