Conversion (24)

Conversion from HTML to PDF in Java is a critical task for developers aiming to generate dynamic documents, reports, or invoices. Whether you're building a web application or automating data exports, this process ensures consistent formatting across platforms.
This article demonstrates how to convert HTML to PDF in Java using the powerful Spire.PDF for Java and QT WebEngine, ensuring high-fidelity PDF rendering from live webpages, raw HTML strings or HTML files.
Table of Contents:
- Why Convert HTML to PDF in Java?
- Installation and Setup
- Convert URL to PDF in Java
- Convert HTML String to PDF in Java
- Convert HTML Files to PDF in Java
- Frequently Asked Questions
- Conclusion
Why Convert HTML to PDF in Java?
The benefits of using Jave for HTML to PDF conversion lie in:
- Preserve Formatting: Maintain exact HTML/CSS styling in printable documents
- Cross-Platform Compatibility: Ensure consistent document rendering across devices
- Generate Professional Documents: Generate dynamic PDFs from HTML web templates
- Archive Web Content: Create permanent snapshots of web pages
- Automate Workflows: Integrate conversion into Java applications
Installation and Setup
Spire.PDF for Java + Qt Web Engine can act as a professional HTML to PDF converter for Java. To get started, follow the instructions below:
Install Spire.PDF for Java
To install Spire.PDF, you can use Maven or manually include the JAR file.
1. Maven Configuration
Add the following dependency to your pom.xml:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.10.3</version>
</dependency>
</dependencies>
2. Manual JAR Inclusion
Download the latest version of Spire.PDF for Java from the official website and add the JAR file to your project.
Download Qt WebEngine Plugin
Qt WebEngine is a web browser engine module that enables developers to embed web content (HTML, CSS, JavaScript) into their cross-platform applications.
This scenario uses an open-source version of Qt WebEngine (under LGPLv3/GPL) for rendering HTML content. You can download the appropriate QT plugin version for your OS:
After downloading, unzip the package to a local path to get the "plugins" folder (e.g., F:\ Plugin\plugins-windows-x64\plugins).

Important Notes:
- For Linux/Mac environments:
Directly copy the plugin package to system and unzip it. Ensure read permissions for the plugin folder.
- Headless Server Requirement:
The conversion method requires GUI support. For servers without GUI, you need install the xvfb tool first, and then use the xvfb-run command to run your application.
#1. install xvfb
Centos:yum install Xvfb
Ubuntu:apt-get install Xvfb
#2. Run application
xvfb-run java -jar HTMLToPDF.jar
Convert URL to PDF in Java
The following Java code converts an HTML webpage (URL) to PDF. Perfect for live web content capture.
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.htmlconverter.qt.HtmlConverter;
import com.spire.pdf.htmlconverter.qt.Size;
public class ConvertUrlToPdf {
public static void main(String[] args) {
// Specify the url path
String url = "https://www.google.com/";
// Specify the output file path
String fileName = "output/UrlToPdf.pdf";
// Specify the plugin path
String pluginPath = "F:\\Plugin\\plugins-windows-x64\\plugins";
// Set the plugin path
HtmlConverter.setPluginPath(pluginPath);
// Convert URL to PDF
HtmlConverter.convert(url, fileName, true, 1000000, new Size(1200f, 1000f), new PdfMargins(0));
}
}
Critical: If plugins are missing or path is incorrect, conversion fails.
Parameters Explained
In the above code, the core conversion method is HtmlConverter.convert(). It accepts 6 parameters which are explained below:
| Parameter | Purpose |
|---|---|
| String url | Specify the web address to convert. |
| String fileName | Specify the output PDF file path. |
| boolean enableJavaScript | Allows dynamic content (e.g., AJAX, animations). Set false to disable. |
| int timeout | Max page load time. Adjust for slow sites. |
| Size pageSize | Set the output PDF page size. |
| PdfMargins margins | Set the output PDF margins. |
Result:

Convert HTML Strings to PDF in Java
This Java code demonstrates how to convert an HTML string into a PDF file. Ideal for generating PDFs from dynamic HTML content (e.g., user-generated).
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.htmlconverter.LoadHtmlType;
import com.spire.pdf.htmlconverter.qt.HtmlConverter;
import com.spire.pdf.htmlconverter.qt.Size;
import java.io.IOException;
public class ConvertHtmlStringToPdf {
public static void main(String[] args) throws IOException {
// Specify the Html string
String htmlString = "<html>" +
"<style> h1 {color: #FF5733;font-size: 24px;margin-top: 50px;} </style>" +
"<body>" +
"<h1>Hello, PDF!</h1>" +
"<p>This is a simple example of converting HTML String to PDF.</p>" +
"<ul>" +
"<li>First Item</li>" +
"<li>Second Item</li>" +
"<li>Third Item</li>" +
"</ul>" +
"</body></html>";
// Specify the output file path
String outputFile = "output/HtmlStringToPdf.pdf";
// Specify the plugin path
String pluginPath = "F:\\Plugin\\plugins-windows-x64\\plugins";
// Set the plugin path
HtmlConverter.setPluginPath(pluginPath);
// Convert the HTML string to PDF
HtmlConverter.convert(htmlString, outputFile, true, 100000, new Size(700, 900), new PdfMargins(0), LoadHtmlType.Source_Code);
}
}
In this code:
- Specifies an HTML string containing inline CSS styling (e.g., red h1 text) and simple content (heading, paragraph, list).
- Uses HtmlConverter.convert() to convert HTML string to PDF with the following parameters:
- HTML content (string)
- Output file path
- true to enable JavaScript execution.
- Timeout (100,000 milliseconds)
- Page size (700x900)
- Zero margins
- LoadHtmlType.Source_Code to indicate the input is raw HTML.
Output:

Convert HTML Files to PDF in Java
This example shows how to convert HTML files to PDF documents programmatically. Suitable for static document archival and design-heavy publications.
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.htmlconverter.LoadHtmlType;
import com.spire.pdf.htmlconverter.qt.HtmlConverter;
import com.spire.pdf.htmlconverter.qt.Size;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ConvertHtmlToPdf {
public static void main(String[] args) throws IOException {
// Invoke the custom method HtmlToString() to convert HTML file to string
String htmlString = HtmlToString("sample1.html");
// Specify the output file path
String outputFile = "output/HtmlFileToPdf.pdf";
// Specify the plugin path
String pluginPath = "F:\\Plugin\\plugins-windows-x64\\plugins";
// Set the plugin path
HtmlConverter.setPluginPath(pluginPath);
// Convert the HTML string to PDF
HtmlConverter.convert(htmlString, outputFile, true, 100000, new Size(700, 900), new PdfMargins(0), LoadHtmlType.Source_Code);
}
// Convert a HTML file to string
public static String HtmlToString(String filePath) throws IOException {
String path = filePath;
File file = new File(path);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuilder = new StringBuilder();
String temp = "";
while ((temp = bufferedReader.readLine()) != null) {
stringBuilder.append(temp + "\n");
}
bufferedReader.close();
String str = stringBuilder.toString();
return str;
}
}
Note: Only inline CSS style and internal CSS style can be rendered correctly on PDF. If you have an external CSS style sheet, please convert it to inline or internal CSS style.
Core Workflow Explained
-
Read HTML File: Uses the custom method HtmlToString() to convert an HTML file into a string.
- Opens the HTML file using FileReader and BufferedReader.
- Reads line-by-line, appending content to a StringBuilder.
- Returns the HTML content as a single string.
-
Configure Output & Plugins: Specifies output file path and sets the Qt plugin path.
-
Convert HTML to PDF: Uses the same HtmlConverter.convert() method as in above example.
Result:

Frequently Asked Questions (FAQs)
Q1. Why do I need Qt plugins for HTML-to-PDF conversion?
Qt WebEngine provides a Chromium-based rendering engine required to parse modern HTML/CSS/JavaScript. Spire.PDF uses Qt under the hood for accurate web page rendering.
Q2: How can I avoid watermarks in the output PDF?
Request a 30-day trial license to remove the watermarks and get rid of the function limitations.
Q3. How do I optimize conversion speed for large documents?
- Disable JavaScript if unnecessary
- Reduce timeout
- Set smaller page size
Conclusion
Using Spire.PDF for Java with the QT plugin streamlines HTML to PDF conversion in Java with minimal code. Whether sourcing from URLs, strings, or local files, this article provides detailed instructions and practical code examples to help enhance your document automation workflows.
Final Tip: Explore online documentation for advanced Java PDF processing features like digital signatures and PDF/A compliance.
Java convert PDF to HTML with embedded SVG/Image and save HTML to stream
2020-04-15 08:12:08 Written by KoohjiSpire.PDF supports to convert PDF to HTML and save the resulted HTML file to stream by calling the method PdfDocument.saveToStream(). When converting PDF to HTML, it also supports to set the convert options with embedded SVG/Image on the resulted HTML file. This article will demonstrate how to convert the PDF pages to HTML with embedded SVG and embedded image.
import com.spire.pdf.*;
import java.io.*;
public class PDFtoHTML {
public static void main(String[] args) throws FileNotFoundException {
String inputFile = "Sample.pdf";
String outputFile = "output/toHTML_out.html";
//Load the sample document file
PdfDocument pdf = new PdfDocument();
pdf .loadFromFile(inputFile);
//Set the bool useEmbeddedSvg and useEmbeddedImg as true
pdf .getConvertOptions().setPdfToHtmlOptions(true,true);
//Save to stream
File outFile = new File(outputFile);
OutputStream outputStream = new FileOutputStream(outFile);
pdf.saveToStream(outputStream, FileFormat.HTML);
pdf.close();
}
}
Converting images to PDF is beneficial for many reasons. For one reason, it allows you to convert images into a format that is more readable and easier to share. For another reason, it dramatically reduces the size of the file while preserving the quality of images. In this article, you will learn how to convert images to PDF in Java using Spire.PDF for Java.
There is no straightforward method provided by Spire.PDF to convert images to PDF. You could, however, create a new PDF document and draw images at the specified locations. Depending on whether the page size of the generated PDF matches the image, this topic can be divided into two subtopics.
Install Spire.PDF for Java
First, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.10.3</version>
</dependency>
</dependencies>
Additionally, the imgscalr library is used in the first code example to resize images. It is not necessary to install it if you do not need to adjust the image’s size.
Add an Image to PDF at a Specified Location
The following are the steps to add an image to PDF at a specified location using Spire.PDF for Java.
- Create a PdfDocument object.
- Set the page margins using PdfDocument.getPageSettings().setMargins() method.
- Add a page using PdfDocument.getPages().add() method
- Load an image using ImageIO.read() method, and get the image width and height.
- If the image width is larger than the page (the content area) width, resize the image to make it to fit to the page width using the imgscalr library.
- Create a PdfImage object based on the scaled image or the original image.
- Draw the PdfImage object on the first page at (0, 0) using PdfPageBase.getCanvas().drawImage() method.
- Save the document to a PDF file using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfImage;
import org.imgscalr.Scalr;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
public class AddImageToPdf {
public static void main(String[] args) throws IOException {
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Set the margins
doc.getPageSettings().setMargins(20);
//Add a page
PdfPageBase page = doc.getPages().add();
//Load an image
BufferedImage image = ImageIO.read(new FileInputStream("C:\\Users\\Administrator\\Desktop\\announcement.jpg"));
//Get the image width and height
int width = image.getWidth();
int height = image.getHeight();
//Declare a PdfImage variable
PdfImage pdfImage;
//If the image width is larger than page width
if (width > page.getCanvas().getClientSize().getWidth())
{
//Resize the image to make it to fit to the page width
int widthFitRate = width / (int)page.getCanvas().getClientSize().getWidth();
int targetWidth = width / widthFitRate;
int targetHeight = height / widthFitRate;
BufferedImage scaledImage = Scalr.resize(image,Scalr.Method.QUALITY,targetWidth,targetHeight);
//Load the scaled image to the PdfImage object
pdfImage = PdfImage.fromImage(scaledImage);
} else
{
//Load the original image to the PdfImage object
pdfImage = PdfImage.fromImage(image);
}
//Draw image at (0, 0)
page.getCanvas().drawImage(pdfImage, 0, 0, pdfImage.getWidth(), pdfImage.getHeight());
//Save to file
doc.saveToFile("output/AddImage.pdf");
}
}

Convert an Image to PDF with the Same Width and Height
The following are the steps to convert an image to a PDF with the same page size as the image using Spire.PDF for Java.
- Create a PdfDocument object.
- Set the page margins to zero using PdfDocument.getPageSettings().setMargins() method.
- Load an image using ImageIO.read() method, and get the image width and height.
- Add a page to PDF based on the size of the image using PdfDocument.getPages().add() method.
- Create a PdfImage object based on the image.
- Draw the PdfImage object on the first page from the coordinate (0, 0) using PdfPageBase.getCanvas().drawImage() method.
- Save the document to a PDF file using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfImage;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
public class ConvertImageToPdfWithSameSize {
public static void main(String[] args) throws IOException {
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Set the margins to 0
doc.getPageSettings().setMargins(0);
//Load an image
BufferedImage image = ImageIO.read(new FileInputStream("C:\\Users\\Administrator\\Desktop\\announcement.jpg"));
//Get the image width and height
int width = image.getWidth();
int height = image.getHeight();
//Add a page of the same size as the image
PdfPageBase page = doc.getPages().add(new Dimension(width, height));
//Create a PdfImage object based on the image
PdfImage pdfImage = PdfImage.fromImage(image);
//Draw image at (0, 0) of the page
page.getCanvas().drawImage(pdfImage, 0, 0, pdfImage.getWidth(), pdfImage.getHeight());
//Save to file
doc.saveToFile("output/ConvertPdfWithSameSize.pdf");
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
In the article of convert PDF to SVG by Spire.PDF, each page on the PDF file has been saved as a single SVG file. For example, if the PDF contains 10 pages, we will get 10 SVG files separately. From version 2.7.6, Spire.PDF for Java supports to convert a multipage PDF to one single SVG file in Java.
import com.spire.pdf.*;
public class PDFtoSVG {
public static void main(String[] args) throws Exception {
String inputPath = "Sample.pdf";
PdfDocument document = new PdfDocument();
document.loadFromFile(inputPath);
document.getConvertOptions().setOutputToOneSvg(true);
document.saveToFile("output.svg", FileFormat.SVG);
document.close();
}
}
Effective screenshot of the resulted one SVG file:

PDF (Portable Document Format) and XPS (XML Paper Specification) are two commonly used document formats for sharing and printing documents. While PDF is widely known and supported, XPS is a Microsoft-developed format that has gained popularity due to its superior graphics rendering capabilities. In this article, we will demonstrate how to use Spire.PDF for Java to convert PDF to XPS and XPS to PDF in high quality.
Install Spire.PDF for Java
First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.10.3</version>
</dependency>
</dependencies>
Convert PDF to XPS in Java
Spire.PDF for Java has a powerful conversion feature, which can convert PDF to XPS in just three steps. The detailed steps are as follows:
- Create a PdfDocument instance.
- Load a PDF sample document using PdfDocument.loadFromFile() method.
- Save the document as XPS using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.*;
public class PDFtoXPS {
public static void main(String[] args) {
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load the PDF file
pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf.pdf");
//Save to XPS
pdf.saveToFile("ToXPS.xps", FileFormat.XPS);
pdf.close();
}
}

Convert XPS to PDF in Java
The PdfDocument.saveToFile() method provided by Spire.PDF for Java enables the conversion of a XPS file into a PDF document. The following are steps to convert XPS to PDF.
- Create a PdfDocument instance.
- Load a XPS file document using PdfDocument.loadFromFile() method.
- Save the document as PDF using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.*;
public class XPStoPDF {
public static void main(String[] args) {
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a XPS file
pdf.loadFromXPS("C:\\Users\\Administrator\\Desktop\\sample.xps");
//Save to PDF
pdf.saveToFile("toPDF.pdf", FileFormat.PDF);
pdf.close();
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
SVG, short for scalable vector graphics, is a vector image format based on XML for two-dimensional graphics. Vector image files, like SVG and PDF files, are very similar. They can display text, images, and other elements in the same appearance and keep the definition no matter how you zoom them. And because of their similarity, PDF files can be converted to SVG files almost losslessly. This article shows an easy method to convert PDF files to SVG files using Spire.PDF for Java.
- Convert Each Page of a PDF File to an SVG File
- Convert All the Pages of a PDF File to a Single SVG File
Install Spire.PDF for Java
First, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.10.3</version>
</dependency>
</dependencies>
Convert Each Page of a PDF File to an SVG File
The detailed steps are as follows:
- Create an object of PdfDocument class.
- Load a PDF document from disk using PdfDocument.loadFromFile() method.
- Convert the document to SVG file and save it using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.*;
public class PDFToSVG {
public static void main(String[] args) {
//Create an object of Document class
PdfDocument pdf = new PdfDocument();
//Load a PDF document from disk
pdf.loadFromFile("D:/Samples/Sample.pdf");
//Convert the document to SVG and Save it
pdf.saveToFile("D:/javaOutput/PDFToSVG.svg", FileFormat.SVG);
}
}

Convert All the Pages of a PDF File to a Single SVG File
The detailed steps are as follows:
- Create an object of PdfDocument class.
- Load a PDF document from disk using PdfDocument.loadFromFile() method.
- Change the conversion settings to convert the PDF file to a single SVG file using PdfDocument.getConvertOptions().setOutputToOneSvg() method.
- Convert the document to SVG file and save it using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.*;
public class PDFToSVG {
public static void main(String[] args) {
//Create an object of Document class
PdfDocument pdf = new PdfDocument();
//Load a PDF document from disk
pdf.loadFromFile("D:/Samples/Sample.pdf");
//Change the conversion settings to convert the PDF file to a single SVG file
pdf.getConvertOptions().setOutputToOneSvg(true);
//Convert the document to SVG and Save it
pdf.saveToFile("D:/javaOutput/PDFToSVG.svg", FileFormat.SVG);
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
PDF file format makes the presentation of documents consistent across devices. However, when you need to put PDF documents on web pages, it's better to convert them to HTML files. In this way, all the content of your document can be displayed in the browser directly, with no need for downloading files. And the loading of large PDF documents takes a long time, while HTML files can be rendered in the browser very quickly. In addition, compared to PDF files, it is much easier for search engines to crawl HTML web pages to get information, which will give your website more exposure. This article will show how to convert PDF documents into HTML files in Java using Spire.PDF for Java.
- Convert a PDF document to an HTML file in Java
- Convert a PDF document to an HTML file with SVG Embedded
- Convert a PDF document to HTML Stream in Java
Install Spire.PDF for Java
First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.10.3</version>
</dependency>
</dependencies>
Convert a PDF document to an HTML file in Java
The conversion from a PDF document to an HTML file can be directly done by loading a PDF document and saving it as an HTML file using PdfDocument.saveToFile(String filename, FileFormat.HTML) method provided by Spire.PDF for Java. The detailed steps are as follows.
- Create an object of PdfDocument.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Save the PDF file as an HTML file using PdfDocument.saveToFle() method.
- Java
Java
import com.spire.pdf.*;
public class convertPDFToHTML {
public static void main(String[] args) {
//Create an object of PdfDocument
PdfDocument pdf = new PdfDocument();
//Load a PDF file
pdf.loadFromFile("C:/Guide to a Foreign Past.pdf");
//Save the PDF file as an HTML file
pdf.saveToFile("PDFToHTML.html",FileFormat.HTML);
pdf.close();
}
}
Convert a PDF document to an HTML file with SVG Embedded
Spire.PDF for Java also provides the PdfDocument.getConvertOptions().setPdfToHtmlOptions(true) method to enable embedding SVG while converting. The detailed steps for converting a PDF file to an HTML file with SVG embedded are as follows.
- Create an object of PdfDocument.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Enable embedding SVG using PdfDocument.getConvertOptions().setPdfToHtmlOptions(true) method.
- Save the PDF file as an HTML file using PdfDocument.saveToFle() method.
- Java
import com.spire.pdf.*;
public class convertPDFToHTMLEmbeddingSVG {
public static void main(String[] args) {
//Create an object of PdfDocument
PdfDocument doc = new PdfDocument();
//Load a PDF file
doc.loadFromFile("C:/Guide to a Foreign Past.pdf");
//Set embedding SVG
doc.getConvertOptions().setPdfToHtmlOptions(true);
//Save the PDF file as an HTML file
doc.saveToFile("PDFToHTMLEmbeddingSVG.html", FileFormat.HTML);
doc.close();
}
}
Convert a PDF document to HTML Stream in Java
Spire.PDF for Java also supports converting PDF documents to HTML stream. The detailed steps are as follows.
- Create an object of PdfDocument.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Save the PDF file as HTML stream using PdfDocument.saveToStream() method.
- Java
import com.spire.pdf.*;
import java.io.*;
public class convertPDFToHTMLStream {
public static void main(String[] args) throws FileNotFoundException {
//Create an object of PdfDocument
PdfDocument pdf = new PdfDocument();
//Load a PDF file
pdf.loadFromFile("C:/Guide to a Foreign Past.pdf");
//Save the PDF file as HTML stream
File outFile = new File("PDFToHTMLStream.html");
OutputStream outputStream = new FileOutputStream(outFile);
pdf.saveToStream(outputStream, FileFormat.HTML);
pdf.close();
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Nowadays, it is not difficult to convert PDF documents into Word files using a software. However, if you want to maintain the layout and even the font formatting while converting, it is not something that every software can accomplish. Spire.PDF for Java does it well and offers you the following two modes when converting PDF to Word in Java.
Fixed Layout mode has fast conversion speed and is conducive to maintaining the original appearance of PDF files to the greatest extent. However, the editability of the resulting document will be limited since each line of text in PDF will be presented in a separate frame in the generated Word document.
Flowable Structure is a full recognition mode. The converted content will not be presented in frames, and the structure of the resulting document is flowable. The generated Word document is easy to re-edit but may look different from the original PDF file.
Install Spire.PDF for Java
First, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.10.3</version>
</dependency>
</dependencies>
Convert PDF to Doc/Docx with Fixed Layout
The following are the steps to convert PDF to Doc or Docx with fixed layout.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Convert the PDF document to a Doc or Docx format file using PdfDocument.saveToFile(String fileName, FileFormat fileFormat) method.
- Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
public class ConvertPdfToWordWithFixedLayout {
public static void main(String[] args) {
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a sample PDF document
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Convert PDF to Doc and save it to a specified path
doc.saveToFile("output/ToDoc.doc", FileFormat.DOC);
//Convert PDF to Docx and save it to a specified path
doc.saveToFile("output/ToDocx.docx", FileFormat.DOCX);
doc.close();
}
}
Convert PDF to Doc/Docx with Flowable Structure
The following are the steps to convert PDF to Doc or Docx with flowable structure.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Set the conversion mode as flow using PdfDocument. getConvertOptions().setConvertToWordUsingFlow() method.
- Convert the PDF document to a Doc or Docx format file using PdfDocument.saveToFile(String fileName, FileFormat fileFormat) method.
- Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
public class ConvertPdfToWordWithFlowableStructure {
public static void main(String[] args) {
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a sample PDF document
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Convert PDF to Word with flowable structure
doc.getConvertOptions().setConvertToWordUsingFlow(true);
//Convert PDF to Doc
doc.saveToFile("output/ToDoc.doc", FileFormat.DOC);
//Convert PDF to Docx
doc.saveToFile("output/ToDocx.docx", FileFormat.DOCX);
doc.close();
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
PDF/A is a kind of PDF format designed for archiving and long-term preservation of electronic documents. Unlike paper documents that are easily damaged or smeared, PDF/A format ensures that documents can be reproduced in exactly the same way even after long-term storage. This article will demonstrate how to convert PDF to PDF/A-1A, 2A, 3A, 1B, 2B and 3B compliant PDF using Spire.PDF for Java.
Install Spire.PDF for Java
First of all, you're required to add the Spire.PDF.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.10.3</version>
</dependency>
</dependencies>
Convert PDF to PDF/A
The detailed steps are as follows:
- Create a PdfStandardsConverter instance, and pass in a sample PDF file as a parameter.
- Convert the sample file to PdfA1A conformance level using PdfStandardsConverter.toPdfA1A() method.
- Convert the sample file to PdfA1B conformance level using PdfStandardsConverter. toPdfA1B() method.
- Convert the sample file to PdfA2A conformance level using PdfStandardsConverter. toPdfA2A() method.
- Convert the sample file to PdfA2B conformance level using PdfStandardsConverter. toPdfA2B() method.
- Convert the sample file to PdfA3A conformance level using PdfStandardsConverter. toPdfA3A() method.
- Convert the sample file to PdfA3B conformance level using PdfStandardsConverter. toPdfA3B() method.
- Java
import com.spire.pdf.conversion.PdfStandardsConverter;
public class ConvertPdfToPdfA {
public static void main(String[] args) {
//Create a PdfStandardsConverter instance, and pass in a sample file as a parameter
PdfStandardsConverter converter = new PdfStandardsConverter("sample.pdf");
//Convert to PdfA1A
converter.toPdfA1A("output/ToPdfA1A.pdf");
//Convert to PdfA1B
converter.toPdfA1B("output/ToPdfA1B.pdf");
//Convert to PdfA2A
converter.toPdfA2A( "output/ToPdfA2A.pdf");
//Convert to PdfA2B
converter.toPdfA2B("output/ToPdfA2B.pdf");
//Convert to PdfA3A
converter.toPdfA3A("output/ToPdfA3A.pdf");
//Convert to PdfA3B
converter.toPdfA3B("output/ToPdfA3B.pdf");
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Converting PDF files to images is essential for various document processing tasks, including generating thumbnails, archiving, and image manipulation. These conversions allow applications to present PDF content in more accessible formats, enhancing user experience and functionality. Java libraries such as Spire.PDF for Java enable efficient conversions to formats like PNG , JPEG , GIF , BMP , TIFF , and SVG , each serving specific purposes based on their characteristics.
This guide will walk you through the conversion process using Spire.PDF for Java, providing optimized code examples for each format. Additionally, it will explain the key differences among these formats to help you select the most suitable option for your needs.
- Java PDF-to-Image Conversion Library
- Image Format Comparison
- Convert PDF to PNG, JPEG, GIF, and BMP
- Convert PDF to TIFF in Java
- Convert PDF to SVG in Java
- Conclusion
- FAQs
Java PDF-to-Image Conversion Library
Spire.PDF for Java is a robust, feature-rich library for PDF manipulation. It offers several advantages for image conversion:
- High-quality rendering that preserves document formatting and layout
- Batch processing capabilities for handling multiple documents
- Flexible output options including resolution and format control
- Lightweight implementation with minimal memory footprint
The library supports conversion to all major image formats while maintaining excellent text clarity and graphic fidelity, making it suitable for both simple conversions and complex document processing pipelines.
Installation
To get started, download Spire.PDF for Java from our website and add it as a dependency in your project. For Maven users, include the following in your pom.xml:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.6.2</version>
</dependency>
</dependencies>
Image Format Comparison
Different image formats serve distinct purposes. Below is a comparison of commonly used formats:
| Format | Compression | Transparency | Best For | File Size |
|---|---|---|---|---|
| PNG | Lossless | Yes | High-quality graphics, logos | Medium |
| JPEG | Lossy | No | Photographs, web images | Small |
| GIF | Lossless | Yes (limited) | Simple animations, low-color graphics | Small |
| BMP | None | No | Uncompressed images, Windows apps | Large |
| TIFF | Lossless | Yes | High-quality scans, printing | Very Large |
| SVG | Vector-based | Yes (scalable) | Logos, icons, web graphics | Small |
PNG is ideal for images requiring transparency and lossless quality, while JPEG is better for photographs due to its smaller file size. GIF supports simple animations, and BMP is rarely used due to its large size. TIFF is preferred for professional printing, and SVG is perfect for scalable vector graphics.
Convert PDF to PNG, JPEG, GIF, and BMP
Converting PDF files into various image formats like PNG, JPEG, GIF, and BMP allows developers to cater to diverse application needs. Each format serves different purposes; for example, PNG is ideal for high-quality graphics with transparency, while JPEG is better suited for photographs with smooth gradients.
By leveraging Spire.PDF's capabilities, developers can easily generate the required image formats for their projects, ensuring compatibility and optimal performance.
Basic Conversion Example
Let's start with the fundamental conversion process. The following code demonstrates how to convert each page of a PDF into individual PNG images:
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.PdfImageType;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ConvertPdfToImage {
public static void main(String[] args) throws IOException {
// Create a PdfDocument instance
PdfDocument doc = new PdfDocument();
// Load a PDF file
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");
// Iterate through the pages
for (int i = 0; i < doc.getPages().getCount(); i++) {
// Convert the current page to BufferedImage
BufferedImage image = doc.saveAsImage(i, PdfImageType.Bitmap);
// Save the image data as a png file
File file = new File("output/" + String.format(("ToImage-img-%d.png"), i));
ImageIO.write(image, "PNG", file);
}
// Clear up resources
doc.close();
}
}
Explanation:
- The PdfDocument class loads the input PDF file.
- saveAsImage() converts each page into a BufferedImage .
- ImageIO.write() saves the image in PNG format.
Tip : To convert to JPEG, GIF, or BMP, simply replace " PNG " with " JPEG ", " GIF ", or " BMP " in the ImageIO.write() method.
Output:

PNG with Transparent Background
Converting a PDF page to PNG with a transparent background involves adjusting the conversion options using the setPdfToImageOptionsmethod. This adjustment provides greater flexibility in generating the images, allowing for customized output that meets specific requirements.
Here’s how to achieve this:
doc.getConvertOptions().setPdfToImageOptions(0);
for (int i = 0; i < doc.getPages().getCount(); i++) {
BufferedImage image = doc.saveAsImage(i, PdfImageType.Bitmap);
File file = new File("C:\\Users\\Administrator\\Desktop\\Images\\" + String.format(("ToImage-img-%d.png"), i));
ImageIO.write(image, "PNG", file);
}
Explanation:
- setPdfToImageOptions(0) ensures transparency is preserved.
- The rest of the process remains the same as the basic conversion.
Custom DPI Settings
The saveAsImage method also provides an overload that allows developers to specify the DPI (dots per inch) for the output images. This feature is crucial for ensuring that the images are rendered at the desired resolution, particularly when high-quality images are required.
Here’s an example of converting a PDF to images with a specified DPI:
for (int i = 0; i < doc.getPages().getCount(); i++) {
BufferedImage image = doc.saveAsImage(i, PdfImageType.Bitmap,300, 300);
File file = new File("C:\\Users\\Administrator\\Desktop\\Images\\" + String.format(("ToImage-img-%d.png"), i));
ImageIO.write(image, "PNG", file);
}
Explanation:
- The saveAsImage() method accepts dpiX and dpiY parameters for resolution control.
- Higher DPI values (e.g., 300) produce sharper images but increase file size.
DPI Selection Tip:
- 72-100 DPI : Suitable for screen display
- 150-200 DPI : Good for basic printing
- 300+ DPI : Professional printing quality
- 600+ DPI : High-resolution archival
Convert PDF to TIFF in Java
TIFF (Tagged Image File Format) is another popular image format, especially in the publishing and printing industries. Spire.PDF makes it easy to convert PDF pages to TIFF format using the saveToTiff method.
Here’s a simple example demonstrating how to convert a PDF to a multi-page TIFF:
import com.spire.pdf.PdfDocument;
public class ConvertPdfToTiff {
public static void main(String[] args) {
// Create a PdfDocument object
PdfDocument doc = new PdfDocument();
// Load a PDF file
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");
// Convert a page range to tiff
doc.saveToTiff("output/PageToTiff.tiff",0,2,300,300);
// Clear up resources
doc.dispose();
}
}
Explanation:
- saveToTiff() converts a specified page range (here, pages 0 to 2).
- The last two parameters set the DPI for the output image.
Output:

Convert PDF to SVG in Java
SVG (Scalable Vector Graphics) is a vector image format that is widely used for its scalability and compatibility with web technologies. Converting PDF to SVG can be beneficial for web applications that require responsive images.
To convert a PDF document to SVG using Spire.PDF, the following code can be implemented:
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
public class ConvertPdfToSvg {
public static void main(String[] args) {
// Initialize a PdfDocument object
PdfDocument doc = new PdfDocument();
// Load the PDF document from the specified path
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");
// Optionally, convert to a single SVG (uncomment to enable)
// doc.getConvertOptions().setOutputToOneSvg(true);
// Save the document as SVG files (one SVG per page by default)
doc.saveToFile("Output/PDFToSVG.svg", FileFormat.SVG);
// Clear up resources
doc.dispose();
}
}
Explanation:
- saveToFile() with FileFormat.SVG exports the PDF as an SVG file.
- Optionally, setOutputToOneSvg(true) merges all pages into a single SVG.
Output:

Conclusion
Spire.PDF for Java simplifies PDF-to-image conversion with support for popular formats like PNG, JPEG, TIFF, SVG, and more . Key features such as transparency control, custom DPI settings, and multi-page TIFF/SVG output enable tailored solutions for generating thumbnails, high-quality prints, or web-optimized graphics. The library ensures high fidelity and performance , making it ideal for batch processing or dynamic rendering. Easily integrate the provided code snippets and APIs to enhance document handling in your Java applications, whether for reporting, archiving, or interactive content.
FAQs
Q1. Can I specify the DPI when converting to TIFF or SVG?
When converting to TIFF, you can specify the DPI to ensure high-quality output. However, SVG is a vector format and does not require DPI settings, as it scales based on the display size.
Q2. Can I convert specific pages of a PDF to images?
Yes, both the saveAsImage and saveToTiff methods allow you to indicate which pages to include in the conversion.
Q3. What is the difference between lossless and lossy image formats?
Lossless formats (like PNG and TIFF) retain all image quality during compression, while lossy formats (like JPEG) reduce file size by discarding some image information, which may affect quality.
Q4. How does converting to SVG differ from raster formats?
Converting to SVG generates vector images that scale without losing quality, while raster formats like PNG and JPEG are pixel-based and can lose quality when resized.
Q5. What other file formats can Spire.PDF convert PDFs to?
Spire.PDF is a powerful Java PDF library that supports converting PDF files to multiple formats, such as:
Get a Free License
To fully experience the capabilities of Spire.PDF for Java without any evaluation limitations, you can request a free 30-day trial license.