The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.

Printing and Document Generation

Mobile devices are continuing to close the gap on capabilities that had formerly been the sole province of desktop systems or servers. After all, if the vision is that people should be able to use phones and tablets instead of desktops and notebooks, phones and tablets need to do whatever it is that those people need to have done.

One such capability is the ability to print to networked printers. While various third-party printing options had been available for some time, it is only starting with the Android 4.4 release that the OS and framework itself has support for printing. Hence, at this time, a significant majority of Android devices will be natively capable of printing, and so users will be more likely to expect that your app supports such printing.

As it turns out, the print engine in Android is centered upon the PDF document format, and Android supports converting HTML into PDF, albeit on a somewhat limited basis.

The API seems simple and clean. It actually is simple and clean… so long as you are printing very simple contents (bitmaps or HTML). Once you get into anything more complicated than that, the threading alone starts to make things rather messy.

This chapter describes how to use the Android 4.4 print system, including how to print HTML and PDF files. It will also cover how to generate HTML and PDF files, whether for printing or for other purposes (e.g., reports to be emailed or uploaded somewhere).


Understanding this chapter requires that you have read the core chapters of this book. Also, you should read the chapter on advanced uses of WebView.

The Android Print System

Writing programs that print on desktop operating systems historically has been tedious. The fine-grained control that is needed for high-quality output makes the APIs complicated, and these tend to be only partially masked by high-level wrappers to simplify common scenarios.

Android’s print system is no different.

Starting with Android 4.4, you can request access to a PrintManager system service (via getSystemService(), called on any Context). It offers a print() method that lets you describe what should be printed, in the form of a PrintAttributes (e.g., what size paper are you looking for?) and a PrintDocumentAdapter. The latter is responsible for working with Android to actually create the content to be printed.

print() returns a PrintJob, which you can use to examine the status of the print request. PrintManager also offers a getPrintJobs() method that returns all of your outstanding print requests. Note that you cannot access print jobs from other applications.

Hence, the real complexity of printing lies in the PrintDocumentAdapter implementation. This class is responsible for generating a PDF that represents the content to be printed. This leads to four basic ways of working with a PrintDocumentAdapter:

  1. Have one created for you, such as via a WebView for printing HTML content
  2. Create one that takes a PDF generated elsewhere and uses it for the output
  3. Create one that uses Android’s Canvas-based PDF generation class, called PrintedPdfDocument
  4. Use APIs that avoid all of this entirely, such as printBitmap() on PrintHelper

About the Sample App

The preview of this section may contain nuts.

Printing a Bitmap

The preview of this section was eaten by a grue.

Printing an HTML Document

The preview of this section was the victim of a MITM ('Martian in the middle') attack.

Printing a PDF File

The preview of this section was traded for a bag of magic beans.

Printing Using a Canvas

The preview of this section was lost in the sofa cushions.

Print Jobs

The preview of this section left for Hollywood to appear in a reality TV show.

Printing, Threads, and Services

The preview of this section apparently resembled a Pokémon.

Printing Prior to Android 4.4

The preview of this section is presently indisposed.

HTML Generation

The preview of this section is presently indisposed.

PDF Generation Options

The preview of this section was whisked away by a shark-infested tornado.