Build a PDF Generator Android App in Kotlin or Java From HTML Template

You are currently viewing Build a PDF Generator Android App in Kotlin or Java From HTML Template

Here, You are going to know the concept of “Build a PDF Generator Android App in Kotlin or Java From HTML Template”. Let’s go for an article to understand the topic below.

Build a PDF Generator Android App in Kotlin or Java From HTML Template

PDF-Generator

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(Android) Circular Progress Bar in App [New]

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

Conclusion

I hope this article helps you to Build a PDF Generator Android App in Kotlin or Java From HTML Template. If you face any issues please let me know via the comment section. Share this article with other Java program developers via social networks. Thank You!

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply