Java: Convert HTML to PDF

The benefits of converting HTML documents to PDF files are:

  • Read and view offline
  • Easy to edit and comment
  • Easy to share and print
  • Smaller in size

This article demonstrates how to render an HTML webpage (URL) or an HTML string to a PDF document using Spire.PDF for Java with QT web engine.

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>10.10.7</version>
    </dependency>
</dependencies>
    

Download Plugin

This scenario also relies on QT web engine which is an open-source library that provides functionality for rendering HTML content. Please download the plugin that fits in with your operating system from the following link.

Unzip the package somewhere on your disk to get the "plugins" folder. In this example, we saved the plugin under the path "F:\Libraries\Plugin\plugins-windows-x64\plugins‪‪".

Java: Convert HTML to PDF

For Linux and Mac environments, please directly copy the plugin package to system and decompress it, and ensure that the decompressed folder has permission to read.

Please note that this conversion method requires GUI (graphical user interface) support. For servers that do not have a graphical user interface installed, follow the following steps to convert.

Convert a URL to PDF

The following are the steps to convert a URL to a PDF document using Spire.PDF for Java with QT plugin.

  • Specify the URL path to convert.
  • Specify the output file path.
  • Specify the plugin path, and pass it as a parameter of HtmlConvert.setPluginPath() method.
  • Call HtmlConverter.convert (String url, String fileName, boolean enableJavaScript, int timeout, com.spire.pdf.htmlconverter.qt.Size pageSize, com.spire.pdf.graphics.PdfMargins margins) method to convert a URL to PDF.
  • Java
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.wikipedia.org/";

        //Specify the output file path
        String fileName = "output/UrlToPdf.pdf";

        //Specify the plugin path
        String pluginPath = "F:\\Libraries\\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));
    }
}

Java: Convert HTML to PDF

Convert an HTML String to PDF

The following are the steps to convert an HTML string to a PDF document using Spire.PDF for Java with QT plugin.

  • Specify the URL path to convert.
  • Specify the output file path.
  • Specify the plugin path, and pass it as a parameter of HtmlConvert.setPluginPath() method.
  • Call HtmlConverter.convert (String htmlString, String fileName, boolean enableJavaScript, int timeout, com.spire.pdf.htmlconverter.qt.Size pageSize, com.spire.pdf.graphics.PdfMargins margins, com.spire.pdf.htmlconverter.LoadHtmlType htmlType) method to convert an HTML string to PDF.

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.

  • Java
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 ConvertHtmlStringToPdf {

    public static void main(String[] args) throws IOException {

        //Invoke the custom method HtmlToString() to convert HTML file to string
        String htmlString = HtmlToString("C:\\Users\\Administrator\\Desktop\\Sample.html");

        //Specify the output file path
        String outputFile = "output/HtmlToPdf.pdf";

        //Specify the plugin path
        String pluginPath = "F:\\Libraries\\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;
    }
}

Java: Convert HTML to 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.