3

In a project I am working on, I have a requirement to generate a PDF. This PDF will contain text only, and the contents of the text will be rather variable. Most of the text should be printed in separate paragraphs printed under each other. In case the paragraph doesn't fit on a page, it should just be added to the next one. It should be free (as in beer, preferably as in open-source too) and stable (so not some toy project).

As a sidenote, the project itself will be used commercially. Users will be able to send an API call, and a specific PDF will be generated for them (its contents are what we specialize in).

3

According to What is the best PDF open source library for Java? [closed] over at StackOverflow your best bet could possible be iText:

iText is a PDF library that allows you to CREATE, ADAPT, INSPECT and MAINTAIN documents in the Portable Document Format (PDF): - Generate documents and reports based on data from an XML file or a database - Create maps and books, exploiting numerous interactive features available in PDF - Add bookmarks, page numbers, watermarks, and other features to existing PDF documents - Split or concatenate pages from existing PDF files - Fill out interactive forms - Serve dynamically generated or manipulated PDF documents to a web browser

iText is used by Java, .NET, Android and GAE developers to enhance their applications with PDF functionality.

Several iText engineers are actively supporting the project on StackOverflow: https://stackoverflow.com/questions/tagged/itext

This library seems to be free for personal use.

  • It seems that iText isn't free for commercial use, however. Its usage will be in a commercial API (basically, a specific kind of documents will be created for the user) – Kristof Jun 30 '15 at 11:51
  • @Kristof I see you edited in that part about needed for commercial use :-). You might check the reference link, and see if Apache FOP is a better alternative for you – holroy Jun 30 '15 at 11:55
  • Yep, I edited it for clarity :) – Kristof Jun 30 '15 at 11:57
  • 1
    I checked on Apache FOP, after messing around with it, it definitely seems like a great solution. Pretty darn fast too, thanks! – Kristof Jul 1 '15 at 10:27
  • @Kristof Could you write an answer on Apache FOP, what it does and how it fits your need? – Angelo Fuchs Jul 18 '15 at 20:01
3

Apache FOP, in combination with Freemarker is a great solution for setting up a simple templating system to generate PDF's with not much more than text and some basic images on it.

Freemarker is used to create dynamic templates which result in an XML in the style of:

<letter>
    <paragraph>Some static content always in here</paragraph>
    <paragraph>Bla bla bla</paragraph>
    <paragraph>Foo foo foo</paragraph>
</letter>

Where the bla bla bla and the foo foo foo actually come from a List in a POJO. FOP can also be used to create this XML (or have an entirely dynamic template), but it feels more brittle to do this.

Apache FOP is then used to create an XML out of the PDF. You basically tell FOP that for a "paragraph" it has to create a textblock in the PDF with certain specifications.

That way you can use it like you would use CSS in an ordinary webpage, and totally split up the content generation and structure (Freemarker) and the final presentation (Apache FOP).

  • This sounds like a neat solution, could you please add links to Freemarker and Apache FOP? – holroy Jul 19 '15 at 11:26
  • For me it's a pretty neat solution. If I need extra kinds of documents, I just add Freemarker templates. If I need extra template visualizations (like a specific kind of paragraph) I just add stuff to the FOP tempate. Also, by editing my FOP template, I affect every kind of document generated. – Kristof Jul 19 '15 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.