HTML to PDF in Java
This tutorial instructs Java developers on how to use the IronPDF library to convert HTML content into pixel-perfect PDF (portable document format) documents.
IronPDF is a fully-featured PDF converter and PDF processing library. IronPDF is available for both .NET and Java programming languages. This tutorial covers the library's use for converting HTML content (files, markup, etc) in Java applications. The tutorial for converting HTML to PDF in .NET applications is available here.
How to Convert HTML to PDF in Java
- Install the HTML to PDF Java PDF Library
- Convert an HTML String to a PDF Document
- Generate PDF Files from Web Pages By URL
- Convert HTML Files to PDF Files
1. Installing the IronPDF PDF Library for Java
Install with Maven
Manually install into your project
There are two ways to incorporate the IronPDF Library in a Java Project:
- Add IronPDF as a dependency in a Maven-configured Java Project
- Download the IronPDF JAR File and add it to the project classpath manually.
The following section will briefly cover both installation methods.
Option 1: Install IronPDF as a Maven Dependency
To install IronPDF in a Java project using Maven, add the following artifacts to the dependency section of the Java project's pom.xml file.
The first artifact references the latest version of the IronPDF library. The second artifact references an SL4J implementation. This dependency is required to enable IronPDF's rendering engine to generate logging messages during execution. Software engineers can substitute this dependency for other logging providers (such as Logback and Log4J); or omit it entirely if they do not need or desire logging.
mvn install command inside a terminal at the Java project's root directory to download the previously mentioned libraries.
Option 2: Install the IronPDF JAR Manually
Developers who prefer not to use Maven or any other dependency management system will need to download the IronPDF library JAR file (and the optional SL4J implementation) and manually add it to their project's class path.
Download the IronPDF JAR file directly from here (or from the Maven Repository).
How-To Guide and Code Examples
2. Converting HTML to PDF
This section showcases IronPDF's flagship HTML-to-PDF rendering abilities.
PdfDocument class is the entry point for all of IronPDF's PDF document rendering and manipulation features. The class includes a set of robust methods for converting HTML to PDF documents in support of the following three use cases: converting from HTML string/markup, converting from an HTML file, and converting from a URL. This section will briefly cover each of these use cases, with links to additional content for gathering additional information.
2.1 Import the IronPDF Package
All of IronPDF conversion and processing components are contained in the com.ironsoftware.ironpdf package.
Include the following import statement at the top of the Java source files (where IronPDF will be used) to make these components accessible to the application's source code.
// Import statement for IronPDF for Java import com.ironsoftware.ironpdf.*;
2.2. Set the License Key (optional)
IronPDF for Java is free to use. However, for free users, it will brand PDF documents with a tiled background watermark (as shown in the image below).
To use IronPDF to generate PDFs without the watermarks, the library must use a valid license key. The line of code below configures the library with a license key.
// Apply your license key License.setLicenseKey("YOUR-LICENSE-KEY");
The license key should be set before generating PDF files or customizing file content. We recommend that you call the
setLicenseKey method before all other lines of code.
2.3 Set the Log File Location (optional)
By default (and assuming that there is an SLF4J provider installed), IronPDF generates log messages to a text file called IronPdfEngine.log located in the Java application's root directory.
To specify a different name and location for the log file, use the
// Set a log path Settings.setLogPath(Paths.get("IronPdfEngine.log"));
Settings.setLogPath must be called before using any PDF conversion and manipulation methods.
2.4. Creating a PDF from HTML String
PdfDocument.renderHtmlAsPdf converts a string of HTML content into a PDF document.
The following code sample uses a single headline element to generate a new file.
PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>Hello from IronPDF!</h1>"); pdf.saveAs("htmlstring_to_pdf.pdf");
renderHtmlAsPdf method can source images, stylesheets and scripts located in folders on a computer or on a network drive. The next example produces a PDF document from HTML that references a CSS file and an image located in an
String html = "<html><head><title>Hello world!</title><style class='text/css' href='assets/style.css'></style></head><body><div class=''><h1>Hello from IronPDF!</h1><a href='https://ironpdf.com/java/'><img src='assets/logo.png' /></a></div></body></html>"; PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html); pdf.saveAs("output.pdf");
The result of the above code is shown in the image below.
A second (optional) argument to
renderHtmlAsPdf allows developers to specify a base path from which to reference web assets. This path can be to a directory on the local filesystem or even a URL path.
2.5. Creating a PDF from a URL
Developers can convert online web pages to PDF documents using IronPDF's
The next example renders the Wikipedia article into PDF content.
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://en.wikipedia.org/wiki/PDF"); pdf.saveAs("url_to_pdf.pdf");
The generated PDF file format is shown below.
Learn more about converting web pages into PDF content from the code example.
2.6. Creating a PDF from an HTML File
IronPDF can also render an HTML document stored on a local filesystem directly into its equivalent PDF format.
The next code example uses this invoice as a real-world demonstration of how well IronPDF can convert HTML files.
The HTML markup for the invoice is reproduced here for convenience:
<html> <head> <meta charset="utf-8"> <title>Invoice</title> <link rel="stylesheet" href="style.css"> <link rel="license" href="https://www.opensource.org/licenses/mit-license/"> <script src="script.js"></script> </head> <body> <header> <h1>Invoice</h1> <address contenteditable> <p>Jonathan Neal</p> <p>101 E. Chapman Ave<br>Orange, CA 92866</p> <p>(800) 555-1234</p> </address> <span><img alt="" src="http://www.jonathantneal.com/examples/invoice/logo.png"><input type="file" accept="image/*"></span> </header> <article> <h1>Recipient</h1> <address contenteditable> <p>Some Company<br>c/o Some Guy</p> </address> <table class="meta"> <tr> <th><span contenteditable>Invoice #</span></th> <td><span contenteditable>101138</span></td> </tr> <tr> <th><span contenteditable>Date</span></th> <td><span contenteditable>January 1, 2012</span></td> </tr> <tr> <th><span contenteditable>Amount Due</span></th> <td><span id="prefix" contenteditable>$</span><span>600.00</span></td> </tr> </table> <table class="inventory"> <thead> <tr> <th><span contenteditable>Item</span></th> <th><span contenteditable>Description</span></th> <th><span contenteditable>Rate</span></th> <th><span contenteditable>Quantity</span></th> <th><span contenteditable>Price</span></th> </tr> </thead> <tbody> <tr> <td><a class="cut">-</a><span contenteditable>Front End Consultation</span></td> <td><span contenteditable>Experience Review</span></td> <td><span data-prefix>$</span><span contenteditable>150.00</span></td> <td><span contenteditable>4</span></td> <td><span data-prefix>$</span><span>600.00</span></td> </tr> </tbody> </table> <a class="add">+</a> <table class="balance"> <tr> <th><span contenteditable>Total</span></th> <td><span data-prefix>$</span><span>600.00</span></td> </tr> <tr> <th><span contenteditable>Amount Paid</span></th> <td><span data-prefix>$</span><span contenteditable>0.00</span></td> </tr> <tr> <th><span contenteditable>Balance Due</span></th> <td><span data-prefix>$</span><span>600.00</span></td> </tr> </table> </article> <aside> <h1><span contenteditable>Additional Notes</span></h1> <div contenteditable> <p>A finance charge of 1.5% will be made on unpaid balances after 30 days.</p> </div> </aside> </body> </html>
PdfDocument pdf = PdfDocument.renderHtmlFileAsPdf("C:/invoices/TestInvoice1.html"); pdf.saveAs("htmlfile_to_pdf.pdf");
As with the HTML string to PDF conversion examples, IronPDF correctly resolves any relative URLs in an HTML document to their correct paths on the file system. As a result, the PDF File that this example produces is able to perfectly capture the visual influences that any referenced stylesheets and scripts would normally have on a web page.
3. Further Reading
We have only scratched the surface of IronPDF's HTML to PDF rendering abilities.
Further your understanding of how to use the HTML to PDF converter for Java development using the curated code samples featured in our Code Examples section.
- Read this code example to learn how to customize the appearance of a PDF document during the conversion process.
- Generate PDF files with custom headers and footers, margin sizes, page dimensions, watermarks, and much more.
- Extract PDF content (text and images) from documents, optimize file sizes, and print PDFs programmatically
Study the IronPDF Java API Reference page on the
PdfDocument class for even greater control over rendered HTML to PDF.
Tutorial Quick Access
Download this Tutorial as Java Source Code
The full HTML to PDF Java Source Code for this tutorial is available to download for free as a zipped IntelliJ project.Download
Explore this Tutorial on GitHub
The source code for this project is available on GitHub.
Use this code as an easy way to get up and running in just a few minutes. The project is saved as an IntellJ IDEA project, but can be imported into other popular Java IDEs.Java HTML to PDF