Dynamic PDF Generation

By Neil Giarratana

From CFSouth Conference, February 3, 2001

This process arose from an internal need for dynamic reporting. Crystal Reports was not an option.

Working with PDF

  • Native PDF code
  • Acrobat API (commercial)
  • FDF files
  • FOP

FOP is by the Apache Project. It is a series of Java classes that use XSL formatting objects to create PDF files. FO Document → FOP Java Server → PDF

  1. Verify JDK is installed.
  2. Install Java XML Parsing Engine (Xalan and Xerces).
  3. Install FOP.

The Classpath is the most critical step.

Formatting Objects

A subset of XSL: XSLT is the first half, FO is the second. FO is similar to CSS.

FO Document Format

Root section – w3.org DTD

Page Template(s) – like a slide master

Page Sequence(s)

Page Regions
  • region-before
  • region-after
  • region-body
  • region-start
  • region-end

diagram of the FO document regions

Body overlaps the other regions.

 
Three elements in a page sequence:
  • Title – usually you can ignore this.
  • Static-content – content for every page, like headers and footers.
  • Flow – regular content

The elements must be in the order listed.

Block tag – like a <div> tag. There is no break tag, so blocks are the only way to get a CF/LF equivalent.

Inline tag – changes styles without a new block (CF/LF).

You can insert GIF and JPEG images, as well as vector graphics.

List-block tags – like HTML lists.

Tables – very like HTML tables, except it actually uses the dimensions you give it.

  • fo:table
  • fo:table-column
  • fo:table-row
  • fo:table-cell

Out-of-line block – not supported by FOP. Used to add footnotes or floating elements.

Keeps & Breaks
  • keep-with-next
  • keep-with-previous
  • keep-together
  • break
Hyphenation – automatic hyphenation
Indentation
  • start-indent
  • end-indent
  • text-indent
Character
  • color
  • font-family
  • font-size
Sentence
  • line-spacing
  • line-height
  • text-align

FO Document

The FO document combines these to describe the entire document. 

Dynamic PDF using CFML

CFML Template → CF Server → FO Document → FOP Java Processor → PDF Doc

  1. Verify JDK is installed.
  2. Install XML Parser
  3. Install FOP
  4. Copy cfxml.class into FOP directory.
  5. Copy pdf.cfm custom tag into Custom Tags directory.
  6. Use CFML to define the FO document dynamically.
Do your query output, etc. in the page flow section.

Call your FO generator page from another CFM page that sets the cfcontent to the “application/pdf” MIME type, does a CFHTTP call to the FO generator page, and then calls the Create PDF custom tag.

Apache XML project – http://xml.apache.com/