Printing HTML documents

Printing out content beyond a simple photo on Android requires composing text and graphics in a
print document. The Android framework provides a way to use HTML to compose a document and
print it with a minimum of code.

In Android 4.4 (API level 19), the WebView class has been updated to
enable printing HTML content. The class allows you to load a local HTML resource or download
a page from the web, create a print job and hand it off to Android's print services.

This lesson shows you how to quickly build an HTML document containing text and graphics and
use WebView to print it.

Load an HTML document

Printing an HTML document with WebView involves loading an HTML
resource or building an HTML document as a string. This section describes how to build an HTML
string and load it into a WebView for printing.

This view object is typically used as part of an activity layout. However, if your application
is not using a WebView, you can create an instance of the class
specifically for printing purposes. The main steps for creating this custom print view are:

Create a WebViewClient that starts a print job after
the HTML resource is loaded.

Note: Make sure your call for generating a print job happens in the onPageFinished() method of the WebViewClient you created in the previous section. If you don't wait until page
loading is finished, the print output may be incomplete or blank, or may fail completely.

Note: The example code above holds an instance of the
WebView object so that is it not garbage collected before the print job
is created. Make sure you do the same in your own implementation, otherwise the print process
may fail.

If you want to include graphics in the page, place the graphic files in the assets/
directory of your project and specify a base URL in the first parameter of the
loadDataWithBaseURL() method, as shown in the
following code example:

An HTML document containing CSS print attributes, such as landscape properties, is not
supported.

You cannot use JavaScript in a HTML document to trigger printing.

Note: The content of a WebView object that is included in
a layout can also be printed once it has loaded a document.

If you want to create a more customized print output and have complete control of the content
draw on the printed page, jump to the next lesson:
Printing a custom document lesson.

Create a print job

After creating a WebView and loading your HTML content, your
application is almost done with its part of the printing process. The next steps are accessing
the PrintManager, creating a print adapter, and finally, creating a print
job. The following example illustrates how to perform these steps:

This example saves an instance of the PrintJob object for use by the
application, which is not required. Your application may use this object to track the progress of
the print job as it's being processed. This approach is useful when you want to monitor the status
of the print job in you application for completion, failure, or user cancellation. Creating an
in-app notification is not required, because the print framework automatically creates a system
notification for the print job.