Quick revision
The short answer
Apache PDFBox is a low-level, Apache 2.0 licensed library where we draw text and lines at coordinates, which suits simple documents and manipulating existing PDFs. iText 7 and later is powerful but dual-licensed under the AGPL or a commercial licence, which matters if we distribute our software or offer it as a service; OpenPDF is an LGPL/MPL fork of old iText. For invoices, HTML-to-PDF with OpenHTMLtoPDF or Flying Saucer is often easiest, because we design the template in HTML and CSS and fill it with Thymeleaf. Whatever we choose, we embed fonts and check the licence with legal.
What to remember
In this answer, we compared ways to generate PDF invoices in Java. PDFBox is low level and Apache 2.0, good for simple documents and manipulating PDFs. iText 7 and later is feature-rich but AGPL or commercial, which matters for distributed software and services, while OpenPDF is an LGPL/MPL fork of old iText. HTML-to-PDF with OpenHTMLtoPDF or Flying Saucer lets us design invoices as Thymeleaf templates in HTML and CSS. In every case we embed fonts that cover our customers' scripts, and we check licences with legal before choosing.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Apache PDFBox is a low-level, Apache 2.0 licensed library where we draw text and lines at coordinates, which suits simple documents and manipulating existing PDFs. iText 7 and later is powerful but dual-licensed under the AGPL or a commercial licence, which matters if we distribute our software or offer it as a service; OpenPDF is an LGPL/MPL fork of old iText. For invoices, HTML-to-PDF with OpenHTMLtoPDF or Flying Saucer is often easiest, because we design the template in HTML and CSS and fill it with Thymeleaf. Whatever we choose, we embed fonts and check the licence with legal.
Why this question is asked
Generating invoices as PDF is a common requirement, and the library choice affects how quickly the team can change a layout, how the documents look in every language, and, less obviously, what legal obligations the company takes on. Interviewers ask this question because a good answer covers both the engineering trade-offs and the licensing, which many developers overlook until a legal review blocks a release. In this answer, we will recap what a PDF library does, compare the low-level approach with PDFBox, the iText family and OpenPDF, and the HTML-to-PDF approach, look at fonts, and then explain what the licences mean in practice.
1A Quick Recap of How PDF Libraries Work
A PDF page is not a flowing document like a web page. It is a canvas with instructions such as move to x=50, y=780, set the font to 12 points, draw this text. There is no automatic line wrapping, no table layout and no page breaking in the format itself. Anything like that has to be computed by the library or by us.
So PDF libraries fall into two groups. Low-level libraries give us the drawing instructions, and we compute positions ourselves. Higher-level libraries offer paragraphs, tables and page breaks, or take a layout language such as HTML and CSS and compute the positions for us.
2Low Level With Apache PDFBox
PDFBox is an Apache project under the permissive Apache 2.0 licence. Its strength is working with PDFs: reading text out of them, merging and splitting, filling forms, adding signatures and stamps. For creating documents it is low level: we open a content stream on a page and draw.
Notice that every position is ours to compute. A table of invoice lines with wrapping descriptions and a page break after line 30 becomes a small layout engine we must write and maintain. That is fine for a simple receipt or a label, and painful for a complex invoice that marketing wants to redesign every year.
3The iText Family and OpenPDF
iText is a very capable PDF library with a high-level layout API, tables, PDF/A and PDF/UA support, digital signatures and an HTML conversion add-on. Since version 5, and in today's iText 7 and later, it is dual-licensed: we may use it under the AGPL, or buy a commercial licence.
OpenPDF is a community fork of an older iText version from before the licence change, available under the LGPL and MPL. It offers a similar high-level API (Document, Paragraph, PdfPTable) and is a reasonable choice for code that already uses the old iText style, but it does not have all the features of modern iText.
4HTML to PDF With OpenHTMLtoPDF or Flying Saucer
For documents such as invoices, the most productive approach is often to design the layout in HTML and CSS, the tools many developers and designers already know, and let a renderer turn it into PDF. We render the HTML with Thymeleaf from our invoice data, then pass it to OpenHTMLtoPDF (which uses PDFBox underneath) or Flying Saucer (which uses OpenPDF).
| Library | Style | Licence | Best for |
|---|---|---|---|
| Apache PDFBox | Draw at coordinates | Apache 2.0 | Simple documents, manipulating PDFs |
| iText 7 and later | High-level layout API | AGPL or commercial | Rich features, if the licence fits |
| OpenPDF | Old iText-style API | LGPL / MPL | Existing iText 2 style code |
| OpenHTMLtoPDF, Flying Saucer | HTML and CSS templates | LGPL | Invoices designed as templates |
Notice what we no longer do: positions, wrapping and page breaks come from CSS, including paged-media rules such as @page for margins and page numbers. The limits are that these renderers need well-formed XHTML and support mostly CSS 2.1 with some extras, not flexbox or grid, so templates use tables and block layout. Also check the project's maintenance status, since some of these renderers are maintained by small groups or through forks.
5Fonts Must Be Embedded
Every PDF viewer knows 14 standard fonts, such as Helvetica and Times, but they only cover basic Western characters. If we rely on them, a customer named with Polish, Chinese or Arabic characters gets missing glyphs or an exception, and the document may look different on different machines. So we embed a font file, such as a Noto font, that covers the scripts our customers use. The library embeds only the characters actually used (a subset), so the file stays small. With HTML renderers we register the fonts with the builder and name them in the CSS font-family, with fallbacks for other scripts.
6What Licensing Means in Practice
A licence decides what we owe in return for using a library. Apache 2.0 is permissive: we can use PDFBox in closed-source commercial software, keeping the notices. The LGPL lets us use a library from proprietary code as long as we comply with its terms, mainly allowing the library itself to be replaced or modified; for a Java library used as a separate jar, most companies consider this manageable, but legal teams have their own rules.
The AGPL is the strong one. Like the GPL, it requires that software built with the library be made available as source under the AGPL when we distribute it, and it adds a network clause: if users interact with our software over a network, as with a SaaS invoicing service, we must offer them the source too. For most commercial companies that is not acceptable, so using iText 7 means buying a commercial licence. None of this is a decision for the engineering team alone: we bring the options to legal, with the exact libraries and how we use them, before choosing.
7Follow-up questions
Can we use iText for free in an internal tool?
The AGPL's obligations are triggered by distribution and by offering the software to users over a network, so a purely internal tool may be in a different position from a customer-facing service. But interpretations vary, internal tools often become external, and some companies ban AGPL libraries entirely. This is exactly the kind of question to send to legal rather than answer ourselves.
When would you still choose PDFBox for creation?
For simple, fixed layouts such as shipping labels or receipts, when we want no extra dependencies and the permissive licence, or when we are mainly modifying existing PDFs, such as stamping a reference number on a supplier's document or filling a PDF form.
How do you test PDF output?
We extract the text with PDFBox's PDFTextStripper and assert on key values, check that fonts are embedded, and for layout, render pages to images and compare them with approved snapshots, allowing small differences. Validators such as veraPDF check standards like PDF/A.
8Conclusion
In this answer, we compared ways to generate PDF invoices in Java. PDFBox is low level and Apache 2.0, good for simple documents and manipulating PDFs. iText 7 and later is feature-rich but AGPL or commercial, which matters for distributed software and services, while OpenPDF is an LGPL/MPL fork of old iText. HTML-to-PDF with OpenHTMLtoPDF or Flying Saucer lets us design invoices as Thymeleaf templates in HTML and CSS. In every case we embed fonts that cover our customers' scripts, and we check licences with legal before choosing.
Open with
There are three families. PDFBox, under Apache 2.0, is low level: I open a content stream and draw text and lines at coordinates, so I have to do my own wrapping, tables and page breaks.
Then, if they want more
It's great for manipulating PDFs, extracting text, forms and simple documents. iText has a high-level layout API and lots of features, but iText 7 and later is AGPL or commercial.
The AGPL means that if we distribute the software or let users interact with it over a network, we must offer our source under the AGPL, so for a commercial SaaS that usually means buying a licence. OpenPDF is an LGPL and MPL fork of the old iText, with a similar API.
For invoices I'd usually pick HTML to PDF: a Thymeleaf template in HTML and CSS rendered with OpenHTMLtoPDF, which sits on PDFBox, or Flying Saucer on OpenPDF. Designers can change the layout without layout code, though the renderers need XHTML and mostly CSS 2.1.
In all cases I embed fonts such as Noto, because the standard fonts only cover basic Western characters. And I'd review the licences with legal before committing.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare approaches to generating PDF invoices in Java: Apache PDFBox, OpenPDF, iText...": https://scrivio-interview-library.netlify.app/java/document-libraries/78d7a13406/