Skip to content
webtoapkfree

How do I print a page from an Android WebView app?

To print a page from an Android WebView, pass the WebView's print adapter to PrintManager. The site's own window.print() button needs a native bridge.

GuidesNookesh Karri

Light cover card with the title 'How do I print a page from an Android WebView app?' in dark text and a green accent bar

To print a page from an Android WebView app, the app asks the WebView for a print adapter and passes it to Android’s PrintManager, which opens the system print screen. A Print button on the website that calls window.print() won’t do this by itself. You have to connect that button to the native print call.

How printing a page from an Android WebView works

An Android WebView prints a page by handing its content to the system print service. WebView was updated in Android 4.4 (API level 19) to print HTML content, the same release that added the printing framework to Android.

The app’s part is three calls:

  1. Get the PrintManager system service from the activity.
  2. Ask the WebView for an adapter with createPrintDocumentAdapter(jobName).
  3. Call printManager.print(jobName, adapter, attributes).

In Kotlin, inside an activity that holds the WebView in a field named webView, that looks like this:

fun printCurrentPage() {
    val printManager = getSystemService(Context.PRINT_SERVICE) as PrintManager
    val jobName = "Invoice from my shop"
    val adapter = webView.createPrintDocumentAdapter(jobName)
    printManager.print(jobName, adapter, PrintAttributes.Builder().build())
}

print() brings up the print UI and returns a PrintJob straight away, without waiting for the user to confirm. You don’t need to show your own progress notice, because the print framework creates a system notification for the job.

The call only works from an Activity. From anywhere else it throws IllegalStateException.

Why the website’s Print button does nothing

A web page’s Print button fails in a WebView because the WebView doesn’t run window.print(). In a browser, window.print() opens the print dialog for the current document. In a WebView, Android’s own guide lists it among the limits: “You cannot use JavaScript in a HTML document to trigger printing.”

A 2023 fix proposed for a Cordova in-app browser plugin starts from the flat statement that window.print is not implemented in Android’s WebView.

A typical case: a shop’s order page has a “Print invoice” button. In Chrome on the same phone it opens the print preview. Inside the WebView app, the customer taps it and nothing happens. No error, no dialog.

Two ways to connect the Print button to the app

You make the website’s button work by passing the tap to native code, which then runs the print call shown above.

Override window.print. The Cordova fix adds a print method to the plugin’s JavaScript interface, then replaces window.print so it calls that method. You can do the same in your own app. Register an object with addJavascriptInterface, and after each page load run a line of script such as window.print = function () { AndroidPrint.print(); }. The website needs no change, because its existing button keeps calling window.print().

Have the native method switch to the UI thread before it touches the WebView.

Use a print: link. One open-source WebView template has the page link to a special print: address, such as <a href="print:">Print this page</a>. The app catches that address in shouldOverrideUrlLoading and starts the print job from the current WebView content. This needs no JavaScript interface, but you have to edit the website, and the link is dead in a normal browser.

Situation Pattern to use
The site already calls window.print() Override window.print
You can’t edit the website Override window.print
You’d rather not expose a JavaScript interface print: link
The same page must also print in a browser Override window.print

My take: override window.print. The site keeps one button that works in both the browser and the app, and you never have to hide a link from browser visitors.

Save the page as a PDF

Saving a WebView page as a PDF uses the same print job, so there’s no separate API to call. When the standard Android print preview appears, the user can select a printer, save as a PDF, or adjust settings.

What goes wrong with WebView printing

Most WebView print failures come from timing or from limits the official guide spells out.

  • Blank or partial pages. If you print a page the app loads just for printing, start the job in onPageFinished. Start earlier and the output “may be incomplete or blank, or may fail completely”.
  • The job never starts. Keep the WebView in a field until the adapter reaches PrintManager. A WebView that gets garbage collected first can make printing fail.
  • Rotation mid-print. If your activity doesn’t handle a configuration change such as rotation, the system drops its connection to the adapter and you need a new one.
  • Two jobs at once. One WebView instance can process only one print job at a time.
  • Text missing from the PDF. This one may not be your bug. In June 2022, developers reported that System WebView 102.0.5005.78 printed normal text as blank and kept only bold characters. If output breaks on some phones only, check the WebView version on them.

The guide also lists things WebView printing can’t do at all. You can’t add headers or footers, including page numbers. Page ranges aren’t supported, so nobody can print pages 2 to 4 of a long document. And CSS print attributes such as landscape properties aren’t supported.

That’s the hard limit of this approach. If you need page numbers or a fixed layout, generate a PDF on your server and open the PDF from the WebView instead.

Make the page look right on paper

A print stylesheet on your website controls what the printout contains. Styles inside @media print apply only to printed content, so that’s where you hide the menu, the footer and the buttons with display: none.

Test with the Save as PDF option before wasting paper. To see why a rule isn’t applying, inspect the WebView from desktop Chrome.

Printing in a Web2Apk app

A Web2Apk app is built from a generic Android template plus your saved settings, so you can’t add your own native print bridge to it. Web2Apk also doesn’t guarantee that every website feature behaves as it does in Chrome, and printing isn’t something it promises. Install the APK and tap your Print button on a real phone before you rely on it.

If the button does nothing, move the work to your website. Offer the document as a server-generated PDF and let people save or open it. Live website content changes without an app update, so the app you already shipped picks up the new link. Read WebView file download not working first, since downloads have their own traps, and see the website to APK guide for the other WebView differences.

FAQ

Why does window.print() do nothing in an Android WebView?

Android’s WebView doesn’t implement window.print(), and Android’s own guide says an HTML document can’t use JavaScript to trigger printing. The app has to start the print job in native code.

Can I save a WebView page as a PDF?

Yes. Once the app starts a print job, Android’s print preview screen lets the user pick a printer or save the page as a PDF.

Why is my WebView printout blank or cut off?

The usual cause is starting the print job before the page finished loading. Start it from onPageFinished, and keep a reference to the WebView until the print adapter is handed to PrintManager.

Can I add page numbers or print only some pages from a WebView?

No. WebView printing can’t add headers or footers, including page numbers, and it doesn’t support page ranges.

Which Android version supports printing from a WebView?

Android 4.4 (API level 19) and higher. That release added the printing framework and updated WebView to print HTML content.

Sources