Build a PDF Generator Android App in Kotlin or Java From HTML Template Full Project For Beginners

  • Post author:
  • Post category:Java
  • Post comments:0 Comments

PDF-Generator

Download Download Download Download

PDF Generator library, easy way to create PDF from String Content or Any HTML Content.

Get Started

dependencies {
     implementation 'com.uttampanchasara.pdfgenerator:pdfgenerator:1.3'
}

Quick Start

In order to start using PdfGenerator, Just copy below code to your project and just pass the required values and that’s all you done!

Kotlin Code:

 CreatePdf(this)
            .setPdfName("FirstPdf")
            .openPrintDialog(false)
            .setContentBaseUrl(null)
            .setPageSize(PrintAttributes.MediaSize.ISO_A4)
            .setContent("Your Content")
            .setFilePath(Environment.getExternalStorageDirectory().absolutePath + "/MyPdf")
            .setCallbackListener(object : CreatePdf.PdfCallbackListener {
                override fun onFailure(errorMsg: String) {
                    Toast.makeText(this@MainActivity, errorMsg, Toast.LENGTH_SHORT).show()
                }

                override fun onSuccess(filePath: String) {
                    Toast.makeText(this@MainActivity, "Pdf Saved at: $filePath", Toast.LENGTH_SHORT).show()
                }
            })
            .create()

Java Code:

new CreatePdf(this)
            .setPdfName("FirstPdf")
            .openPrintDialog(false)
            .setContentBaseUrl(null)
            .setPageSize(PrintAttributes.MediaSize.ISO_A4)
            .setContent("Your Content")
            .setFilePath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyPdf")
            .setCallbackListener(new CreatePdf.PdfCallbackListener() {
                @Override
                public void onFailure(@NotNull String s) {
                    // handle error
                }

                @Override
                public void onSuccess(@NotNull String s) {
                    // do your stuff here
                }
            })
            .create();

NOTE: ( Provide STORAGE Permission if you are providing filePath to library )

In library, I’m not handling any storage permission related exception, If you are providing your custom filePath then your application must have STORAGE READ-WRITE Permission in order to store Pdf in provided path.

Usage

  • setPdfName : String

Provide Your Pdf name, Library will use to save pdf with this name.

  • openPrintDialog : Boolean

Default is false, If you set true it will send your pdf for print and open the android default pdf print view.

  • setContentBaseUrl : String

If you are loading content from assets folder in that case you can pass your base url here, same as we passed in webview.

  • setPageSize : PrintAttributes.MediaSize

To set custom page size for your pdf, you will have to pass the mediaSize as argument.

See also  Java Swing/AWT GUI Program to Validate Password & Check Strength Desktop App Full Project For Beginners

Example – For A4 size: PrintAttributes.MediaSize.ISO_A4 | ISO_A0 | ISO_A1

  • setContent : String

Provide your String content, which you want to generate Pdf.

  • setFilePath : String

Provide custom file path to save pdf in your own directory, default will be the cache directory of Application

  • setCallbackListener : Listener Interface

Set this callback listener to get callback on pdf generated.

Benefits

  • Easily Generate Pdf
  • No Extra codes
  • Time saving
  • Lightweight

 

 

DOWNLOAD FULL SOURCE CODE

 

Leave a Reply