How to Convert HTML to PDF in Java
- Install Java library to convert HTML to PDF
- Utilize renderHtmlAsPdf method to convert HTML string
- Use renderUrlAsPdf method to generate PDF from URL
- Access renderHtmlFileAsPdf method to convert HTML file in Java
- Save the generated PDF as a new document
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.
- Making Reports
- Converting Invoices into PDF
- Making a Pdf converter
- Creating Fillable PDF Forms
- 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:
- Generating PDFs from HTML, URL, JavaScript, CSS, and many image formats
- Adding headers/footers, signatures, attachments, passwords, and security
- Performance optimization: Full Multithreading and Async support
- 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.
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.
<dependencies>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>2022.11.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>
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("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the PdfDocument to a file
try {
myPdf.saveAs(Paths.get("html_saved.pdf"));
} catch (IOException e) {
throw new RuntimeException(e);
}
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.
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) {
throw new RuntimeException(e);
}
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.
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);
}
}
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.
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.
Top comments (3)
I did my own free implementation plug & play
github.com/davidfesteban/DavidPDF
To bad this isn't open source and cost money.
Yeah, But It provides a free trial and costs very little money.