Content with Style

Web Technique

generate PDF with XSL-FO and FOP

by Matthias Willerich on April 3 2007, 08:57

A lot of 3-letter abbrevations here, I wanted to share some notes about this.
So, the scenario I was faced with was to create a pdf with some dynamic content (text, pixel-based image). As usual, nobody wants to spend any money and the “open-source” card is played early.
So, after a short look I find FOP, an apache project that renders pdf and other formats out of a XSL-FO source.
XSL-FO is an XML subset, that allows you to mark up content in order to generate print formats, such as pdf, rtf or postscript.

My trouble started when I had to embed a font, as pdf only comes with a small preconfigured set :
The Adobe PDF Specification specifies a set of 14 fonts that must be available to every PDF reader: Helvetica (normal, bold, italic, bold italic), Times (normal, bold, italic, bold italic), Courier (normal, bold, italic, bold italic), Symbol and ZapfDingbats.

For FOP you could create a metrics file needed to embed a font with an accompanying tool. On OS X I discovered that Preview did not display the text using the font. Luckily that was fixed quickly when I tried out the -enc ansi option. I’m not sure why there are 2 ways to create a metrics file, but hey, the ansi version works with Preview, the default one (CID-keyed) doesn’t.
Everything seemed fine.

Until I realised that this version only creates 72dpi output, regardless what you specify. Not great when the pdf is meant to be printed out.
So I went for the most recent release, fop 0.93. While I could now generate 150dpi output (or any kind of resolution, for that matter), it didn’t recognize my previously generated metrics file, or font, and gave me grief with some warnings similar to this:

SEVERE: Failed to read font metrics file null Reached EOF, file size=5104 offset=5104

After some international swearing, a lunch-break and some research I found this little gem in their mailing list. It kinda reads as if they literally forgot to test with their own test fonts before release. But who am I to complain, I didn’t spend a minute developing it, did I?
So, after downloading an updated font file reader together with ant and the source package of the 0.93 release, I compiled the whole thing again, without any problems, and started rendering away happily.


  • so where can i see one of these fancy pdfs that your server is kicking out? i’m honestly just curious.

    by naterkane on April 4 2007, 03:23 #

  • Ah. Well, things are still in development, and I can’t really show you the ones this is all about; but I could rustle up a demo that does the same thing, together with the xsl-fo. Yes, that would be nice of me. I’ll fix something up tomorrow.

    by Matthias on April 4 2007, 05:06 #