Blog Post

Best PDF Viewer for Android: How to Create a PDF on Android

Hulya Masharipov
Illustration: Best PDF Viewer for Android: How to Create a PDF on Android
Information

This article was first published in June 2018 and was updated in August 2024.

You can use our Android PDF SDK for viewing and editing already existing PDF documents, but have you wondered how to create a PDF on Android?

If you’re looking for the best PDF viewer for Android or ways to create a PDF on Android, this post will walk you through several options — from using native tools, to leveraging powerful libraries like PSPDFKit.

Exporting a PDF from an App That Supports It

Many apps allow you to export a PDF. For example, both Google Docs and Microsoft Word can do this.

Screenshots of PDF export flow in Google Docs

Using the Print Feature

The Android print feature allows you to save any printable content as a PDF.

To create a PDF from the print preview, select Save as PDF as the target printer.

Screenshots of webpage, share dialog, and print preview

The image below shows what this will look like.

Screenshots of generated PDF

Now that we’ve looked at the options a user has for creating PDFs, let’s look at some ways you can programmatically create PDFs in your apps.

Creating a Blank PDF with PSPDFKit’s Android PDF Viewer Library

With our Android PDF Generation library, it’s easy to create a PDF with empty pages. The page can have a patterned background or even an image background. An example of this can be found in our free PDF Viewer app by tapping the + button in the document browser.

Screenshot of the new document screen in PDF Viewer

Here’s how to create a PDF while programmatically specifying the page size and appearance:

val targetFile: File

val newPage = NewPage.emptyPage(Size(595f, 842f))
    .backgroundColor(Color.LTGRAY)
    .build()

val pdfProcessorTask = PdfProcessorTask(newPage)

try {
    val processor = PdfProcessor.processDocument(pdfProcessorTask, targetFile)
} catch (ex: Exception) {
    // Couldn't create PDF file.
}

To let the user choose the page size and appearance using the same user interface as PDF Viewer, use NewPageDialog:

NewPageDialog.show(supportFragmentManager,null, object : NewPageDialog.Callback {
    override fun onDialogConfirmed(newPage: NewPage) {
        // Use `PdfProcessor` like above.
    }

    override fun onDialogCancelled() {
        // User didn't select a template.
    }
})

Creating a PDF by Drawing onto a Canvas

For maximum flexibility, you can also write your own drawing code to generate PDFs. When doing this, you need to take care to correctly handle line and page breaks.

Here’s a simple example that draws a line directly across the document:

val targetFile: File

val pageSize = Size(595f, 842f)

val newPage = NewPage.emptyPage(pageSize)
    .backgroundColor(Color.LTGRAY)
    .build()

val pdfProcessorTask = PdfProcessorTask(newPage)
pdfProcessorTask.addCanvasDrawingToPage(PageCanvas(pageSize) { canvas ->
    val paint = Paint()
    // Will draw a line from one corner of the page to the other.
    canvas.drawLine(0f, 0f, pageSize.width, pageSize.height, paint)
}, 0)

try {
    val processor = PdfProcessor.processDocument(pdfProcessorTask, targetFile)
} catch (ex: Exception) {
    // Couldn't create PDF file.
}

Screenshot of the resulting PDF

If you already have some code that draws onto a canvas, you can reuse that drawing code (with some noted exceptions).

Conclusion

In this post, you saw how users can create PDFs in existing apps by using the export and print features. You also learned how you can add PDF creation as a feature in your own app.

If your app requires advanced PDF functionality, such as adding digital signatures, filling PDF forms, and working with PDF annotations, you should look at using a commercial library.

Our Android PDF library comes with more than 30 out-of-the-box features and has well-documented APIs to handle complex use cases. Try our PDF library for free, and check out our demos to see what’s possible.

FAQ

Here are a few frequently asked questions about creating PDFs on Android.

How can I create a PDF in my Android app?

Use the Android print framework for basic PDFs or PSPDFKit for advanced features like annotations and digital signatures.

What is the minimum Android version supported by PSPDFKit?

PSPDFKit supports Android 5.0 (API level 21) and above.

Can I add digital signatures to PDFs on Android?

Yes, PSPDFKit allows adding digital signatures to PDFs easily.

Are there free alternatives to PSPDFKit for Android?

Yes, libraries like iText and PdfBox are free, but they may lack advanced features.

How do I display a PDF using PSPDFKit?

Use PdfActivity to open and display a PDF directly within your app.

Author
Hulya Masharipov Technical Writer

Hulya is a frontend web developer and technical writer at Nutrient who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

Explore related topics

Share post
Free trial Ready to get started?
Free trial