Convert A URL Or HTML File To PDF In Java

Introduction

If you have content that is in HTML format, you may want to convert it to a PDF file. So, you can view it offline, share it with others on any PDF-compatible device, or send it to a printer. This article demonstrates how to convert a website URL or an HTML file to PDF by using Spire.PDF for Java.

Spire.PDF for Java is a comprehensive PDF library that allows developers to create, edit, convert and print PDF documents from within Java applications.

Install Libraries

To begin with, you need to download the Spire.Pdf.jar file from this link and add it as a dependency in your application. If you use Maven, you can easily import the JAR file in your application using the following configurations.

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

In addition, this scenario requires another open-source library – QT Web Engine. Please download the library that fits in with your operating system from the following link, and unzip it somewhere on your hard drive.

In this case, the unzipped file was saved under the path” F:\Libraries\Plugin\plugins-windows-x64”.

Convert a URL or HTML File to PDF in Java

Convert a URL to PDF

The HtmlConverter class is the only core class used for conversion. Invoke the setPluginPath() method of this class to specify the plugin path, and then call HtmlConvert.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. The following code snippet converts the home page of C-Sharpcorner to a PDF document.

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.c-sharpcorner.com/";
        //Specify the output file path
        String fileName = "output/WebToPdf.pdf";
        //Specify the plugin path
        String pluginPath = "F:\\Libraries\\Plugin\\plugins-windows-x64";
        //Set plugin path
        HtmlConverter.setPluginPath(pluginPath);
        //Convert URL to PDF
        HtmlConverter.convert(url, fileName, true, 1000000, new Size(1200 f, 1000 f), new PdfMargins(0));
    }
}

Output

Convert a URL or HTML File to PDF in Java

Convert an HTML File to PDF

The HtmlConvert.convert() method has a different overload HtmlConvert.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 urlHtml), which takes HTML string as a parameter. We need first convert our HTML file to a HTML string, and then render the HTML sting as a PDF document. It is worth mentioning that both inline CSS style and internal CSS style are supported while converting.

Here is what the sample HTML file looks like in a Web browser.

Convert a URL or HTML File to PDF in 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 ConvertHtmlToPdf {
    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";
        //Set plugin path
        HtmlConverter.setPluginPath(pluginPath);
        //Convert 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;
    }
}

Output

Convert a URL or HTML File to PDF in Java

Thanks for reading this article. Hopefully, it will help someone in need.