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:
- obtain a
ParcelFileDescriptorfor the PDF file - create a
PdfRenderer - open a page
- render the page into a
Bitmap - show the bitmap in an
ImageView
Here is a minimal Kotlin example:
You can then set the resulting bitmap on an ImageView.
Displaying It in an Activity
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
- '
PdfRendereris 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
PdfRendererprovides.

