PDF Printer

The PDF Printer is a simpler version of the PDF3Generator. The PDFPrinter lacks support for generating tables and TOC (Table of Contents). To create a PDFPrinter, pass a File object or a FileOutputStream to the constructor:

PDFPrinter pdf = new PDFPrinter(new File("report.pdf")); 

Once a PDFPrinter object is created, a PrintJob can be retrieved from it by calling getPrintJob():

report.print(pdf.getPrintJob()); 

After printing is done, we need to close the PDF stream:

pdf.close(); // or pdf.getPrintJob().end(); 

It is important to close the PDF stream after printing. Otherwise, the PDF file may not be complete. The PDF output can be closed by either calling the end() method on the PrintJob object returned by the PDFPrinter, or by calling the close() method on the PDFPrinter directly.

Compressed PDF

There are a few options that control how PDF files are generated. By default, text and images in a document are compressed. Since InetSoft products only support Zip compression, the generated document requires Acrobat Reader version 3.0 or later. If the PDF file needs to be compatible with earlier versions, the compression can be turned off manually:

 pdf.setCompressText(false);
 pdf.setCompressImage(false); 

Note: The compression needs to be turned off for CJK operating systems. Otherwise some of the binary data will be corrupted in the PDF file.If compression is turned off, you risk the possibility of a large PDF file. This is especially true if you use images or painters in a report.

Font Handling in PDF (Base-14 Fonts)

Another difference in printing a PDF file is the handling of fonts. In PDF, 14 fonts are guaranteed to exist in a viewer. They are called the base14 fonts. The base14 fonts are listed below:

  • courier
  • courier-bold
  • courier-boldoblique
  • courier-oblique
  • helvetica
  • helvetica-bold
  • helvetica-boldoblique
  • helvetica-oblique
  • symbol
  • times-bold
  • times-bolditalic
  • times-italic
  • times-roman
  • zapfdingbats

In order to ensure maximum portability, the Java font objects are mapped into the base14 fonts when generating PDF files.

Table 8. Default mapping between Java Fonts and Base-14 Fonts

Java Font Name

Base14 Font Name

Dialog

Helvetica

Dialoginput

Courier

Serif

Times

SansSerif

Helvetica

Monospaced

Courier

This mapping can be changed:

pdf.putFontName("dialog", "Times"); 

By default the PDFPrinter assumes the output page is letter size (8.5 x 11). To change this, set the page size property:

// switch width and height to print in landscape
pdf.setPageSize(11, 8.5);
// or print on A4 paper
pdf.setPageSize(StyleConstants.PAPER_A4); 

Previous: Import and Export Report File Formats