Android
PDF rendering
Android development
mobile app development
Android Studio

How to render a PDF file in Android

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Android's built-in way to render a PDF is PdfRenderer, which is available from API 21 onward. It does not give you a complete document viewer UI, but it does let you open a PDF, render pages to bitmaps, and display those bitmaps in your own views.

When PdfRenderer Is the Right Choice

PdfRenderer is a good fit when:

  • you control the PDF file locally
  • you only need basic rendering
  • you are willing to build page navigation and zoom behavior yourself

It is not a full-featured PDF reader. If you need annotations, search, high-level gestures, or a polished viewer out of the box, a third-party library may be more appropriate.

Basic PdfRenderer Flow

The flow is:

  1. obtain a ParcelFileDescriptor for the PDF file
  2. create a PdfRenderer
  3. open a page
  4. render the page into a Bitmap
  5. show the bitmap in an ImageView

Here is a minimal Kotlin example:

kotlin
1import android.graphics.Bitmap
2import android.graphics.pdf.PdfRenderer
3import android.os.ParcelFileDescriptor
4import java.io.File
5
6fun renderFirstPage(pdfFile: File): Bitmap {
7    val fileDescriptor = ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_ONLY)
8    val renderer = PdfRenderer(fileDescriptor)
9    val page = renderer.openPage(0)
10
11    val bitmap = Bitmap.createBitmap(
12        page.width,
13        page.height,
14        Bitmap.Config.ARGB_8888
15    )
16
17    page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY)
18
19    page.close()
20    renderer.close()
21    fileDescriptor.close()
22
23    return bitmap
24}

You can then set the resulting bitmap on an ImageView.

Displaying It in an Activity

kotlin
val bitmap = renderFirstPage(File(filesDir, "sample.pdf"))
imageView.setImageBitmap(bitmap)

If the PDF comes from assets, copy it to a real file first because PdfRenderer works with a file descriptor, not directly with an asset stream.

Multiple Pages

To show multiple pages, repeat the openPage(index) plus render(...) process for each page. Do not keep many pages open at once. Open, render, and close them as needed.

That keeps memory usage under control, which matters because rendered PDF pages can become large bitmaps.

Remote PDFs

PdfRenderer does not load a PDF directly from a URL. If the document is remote, download it first to a local file, then open it with a file descriptor.

That is an important architectural point:

  • network fetch
  • local file storage
  • rendering

Treat those as separate steps instead of trying to make one API do all three.

Performance and Scaling

You do not always want to render at the page's full native size. For thumbnails or previews, render into a smaller bitmap sized for your target view.

For large documents:

  • render pages off the main thread
  • cache nearby pages if scrolling
  • recycle bitmaps when no longer needed

Rendering PDF pages on the UI thread is a common source of jank.

When to Use a Third-Party Library

PdfRenderer is excellent for low-level control, but it is intentionally basic. A third-party viewer library may be better when you want:

  • pinch zoom
  • page swiping
  • ready-made viewer widgets
  • more advanced PDF features

The tradeoff is more dependencies and less direct control.

Common Pitfalls

The biggest mistake is trying to pass a remote URL directly into PdfRenderer. It needs a local file descriptor.

Another mistake is forgetting to close PdfRenderer.Page, PdfRenderer, and ParcelFileDescriptor. Those are real native resources, not just lightweight Kotlin objects.

People also render large pages on the main thread and then wonder why scrolling or screen transitions stutter.

Finally, PdfRenderer is available only on API 21 and above, so check your minimum SDK assumptions.

Summary

  • 'PdfRenderer is Android's built-in API for rendering local PDF files.'
  • Open the file with ParcelFileDescriptor, then render each page into a bitmap.
  • Download remote PDFs to local storage before rendering.
  • Close pages and renderers promptly to avoid leaking resources.
  • Use a third-party viewer only if you need richer PDF features than PdfRenderer provides.

Course illustration
Course illustration

All Rights Reserved.