[clug-progsig] Creating a PDF?

Neil Mayhew neil_mayhew at users.sourceforge.net
Thu Apr 3 21:35:29 PDT 2008


On 4/3/08 6:03 PM, Shawn wrote:

> I thought the Apache FOP thing was Java based. This lead me to believe 
> that I'd need to code in Java to use it. This is not the case?
>   

Although fop is written in Java, it's an executable, not a library. If 
you have an FO document mydoc.fo, you use it like this:

fop -fo mydoc.fo -pdf mydoc.pdf

That will produce mydoc.pdf as an output.

> Can you post some of these command line techniques? Or a very quick sample?

You can find examples of simple FO documents online. See, for example, 
the examples that come with fop, which are described at 
<http://xmlgraphics.apache.org/fop/examples.html> and can be viewed 
directly in svn at 
<http://svn.apache.org/repos/asf/xmlgraphics/fop/trunk/examples/fo/basic/>, 
or see <http://www.dpawson.co.uk/xsl/examples.html> for a complete 
workflow XML -> FO.

> I was already thinking I'd be putting the data into XML so that it could be re-purposed, so the FO approach is an attractive option if I don't need to do Java coding.
>   

As you'll see from the dpawson page, most people generate FO from 
something else, typically using XSLT stylesheets. This means you put 
your data into an XML file using markup that reflects the structure and 
nature of the data. You then write a stylesheet that transforms that 
into FO that reflects how you want it to look on the page. You can run 
the stylesheet and create PDF in one step with fop:

fop -xml mydata.xml -xsl mystylesheet.xsl -pdf mydoc.pdf

I have a working application for printing address labels from an address 
database. The address records look like this:

  <record>
    <title>Mr &amp; Mrs A N</title>
    <last>Other</last>
    <address>567 Some Rd</address>
    <city>Calgary</city>
    <state>Alberta</state>
    <country>Canada</country>
    <zip>T2Z 1X4</zip>
    <!-- other info -->
  </record>

I have a stylesheet that turns these into an FO document containing a 
table where each cell is one label, and I print the resulting PDF onto 
label sheets.

Of course, there's no need to use XSLT - you could just create the FO 
directly with, eg, PHP or JavaScript. However, I find that XSLT is a 
very slick and easy way to do things with XML, once you learn the basic 
ideas. It is "a bit different" though :-)

I've done quite a lot of XSLT, and have example code I could send you 
off-list if you're interested. I've also written a lot of DocBook XML 
documents, which I transform to FO and then PDF using the standard 
stylesheets (eg available as an Ubuntu package). DocBook is an XML book 
markup that is good for reports, articles, web sites and many other things.

--Neil

PS For some reason, fop seems not to be in Ubuntu at the moment. 
However, the downloadable fop binary is usable directly 
<http://ubuntuforums.org/showpost.php?p=3848794&postcount=3>.



More information about the clug-progsig mailing list