Android: Print PDF to Thermal Portable Wireless Printer

Recently I got the request to write an application that prints a PDF file on a portable Thermal printer.

Normally that does not make sense, as the protable printers come with 2, 3, 4 or 5 inch paper width only. But the PDF files are created for 3 inch paper and os this Receipt printing makes sense.

The idea was to render the PDF to a bitmap and then print the bitmap to the printer. These printers do not support PDF or Postscript, they come with special Printer Language support as called ZPL, ESP/P, CPCL or others. So we need to create a Bitmap of the PDF first and then print the Bitmap.

I started using the Android Google PDFRenderer but this fails with two issues: the bitmaps created are always transparent and print with black background where white has to be; the text was not rendered as the Fonts did not render.

Among others I found PDFbox and after fixing a small issue with scaling of bitmaps the solution works very well.

The CPCL_Sample code and application can load a PDF file. This is then converted to a scaled bitmap which can then be printed to a CPCL compatible printer. The CPCL performance is very different and printing on a Zebra takes about a minute for a 3 by 4 inch sized bitmap.

The code has to use a lot of intents and background tasks as most processing takes some time. The bitmap is created by an IntentService. The Sewoo SDK prints the bitmap first to a queue buffer and there is no control or feedback about the status of the print job. So it hopefully prints after some time.

By replacing the print SDK you should be able to print the bitmap of the rendered PDF to any Bitmap-Printing capable printer.

Be warned: there are Printers that are compatible with a Printer Language like CPCL but may not support Bitmap printing.

Source code and apk at github.

Leave a Reply