Using FO to generate pdf file

http://www.w3schools.com/xslfo/default.asp
FO is a web standard to generate pdf file from xml document. For detail information, pls click on the link provided, which is a good place to learn FO, surely better then FOP(an open source project in apache to generate pdf using java) document.

below is a snippet from the link:

XSL-FO Documents

XSL-FO documents are XML files with output information. They contain information about the output layout and output contents.

XSL-FO documents are stored in files with a *.fo or a *.fob extension. It is also quite normal to see XSL-FO documents stored with the *.xml extension, because this makes them more accessible to XML editors.
XSL-FO Document Structure

XSL-FO documents have a structure like this:

< ?xml version="1.0" encoding="ISO-8859-1"? >

< fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" >

< fo:layout-master-set >
< fo:simple-page-master master-name="A4" >
< !-- Page template goes here -- >
< /fo:simple-page-master >
< /fo:layout-master-set >

< fo:page-sequence master-reference="A4" >
< !-- Page content goes here -- >
< /fo:page-sequence >

< /fo:root >

Structure explained

XSL-FO documents are XML documents, and must always start with an XML declaration:

< ?xml version="1.0" encoding="ISO-8859-1"? >

The < fo:root > element contains the XSL-FO document. It also declares the namespace for XSL-FO:

< fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" >
< !-- The full XSL-FO document goes here -- >
< /fo:root >

The < fo:layout-master-set > element contains one or more page templates:

< fo:layout-master-set >
< !-- All page templates go here -- >
< /fo:layout-master-set >

Each < fo:simple-page-master > element contains a single page template. Each template must have a unique name (master-name):

< fo:simple-page-master master-name="A4" >
< !-- One page template goes here -- >
< /fo:simple-page-master >

One or more < fo:page-sequence > elements describe page contents. The master-reference attribute refers to the simple-page-master template with the same name:

< fo:page-sequence master-reference="A4" >
< !-- Page content goes here -- >
< /fo:page-sequence >

Note: The master-reference "A4" does not actually describe a predefined page format. It is just a name. You can use any name like "MyPage", "MyTemplate", etc.

/************************************************
One tricky thing when dealing with multiple items in one document, like you have 6 invoices, you want to generate one pdf document contains all of them and print it all at once.

in such situation, you need to have 6 < page-sequence >

to add pagging, you need to add initial-page-number="1" to tell FO that each invoice has its own paging

to remove blank page interval, you need to add force-page-count="odd" to tell FO do not add stupid blank interval page, which is default.
/************************************************
8,554 views 6 replies
Reply #1 Top
i could be wrong or misunderstanding this but wouldnt it be easier (altho much less instructive to be sure) simply to use openoffice to convert xls, xml or for that matter any document creatable using openoffice directly to pdf? 
Reply #2 Top
Sorry, I don't know much about openOffice, maybe it's a good thing I should have a look.

But here, FO, I'm talking about can be used in my Java code to generate dynamic pdf output.

I'm not sure if openOffice can make that or not.
Reply #3 Top

lee, I've done Java based dynamic PDF too, it's nice to see someone else here doing this. For what it's worth, I created Java class FDFMaker.java to create a .fdf file on the fly. I can't find the code here but in case you haven't tried this fdf is a pseudo xmlish format file containing form data called by a .pdf file to create a dynamic PDF on the fly. Super quick and dirty and scales fine; my code is live on the net and being used to print out 20,000 tuition receipts in a full J2EE implementation. I don't think FOP was this advanced when I started coding this stuff back in 2000 so maybe it's a better way, I just wanted to share my experience.
Reply #4 Top
Cool. would you tell me the url, so that I can try that? I don't like fop very much, just haven't found a better way.
Reply #5 Top

No url, dude, just roll your own. Search on PDF, FDF, etc. Adobe's site should have info. PDFPlanet.com or some other site will have the info you need. Find an example .FDF file and you'll see how easy it is. The idea is to create a simple text file with .fdf extension in fdf format at runtime which contains form data (name, address, etc.). This is merged with a PDF template to create a seamless doc. I'm on vacation so can't help much more but I'll keep an eye out here if you have any questions. Bonne chance.
Reply #6 Top
ok, the point is: is it a embedded stuff or a standalong product? Can I use it inside my java code?

say if I have a collection of data, I'd like to generate a report, what should I do in my code, if I use the openoffice you mentioned?