Blog Post

Ways to Create a PDF on Android

Illustration: Ways to Create a PDF on Android

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

In this blog post, we’ll first look at how users can create PDFs in other apps. Then we’ll see how your app can use the PdfProcessor class from PSPDFKit to generate blank PDFs and customize their content.

Export 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

Use 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, just select Save as PDF as the target printer.

Screenshots of webpage, share dialog, and print preview Screenshots of generated PDF

Having taken a look at the options a user has to create PDFs, let’s look at some ways you can programmatically create PDFs in your apps.

Create a Blank PDF

With PSPDFKit, 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 PDF Viewer 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))

val pdfProcessorTask = PdfProcessorTask(newPage)

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

To let the user choose the page size and appearance using the same user interface as PDF Viewer, use NewPageDialog:,null, object : NewPageDialog.Callback {
    override fun onDialogConfirmed(newPage: NewPage) {
        // Use PdfProcessor like above.

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

Create 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 right across the document:

val targetFile: File

val pageSize = Size(595f, 842f)

val newPage = NewPage.emptyPage(pageSize)

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) {
    // Could not 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).


In this post, we’ve seen both ways users can create PDFs in existing apps and how you can add PDF creation as a feature in your own app.

Share Post
Free 60-Day Trial Try PSPDFKit in your app today.
Free Trial

Related Articles

Explore more
RELEASES  |  Android • Products

PSPDFKit 8 for Android Adds Electronic Signatures and Jetpack Compose

DEVELOPMENT  |  Android • Development • Jetpack Compose • UI

Introducing Jetpack Compose Support

DEVELOPMENT  |  Android • Development • Git

Refactoring Git Branches with Interactive Rebase and Autosquash in Android Studio