Skip to content
webtoapkfree

How do I open PDF files in an Android WebView?

Android WebView can't open PDF files on its own. Show them with pdf.js, hand them to a PDF app with an intent, or use the AndroidX PDF viewer.

GuidesNookesh Karri

Light cover card with the title 'How do I open PDF files in an Android WebView?' in dark text and a green accent bar

To open a PDF in an Android WebView, you need a viewer, because Android WebView can’t open PDF files by itself. The three reliable options are: show the PDF through a JavaScript viewer such as pdf.js, hand it to a PDF app with an intent, or show it natively with the AndroidX PDF viewer. Pick by where the PDF lives and who may see it.

Why an Android WebView won’t open PDF files

Android WebView doesn’t render PDFs, so a PDF link shows a blank page or turns into a download. Unlike iOS, which has PDF support built in, Android’s WebView does not support direct viewing of PDFs, and the usual answer is a JavaScript viewer hosted in the WebView.

When WebView meets content it won’t display, it calls the app’s DownloadListener to say “a file should be downloaded”. The callback gets the URL, the user agent, the Content-Disposition header, the MIME type the server reported and the file size.

So the blank screen has two common causes:

  • No DownloadListener. The tap is handed off, and nothing in the app handles it.
  • A listener that only downloads. The PDF saves quietly, but the user never sees it open.

A typical real case: a school app lists timetables as timetable.pdf links. In Chrome they open in a tab. In the WebView app, students tap and nothing happens.

Which method to use

The right way to show a PDF depends on whether it’s public, behind a login, or bundled with the app.

Situation Best method App code needed
Public PDFs on your site pdf.js viewer hosted on your site None
PDFs behind a login pdf.js on your site, same session None
PDF users want to keep or print Intent to a PDF app Yes
PDF bundled for offline use pdf.js in app assets, or native viewer Yes
Native look, search, newer phones only AndroidX PdfViewerFragment Yes

Show the PDF with pdf.js on your own site

Hosting pdf.js on your website lets a WebView app display PDFs with no app change at all. PDF.js has a prebuilt download that includes the library and its viewer, the same viewer Firefox uses, and it’s licensed under Apache 2.0.

The steps:

  1. Download the prebuilt PDF.js package and upload the viewer folder to your site.
  2. Change your PDF links to point at the viewer page, passing the PDF’s address to it.
  3. Keep the PDFs on the same domain as the viewer, so the viewer can fetch them.
  4. Open the link in the app and in a phone browser to check both.

Because the viewer is just a web page, the WebView renders it like the rest of your site. PDFs behind a login also work, since the viewer requests the file with the same cookies as the page. For private files, developers recommend the same route: embed pdf.js rather than pointing WebView at the file.

One catch from the docs: the PDF.js worker isn’t enabled for file:// URLs, so serve the viewer from a real server.

Open the PDF in a PDF app with an intent

Handing the PDF to an installed PDF app is the simplest native option. If you don’t need the document inside your app, you can start an activity and let Android pick the app.

In your WebViewClient, catch links that end in .pdf (or a DownloadListener call with the MIME type application/pdf) and start an ACTION_VIEW intent with the address. For a file the app has already downloaded, don’t pass a file:/// path. Use FileProvider to create a content:// URI and add FLAG_GRANT_READ_URI_PERMISSION. Android’s docs call file-system permissions for file:/// sharing “fundamentally insecure”.

This route suits invoices and certificates that people save or print anyway. It fails on phones with no PDF app, so catch that case and show a message.

Bundle a PDF viewer inside the app

Bundling pdf.js in the app’s assets lets PDFs open offline, at the cost of extra setup. One walkthrough puts a viewer HTML file, its script, and pdf.js, pdf.worker.js and pdf.sandbox.js under src/main/assets/. It then turns on JavaScript and file access and loads the viewer from file:///android_asset/ with the PDF address as a query parameter.

Turning on file access widens what pages in your WebView can read. Only do it if the WebView loads nothing but your own content.

Use the native AndroidX PDF viewer

Android now has an official PDF screen you can open next to the WebView. The PdfViewerFragment shows pages in a paginated view, renders only the visible pages, and offers a search bar through isTextSearchActive. You give it the document by setting documentUri.

The requirements are strict. The fragment needs Android S (API level 31) and SDK extension level 13, and throws UnsupportedOperationException otherwise. The guide still lists the dependency as 1.0.0-alpha0X, so expect API changes.

For older phones there’s PdfRenderer, added in API level 21. It draws one page at a time to a bitmap from a seekable file, so you build scrolling and zoom yourself. It throws SecurityException on password-protected files unless you use the newer constructor added in API level 35. For untrusted files, Android recommends running it in a separate isolated process.

Avoid the online document viewer trick

Many old answers wrap the PDF link in an online document viewer’s address, but that’s the weakest option. It depends on a third-party service, and one developer hit a bandwidth limit error that left the viewer empty.

My take: don’t use it for anything private, like payslips or medical letters. The file passes through someone else’s servers, and you can’t fix it when it breaks. Hosting pdf.js yourself takes about as long to set up and you own every part of it.

PDFs in a Web2Apk app

Web2Apk packages your website into a WebView app, and it doesn’t promise every site feature behaves as in Chrome. Downloads, sign-in, permissions and external links can work differently, so test your PDF links on a real phone before you publish.

The pdf.js-on-your-site route fits best, because live website content changes without an app update. You add the viewer to your site, and the app you already shipped starts showing PDFs.

The limitation: you can’t bundle PDFs into the app. The HTML importer takes one file up to 100 KB, and ZIP or folder imports aren’t supported, so offline PDFs aren’t an option. The website to APK guide lists the other WebView differences to check.

For related fixes, see WebView file download not working, opening external links in the browser and testing an APK on your phone.

FAQ

Android’s WebView has no built-in PDF viewer, unlike iOS. A PDF link either shows nothing or is passed to the app’s DownloadListener as a file to download.

What is the easiest way to show a PDF inside a WebView app?

Host a JavaScript viewer such as pdf.js on your own website and link to it with the PDF’s address. The WebView renders the viewer like any other page, so the app itself needs no changes.

How do I open a PDF in another app from a WebView?

Catch the PDF link, then start an ACTION_VIEW intent with the PDF’s address or a FileProvider content URI. Android lets the user pick an installed PDF app.

Can I use the AndroidX PDF viewer in a WebView app?

Yes, as a separate screen. PdfViewerFragment needs Android S (API level 31) and SDK extension level 13, so you still need a fallback for older phones.

It depends on a third-party service, so the file goes through someone else’s servers and can hit their limits. Avoid it for private documents.

Sources