Search code examples
javaxmlpdfxsltapache-fop

Generate one PDF using multiple FO objects using apache fop in java


Can anyone suggest me how to create one pdf using multiple Apache fo objects?

We know that Apache fop generates pdf using FO(formatting objects). FO is created from xml by applying xsl.

My requirement is, I will be generating multi page pdf files. Each page template will be different, so I would need multiple xml and xsl files for one pdf. I thought of merging them and creating one FO but some PDF size would go till 2000 pages which leads to OutOfMemoryError.

I went through the Apache FOP reference example files for creating one pdf using multiple fop but I could not find one.


Solution

  • An FO file you can contain as many fo:page-sequence elements as you want or need, each one with its master-reference property pointing to a page master.

    So, you can have a single FO file with

    • several fo:page-sequence elements with the same page layout (think of the different chapters in a book)
    • several fo:page-sequence each one with a different layout
    • a combination of the previous cases

    Besides, FOP processes the content of each page fo:page-sequence independently, so having many smaller page sequences instead of a giant one (if possible) also helps avoiding out-of-memory errors.