For OpenPDF 3.0, use the openpdf-html module: pass an HTML string to ITextRenderer, call layout(), then write the PDF with createPDF(). The project’s documented example uses the org.openpdf namespace. This path is suitable when its HTML and CSS rendering meets your template’s needs; it is not a guarantee of browser-identical output.
1. Add the OpenPDF HTML module
The project documents com.github.librepdf:openpdf-html as its HTML-to-PDF renderer. Its matching core artifact is com.github.librepdf:openpdf. The versions below are 3.0.5, listed for both artifacts when this article was prepared; check the project README and Maven Central for newer releases before adopting them.
<dependencies>
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf-html</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>3.0.5</version>
</dependency>
</dependencies>
Use matching versions for the HTML module and core library. The Maven coordinates and version should be verified against the OpenPDF project and Maven Central listing, since artifact releases can change.
2. Convert an HTML string to a PDF
This is the basic flow shown by the module documentation. It writes output.pdf to the working directory:
Recommended Free Tools
import org.openpdf.pdf.ITextRenderer;
import java.io.FileOutputStream;
public class HelloWorldPdf {
public static void main(String[] args) throws Exception {
String html = "<html><body><h1>Hello, World!</h1></body></html>";
try (FileOutputStream outputStream = new FileOutputStream("output.pdf")) {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
renderer.createPDF(outputStream);
}
}
}
The order matters: set the document, lay it out, and only then create the PDF. The try-with-resources block closes the output stream whether conversion succeeds or throws. The sample follows the module README’s documented API flow.
3. Match imports to your OpenPDF version
OpenPDF 3.0 uses org.openpdf.* and removes the older com.lowagie.* namespace. If a copied example imports com.lowagie.text.* or a renderer from the old namespace, it may not compile against version 3.0. Use imports from documentation matching the dependency version; the current module example imports org.openpdf.pdf.ITextRenderer. See the OpenPDF release notes for the namespace migration.
4. Check rendering against your actual documents
openpdf-html is derived from Flying Saucer and is described by the project as modernized, with improved CSS3 compatibility. The README also says modern HTML5 support is in progress, so do not assume that a page rendered by OpenPDF will look exactly like it does in Chrome or another browser.
Rank #2
Before using the conversion in production, generate PDFs from representative templates and inspect:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
- CSS layout, including any newer HTML or CSS constructs your pages use.
- Font availability and whether the expected glyphs appear correctly.
- Images and stylesheets referenced by relative paths or external URLs.
- Page breaks, long tables, headers, footers, and content that spans multiple pages.
The module’s documented basic flow accepts an HTML string; if your document depends on external resources, test how those references resolve in your setup rather than assuming a browser’s resource-loading behavior.
5. Treat HTML and resources as untrusted input
OpenPDF’s README puts responsibility for safe input on the application developer: it says input must be trusted, sanitized, and safe, and that the library does not validate input or enforce sandboxing. Do not treat conversion as an isolation boundary.
If HTML, CSS, image paths, or other resource references can be influenced by users, validate or sanitize the content and restrict what the conversion process can access. In particular, design resource access so user-controlled references cannot reach unintended files or network resources. The appropriate controls depend on how your application supplies content and permits resource loading.
6. Understand the license terms
The project describes the core OpenPDF library as dual-licensed under MPL 2.0 or LGPL 2.1, while openpdf-html and openpdf-renderer are identified as LGPL 2.1 only. Review the license texts and your distribution context for both the core and HTML artifacts before shipping; do not assume the module inherits the core artifact’s dual-license options. The project’s README is the relevant starting point.
7. Troubleshoot common problems
| Symptom | Likely cause | What to check |
|---|---|---|
ITextRenderer or another class cannot be found |
The HTML module is missing, dependency versions do not match, or imports are from an older package namespace. | Include openpdf-html, align its version with openpdf, and use the org.openpdf imports documented for OpenPDF 3.0. |
| The code compiles, but the PDF layout differs from a browser | The renderer does not promise complete browser-equivalent HTML5 and CSS behavior. | Reduce the page to a small reproducible template, then test the specific CSS, fonts, images, and page-break behavior your output requires. |
| Images, fonts, or styles are missing | A resource reference may not resolve in the conversion environment, or the resource may not be available to the renderer. | Check each reference and resource-access configuration in the environment running the conversion; use representative assets in a test document. |
| Unexpected file or network access is possible | OpenPDF does not sandbox or validate supplied input. | Sanitize untrusted content and apply application-level restrictions to resource references and access. |
8. When to use a screenshot API instead
If you need a visual capture of a live webpage rather than a PDF generated from HTML inside a Java application, a browser screenshot service is a different tool for the job. ScreenshotNeo is a website screenshot API and MCP server; its single request can return an image or PDF. It does not replace OpenPDF for a Java document-generation pipeline.
Rank #4
Or skip the browser setup
For a live webpage PDF, ScreenshotNeo can return a PDF from one API request. See the API documentation for parameters and options.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.pdf
ScreenshotNeo accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be disabled. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and responses identify the page verdict and billing status. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for AI agents and MCP clients. The Free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000 shots.
Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.
Frequently Asked Questions
Does OpenPDF 3.0 use the com.lowagie package?
No. OpenPDF 3.0 uses the org.openpdf namespace; the older com.lowagie namespace was removed.
Best Value
Is openpdf-html a browser engine?
It is a Flying Saucer-derived HTML-to-PDF renderer, but the project describes modern HTML5 support as in progress, so browser-identical rendering is not established.
Can OpenPDF safely convert HTML submitted by users without extra controls?
No. The project says it does not validate or sandbox input; the application developer must ensure input and resource access are safe.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




