HTML2PDF Java (Code Example Tutorial)

PDF documents are an important file format when it comes to sending and receiving data online. PDF stands for "Portable Document Format" and allows users to preserve formatting and data in a permanent form. To work with PDF documents, a PDF viewer and editor is important.

Sometimes there is a need to create different file formats in our application programs or convert them to PDF. Since Java is a popular language used by many programmers, these developers may need to work with PDF files in their Java applications. It is useful to create reports, invoices or bills as needed. In Java, it can be a difficult task to work with PDF files. That's why we'll use IronPDF - a Java library for creating PDF documents programmatically.

IronPDF - A Java Library

Iron Software engineers have now developed IronPDF for the Java Language which helps Java developers to create, edit and manipulate PDF documents. IronPDF allows you to work with all aspects of PDF files. It offers developers a wide range of features to create and customize PDFs. It also helps to control the layout and formatting of the PDF document.

IronPDF for Java builds on the capabilities of the .NET framework, making it a versatile tool for working with PDFs. Apart from creating and editing PDFs, it is especially helpful in converting HTML files to PDF and from other file formats.

Steps to Convert HTML to PDF in Java


Prerequisites

To create a PDF Java conversion application, you need to download and install the following prerequisites:

  1. Java Development Kit (JDK): the latest version of the JDK must be installed on your computer for the PDF conversion application to compile and run. The JDK can be downloaded from the Oracle website.
  2. Maven: Maven must be installed as it is a build automation tool used mainly for Java projects. Maven can be downloaded from the Apache Maven website.
  3. IronPDF Java Library: Now you need to add the latest version of IronPDF Java Library as a dependency to your PDF conversion application. Add the following IronPDF dependency to the pom.xml file of your project:

    <dependency>
       <groupId>com.ironsoftware</groupId>
       <artifactId>com.ironsoftware</artifactId>
       <version>2024.3.1</version>
    </dependency>
    XML
  4. You will also need to add the Slf4j dependency in the pom.xml file.

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.5</version>
    </dependency>
    XML

Once all the prerequisites are downloaded and installed, the project can now be used to convert HTML files to PDF documents in Java applications.

Adding Necessary Imports and License Key

First, you need to add the IronPDF import to your main Java file where you want to run the code for the PDF converter. You need to import all the required classes, and this can be done with this single line of code:

import com.ironsoftware.ironpdf.*;
JAVA

Next, in the main method, enter the license key you obtained at the time of purchase or trial key using the IronPDF setLicenseKey.

License.setLicenseKey("Your license key");
JAVA

Convert HTML String to PDF

IronPDF can convert HTML content to PDF with simply one line of code.

PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~HTML2PDF in JAVA~ </h1> Made with IronPDF!");
myPdf.saveAs(Paths.get("html_saved.pdf"));
JAVA

In the above code, the renderHtmlAsPdf method is used to pass the HTML string that will be converted to a PDF document. Then the saveAs method is called to generate the PDF.

This creates a PDF document named "html_saved.pdf" which contains the HTML content passed as a string.

Convert HTML File to PDF

With the following code you can convert the HTML file into a PDF document:

// Render the HTML as a PDF. Stored in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");

// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
JAVA

Here, the renderHtmlFileAsPdf method is used to convert an HTML file into a PDF file. CSS and JavaScript can also be rendered along with the HTML file, preserving the layout and formatting of the HTML file.

Convert HTML URL to PDF

Sometimes you want to create PDF files directly from a web page. For this purpose, the IronPDF library provides the method renderUrlAsPdf to convert HTML from a URL to PDF. Here we pass the URL as an argument to the method.

// Render the HTML as a PDF. Stored in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");

// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("url.pdf"));
JAVA

You can also specify the formatting of the PDF file, such as page size, page orientation, margin size, layout and many other properties simply by using the ChromePdfRenderOptions and passing the layout options in the renderUrlAsPdf method as the second argument.

After running the project with any of the above code samples, PDF files will be generated for all HTML content. Below you can see the sample output of "Convert HTML to PDF".

HTML2PDFJava - Figure 1: URL to PDF Output

The result of converting the web page to PDF by URL

You can find further code examples on IronPDF's website.

HTML2PDFJava - Figure 2: IronPDF Website

The IronPDF Website

IronPDF renders all images and text without losing any formatting. Buttons are clickable, and text boxes are editable in the PDF file.

Summary

In this article, we have looked at PDF creation using the "Convert HTML to PDF" feature of the IronPDF Java API.

IronPDF for Java is free to use but for deployment purposes, it has a commercial license which starts from only $749. You can also access the free trial of the full version of IronPDF to test its functionality in production mode.