HTML To PDF JAVA Conversion (Without Losing Formatting)

In this tutorial, we will learn to convert Html files into pdf documents in our Java Applications. PDf format is a widely used document format when it comes to working with files. Java is a widely used language for developing enterprise-level applications. Therefore It is necessary for Java developers to learn how to generate HTML to pdf in java.

There are multiple use cases where we need to convert HTML into PDFs.

  1. Making Reports
  2. Converting Invoices into PDF
  3. Making a Pdf converter
  4. Creating Fillable PDF Forms
  5. Converting HTML files into PDF files . and many more.

We need a third-party HTML to pdf java library for generating pdf documents. There are multiple libraries but either they are paid, slow, or do not provide enough functionalities. We need an efficient library that is free for development, easy to use, and work with all application type which provides all necessary classes and functions to work with pdf files.

I will use the IronPDF library in this tutorial. This library is ideal as it prioritizes accuracy, ease of use, and speed. It is free for development, and work with all java application type and framework including spring boot application. It also works with Kotlin and Scala. So, if you are developing mobile, web, or desktop applications, It will work with all.

Let's first have a look at the IronPDF Library before going any further.

IronPDF:

IronPdf is a library developed and maintained by Iron Software that helps Software Engineers to create, edit and extract PDF content in projects in Java.
IronPDF is supported by Java, Kotli, and Scala. It is developer-friendly and provides a variety of features in a single library.

The following are the features:

  1. Generating PDFs from HTML, URL, JavaScript, CSS, and many image formats
  2. Adding headers/footers, signatures, attachments, passwords, and security
  3. Performance optimization: Full Multithreading and Async support
  4. And many more! Visit their website to see all the code examples and a full list of their 50+ features*

This tutorial is designed in such a way that even a new programmer can easily understand and implement it. This tutorial assumed that you may have a basic understanding of Java.

Let's create a project to convert HTML to pdf in java.

Create a New Project:

I am using IntelliJ IDE for java development. you can use any as per your preference.

So, the Step for creating a new project may differ from IDE to IDE.

Image description

Open, IntelliJ and Click on File> New > Project from Top Menu Bar. The following window will appear.

Name your Project, Select Location, Choose Language, JDK, and click on Create a Project.

New Project will be created.

Install IronPDF:

Now, we need to install the IronPDF java library to use in our project for converting HTML files into pdf documents.

Open Pom.XML file and add the following XML below the properties tag.

  com.ironsoftware ironpdf 2022.11.0  org.slf4j slf4j-simple 2.0.3   
Enter fullscreen mode

Exit fullscreen mode

Now, Open the Terminal and run the following command.
mvn install
This command will download and install the IronPDF.

Now, Open Main.Java file to actually write the code that will create pdf file from HTML Content.

Convert HTML to pdf document:

We first need to reference the IronPDF library to our Main.Java file. Write the following code snippet at the top of the file.
import com.ironsoftware.ironpdf.*;
Write the following code inside Main Function.

PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(" 

~Hello World~

Made with IronPDF!"); // Save the PdfDocument to a file try < myPdf.saveAs(Paths.get("html_saved.pdf")); >catch (IOException e)
Enter fullscreen mode

Exit fullscreen mode

renderHtmlAsPdf() function provided by PdfDocument class takes HTML string as an argument and convert HTML content to pdf file format.

saveAs() function will save the newly generated pdf file to the provided path in the argument.

Output:

Following is the output generated by the above code.

Image description

Convert URL to PDF document.

We can also generate pdf files from the web URL.

The following code demonstrates the example of generating the pdf document from the web URL.

 PdfDocument myPdf = PdfDocument.renderUrlAsPdf ("https://en.wikipedia.org/wiki/PDF"); // Save the PdfDocument to a file try < myPdf.saveAs(Paths.get("html_saved.pdf")); >catch (IOException e)
Enter fullscreen mode

Exit fullscreen mode

renderUrlAsPdf() function provided by PdfDocument class takes the URL as an argument, and converges that URL into an HTML document.

Output:

Following is the output generated by the above-mentioned code, we can see that the pdf file generated by our code is 100% accurate.

Image description

Convert HTML file to PDF document:

The following code demonstrate the example of HTML file conversion into a pdf document. IronPDF really provides a very easy way to generate pdf from HTML files.

public static void main(String[] args) throws IOException < PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("sample.html"); // Save the PdfDocument to a file try < myPdf.saveAs(Paths.get("html_saved.pdf")); >catch (IOException e) < throw new RuntimeException(e); >> 
Enter fullscreen mode

Exit fullscreen mode

renderHtmlFileAsPdf() takes the path of the Html file as an argument, and converts the given file into a new pdf document.

Output:

Following is the output generated by the above code sample. Image and all styling including the font family generated in the pdf document is the exactly same as provided HTML file/web page.

Image description

Summary:

In this tutorial, we have learned a very easy way to convert HTML into a PDF document. We have demonstrated examples with code. We have converted HTML text to PDF, and we have also generated a PDF from an HTML File. Moreover, we have also learned to generate pdf files from URLs.

There are multiple useful and interesting features provided by IronPDF, but it is impossible to cover them all here. For more details, please click here.

I hope this article was understandable and easy to follow. For any queries, feel free to ask in the comment section.