How to convert Android View to PDF

前端 未结 4 748
日久生厌
日久生厌 2020-12-10 12:21

I have created an Android Invoice app. The generated invoice is standard Android layout with nested views. I am looking for a library that I can use to convert this view to

4条回答
  •  庸人自扰
    2020-12-10 12:46

    I made a library to achieve this objective (Getting PDF from Java View objects).

    The main code snippet is -

     PdfGenerator.getBuilder()
                            .setContext(context)
                            .fromViewSource()
                            .fromView(targetView) /* "targetView" is the view ,you want to convert PDF */
                /* "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)*/
                    
                                }
                            });
    

提交回复
热议问题