HTML to PDF Java Conversion using Java PDF API

This article shows HTML to PDF conversion from a file or web page using Java.

HTML to PDF Java

The online HTML to PDF converters allow you to convert single or multiple HTML pages to PDF documents. However, in case you want to perform this conversion programmatically or you need to do a batch conversion of HTML files to PDF, you’ll definitely look for some library or API. So let’s have a look at how to cope with such scenarios.

HTML to PDF Java Converter API – Free Download

Aspose.PDF for Java is a powerful Java class library to work with PDF documents. In addition to the other PDF manipulation features, the API also provides high-quality HTML to PDF conversion features. You can download the API’s JAR file or install using Maven configurations.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

Dependency

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>19.12</version>
    <classifier>jdk17</classifier>
</dependency>

How to Convert HTML to PDF

The following are the steps to convert HTML content to PDF using Aspose.PDF API.

  • Download the Aspose.PDF for Java
  • Integrate the API within your application
  • Load the HTML content from file or URL
  • Create PDF from provided HTML content

HTML to PDF Java Conversion

This is how you can convert an HTML file to PDF using Java.

  • Create an instance of the HtmlLoadOptions class.
  • Create an instance of Document class and initialize it with the HTML file’s path and HtmlLoadOptions object.
  • Call Document.save(String) method with the output PDF file’s name.

The following code sample shows how to convert an HTML document to a PDF.

Input HTML File

Java HTML to PDF

Converted PDF Document

Java HTML to PDF Library

Convert HTML to Password-Protected PDF in Java

Aspose.PDF also lets you convert HTML files to encrypted PDF documents. You can protect the resultant PDF document by specifying the user’s password, owner’s password, access permissions, and cryptographic algorithm in the Document.encrypt() method.

The following code sample shows how to convert HTML to an encrypted PDF.

Output

Convert HTML to PDF

Convert HTML to PDF using URL in Java

You can also convert the web page from the live URL to the PDF document. Below are the steps to achieve this:

  • Create and initialize the URL object.
  • Get the web page into the InputStream object.
  • Create the Document object and initialize it with InputStream and HtmlLoadOptions objects.
  • Save HTML stream as PDF using Document.save() method.

The following code sample shows how to convert HTML content from URL to PDF.

HTML to a Single Page PDF

In case you want to convert the whole HTML content into a single page PDF document, you can set HtmlLoadOptions.setRenderToSinglePage(true) as shown in the following Java code sample.

Get API’s License for Free

Get your temporary license to use and evaluate Aspose.PDF for Java for free for one month.

Conclusion

In this article, you have seen how to convert HTML content from a file or URL to PDF using Java. In addition, the code samples demonstrated how to convert HTML to a single-paged or password-protected PDF. You can explore more about Aspose.PDF for Java using documentation.

Related Articles