/ PDF

XML to PDF Generator For Android

XML to PDF Generator For Android

Android-XML-to-PDF-Generator

Automatically generate PDF file from XML file or Java's View object in Android.

Make PDF from Android layout resources (e.g - R.layout.myLayout,R.id.viewID), Java's view ids or directly views objects

  • Simple: Extremely simple to use. For using Step Builder Design Patten undernath,here IDE greatly helps developers to complete the steps for creating a PDF from XMLs.
  • Powerful: Customize almost everything.
  • Transparent: It shows logs,success-responses, failure-responses , that's why developer will nofity any event inside the process.

➤ Installation

Step 1. Add the JitPack repository to your root build.gradle at the end of repositories

allprojects {
    repositories {
        // ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
        implementation 'com.github.Gkemon:XML-to-PDF-generator:1.0'
}

-----------------------------------------------------

➤ Getting Started

You can generate PDF from many sources.

  • Layout resources (i.e: R.layout.myLayout)
  • View ids (i.e: R.id.viewID)
  • Java view objects (i.e View,TextView,LinearLayout)

From layout resources :

 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)*/
				
                            }
                        });

From view IDs :

    PdfGenerator.getBuilder()
                        .setContext(context)
                        .fromViewIDSource()
                        .fromViewID(activity,R.id.tv_print_area,R.id.tv_print_area)
			/* "fromViewID()" takes array of view ids those MUST BE and MUST BE contained in the inserted "activity" .
			 * You can also invoke "fromViewIDList()" method here which takes list of view ids instead of array. */
                        .setCustomPageSize(3000,3000)
			/* Here I used ".setCustomPageSize(3000,3000)" to set custom page size.*/
                        .setFileName("Test-PDF")
                        .setFolderName("Test-PDF-folder")
                        .openPDFafterGeneration(true)
                        .build(new PdfGeneratorListener() {
                            @Override
                            public void onFailure(FailureResponse failureResponse) {
                                super.onFailure(failureResponse);
                            }

                            @Override
                            public void showLog(String log) {
                                super.showLog(log);
                            }

                            @Override
                            public void onSuccess(SuccessResponse response) {
                                super.onSuccess(response);
                            }
                        });

From views:


PdfGenerator.getBuilder()
                        .setContext(MainActivity.this)
                        .fromViewSource()
                        .fromView(view) 
			/* "fromView()" takes array of view. You can also invoke "fromViewList()" method here
			 * which takes list of view instead of array. */
                        .setCustomPageSize(3000,3000)
                        .setFileName("Test-PDF")
                        .setFolderName("Test-PDF-folder")
                        .openPDFafterGeneration(true)
                        .build(new PdfGeneratorListener() {
                            @Override
                            public void onFailure(FailureResponse failureResponse) {
                                super.onFailure(failureResponse);
                            }

                            @Override
                            public void showLog(String log) {
                                super.showLog(log);
                            }

                            @Override
                            public void onSuccess(SuccessResponse response) {
                                super.onSuccess(response);
                            }
                        });

GitHub

Comments