Sitemap

Convert HTML to PDF in Java (Code Example Tutorial)

5 min readFeb 23, 2023

--

Today I want to tell you about an experience I have been through, few weeks ago I was looking for a way to convert HTML files and websites into PDF (Portable document format) files to write a report on differences in interfaces. For that purpose, I was scraping the internet looking through stack overflow pages until I came across a lifesaving PDF library developed to be used as Java library named as IronPDF for Java. Using this library, I was able to complete my report in just a few hours. So, I thought let’s share this with the world. before we start about how to generate pdf files using IronPDF java library let’s talk about Java first.

Java has long been one of the most popular programming languages for developing web applications. With the rise of PDF documents as a preferred format for sharing and storing information, it’s become increasingly important for developers to be able to convert HTML file content to PDF file. Thankfully, there are a number of libraries available that make this task much easier. One such library is IronPDF for Java.

What is IronPDF?

IronPDF is a powerful Java library that allows developers to create, read, and manipulate PDF documents in their applications. It’s compatible with a wide range of platforms and programming languages, including Java. With IronPDF, developers can quickly and easily generate high-quality PDF documents from HTML, CSS, and other web content.

Get Buttwaleed’s stories in your inbox

Join Medium for free to get updates from this writer.

Before getting started with IronPDF library for converting HTML to PDF format. Lets discuss prerequisites.

prerequisites

You will need to make sure you have the following prerequisites in place:

  1. Java Development Kit (JDK): The JDK must be installed on your system, and its path must be set in the environment variables. If you have not installed the JDK yet, you can follow the instructions to download and install it from the Oracle website.
  2. Java Integrated Development Environment (IDE): You will need a Java IDE such as Eclipse, IntelliJ IDEA, or NetBeans to write and execute your Java code.
  3. Apache Maven: Maven should be installed on your system and integrated with your IDE. This will allow you to manage dependencies and build your Java project more easily.

Once you have fulfilled these prerequisites, you can proceed with creating a new maven project and install IronPDF Java library in it.

Create a new Maven Project

Today I will demonstrate this conversion process in IntelliJ IDE.

  • Go to File -> New -> Project
  • Choose Maven on the left-hand side and click Next.
Press enter or click to view image in full size
  • Choose an appropriate project name and location.
Press enter or click to view image in full size
  • Click on Next and then Finish to create the project.

Install IronPDF Dependencies

Once you have set up your Maven project, you’ll need to add the IronPDF library to your project. To do this, you’ll need to add the following dependency to your pom.xml file:

<dependencies>
<!--Adds IronPDF Java. Use the latest version in the version tag.-->
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>20xx.xx.xxxx</version>
</dependency>
<!--Adds the slf4j logger which IronPDF Java uses.-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>

Once you add these dependencies an Icon will appear in top right corner on the screen click that icon and it will start downloading the following dependencies. It will only take couple of minutes to download and install these dependencies.

Press enter or click to view image in full size

Convert HTML file to PDF Document

In this section we will discuss how you can convert HTML to PDF file format using IronPDF for Java. In below source code snippet we will take a HTML file system from local storage and using IronPDF PDF converter we will be generating PDF files.

Input HTML file screenshot

Press enter or click to view image in full size
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.Settings;
import java.io.IOException;
import java.nio.file.Paths;
public class main {
public static void main(String[] args) throws IOException {
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Stored in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("index.html");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
}
}

Output PDF file screenshot

Press enter or click to view image in full size

As you can see from above code sample and its output IronPDF did an exceptional job at creating live like carbon copy of input HTML file with Links and annotations Intact.

PDF Conversion from URLs

While working with IronPDF I found out that you can create PDF file directly from websites. To do this all you need is the URL of the website and give it to the code as input of HTML source. In below source code example, we will see that in action.

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.Settings;
import java.io.IOException;
import java.nio.file.Paths;
public class main {
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Stored in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://www.facebook.com/");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("url.pdf"));
}
}

Output PDF document

This below output PDF file contains all the links that are associated with the real interface.

Press enter or click to view image in full size

This below output PDF file contains all the links that are associated with the real interface.

Conclusion

In this article, we’ve explored how to use IronPDF for Java to convert HTML content to PDF. We’ve seen how easy it is to get started with IronPDF, and how it provides a range of advanced features that make PDF generation a breeze. If you’re a Java developer looking to generate PDF documents from HTML content, IronPDF is definitely a library to consider. It’s powerful, easy-to-use, and provides a range of features that can help you create high-quality PDF documents quickly and efficiently.

Whether you’re building a web application, a desktop application, or a mobile app, IronPDF can be integrated with ease. It’s compatible with a wide range of platforms and programming languages and provides a range of APIs that make PDF generation a breeze.

In summary, IronPDF for Java is an excellent choice for any developer looking to convert HTML content to PDF in their Java applications. With its powerful features and easy-to-use API, IronPDF makes PDF generation a breeze. If you’re interested in giving it a try, be sure to check out the IronPDF website and documentation and for more information on HTML to PDF conversion please refer to this link.

--

--