Java Convert HTML to PDF
Tags: flying-saucer-pdf-openpdf Flying Saucer PDF HTML to PDF
In this Java tutorial we learn how to convert a html file into a pdf file in Java application using the Flying Saucer PDF Rendering library.
Table of contents
- Add Flying Saucer PDF Rendering (OpenPDF) library to Java project
- Implement PdfConverter Java Class
- How to Convert HTML File to PDF File
Add Flying Saucer PDF Rendering (OpenPDF) library to Java project
First step, we need to add the Flying Saucer PDF Rendering (OpenPDF) dependency to the project.
To use the Flying Saucer PDF Rendering library in the Gradle build project, add the following Flying Saucer PDF Rendering (OpenPDF) dependency into the build.gradle file.
implementation group: 'org.xhtmlrenderer', name: 'flying-saucer-pdf-openpdf', version: '9.1.22'
To use the Flying Saucer PDF Rendering library in the Maven build project, add the following Flying Saucer PDF Rendering (OpenPDF) dependency into the pom.xml file.
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-openpdf</artifactId>
<version>9.1.22</version>
</dependency>
To have more information about the Flying Saucer PDF Rendering Java library you can visit the project repository at github.com/flyingsaucerproject/flyingsaucer
Implement PdfConverter Java Class
At this step we implement a new Java class named PdfConverter, and implement method convertHtmlToPdf() which get the input HTML file path, and convert to PDF file then write it to the PDF file path.
PdfConverter.java
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class PdfConverter {
public void convertHtmlToPdf(String htmlFilePath, String pdfFilePath) {
try {
FileOutputStream fileOutputStream = new FileOutputStream(pdfFilePath);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new File(htmlFilePath));
renderer.layout();
renderer.createPDF(fileOutputStream, false);
renderer.finishPDF();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
How to Convert HTML File to PDF File
At this final step, we learn how to use the PdfConverter class from the previous step to generate a PDF file from a given HTML file.
For example, we have a receipt.html file at D:\SimpleSolution\Data\receipt.html
The receipt.html file content contains HTML code as below.
receipt.html
<!DOCTYPE HTML>
<html>
<head>
<style>
h1 {
color: #25a7e7;
text-align: center;
}
.receipt-header {
width: 100%;
}
.receipt {
width: 100%;
}
.receipt, .receipt th, .receipt td {
border: 1px solid #25a7e7;
border-collapse: collapse;
}
.receipt th {
background-color: #25a7e7;
color: white;
}
.total {
text-align: right;
}
</style>
</head>
<body>
<h1>Receipt</h1>
<div>
<table class="receipt-header">
<tr>
<td>
<table>
<tr>
<th>Bill To:</th>
</tr>
<tr>
<td>Company Name: Simple Solution</td>
</tr>
<tr>
<td>Address: 123 Sample Street</td>
</tr>
<tr>
<td>Email: info@simplesolution.dev</td>
</tr>
<tr>
<td>Phone: 123 456 789</td>
</tr>
</table>
</td>
<td align="right">
<img width="140" src="https://simplesolution.dev/images/Logo_S_v1.png" />
<br />
Simple Solution
</td>
</tr>
</table>
</div>
<br />
<table class="receipt">
<tr>
<th>Item #</th>
<th>Description</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Total</th>
</tr>
<tr>
<td>1</td>
<td>Item 1 Description</td>
<td>5</td>
<td>$100</td>
<td>$500</td>
</tr>
<tr>
<td>2</td>
<td>Item 2 Description</td>
<td>10</td>
<td>$20</td>
<td>$200</td>
</tr>
<tr>
<td>3</td>
<td>Item 3 Description</td>
<td>2</td>
<td>$50</td>
<td>$100</td>
</tr>
<tr>
<td class="total" colspan="4"><b>Total</b></td>
<td><b>$800</b></td>
</tr>
</table>
</body>
</html>
In the following Java program, we convert the HTML file at D:\SimpleSolution\Data\receipt.html to a PDF file named receipt.pdf in the same folder.
HtmlToPdfExample1.java
public class HtmlToPdfExample1 {
public static void main(String... args) {
String htmlFilePath = "D:\\SimpleSolution\\Data\\receipt.html";
String pdfFilePath = "D:\\SimpleSolution\\Data\\receipt.pdf";
PdfConverter pdfConverter = new PdfConverter();
pdfConverter.convertHtmlToPdf(htmlFilePath, pdfFilePath);
}
}
Run the above Java application, we have the PDF file be generated as below.
Open the receipt.pdf file with a PDF reader we have the file content as following screenshot.
Happy Coding 😊
Related Articles
Spring Boot Generate PDF File from HTML Template
Spring Boot Web Download PDF File from HTML Template
Java Export PDF File from HTML Template