PDF is the most widely used document format for exchanging digital information. The cross-platform compatibility, stable layout and security features of PDF format have made it quite famous. The conversion from other documents to PDF is highly in practice these days and HTML to PDF is one of the popular conversion scenarios for converting the content of the web pages to PDF documents.
The online HTML to PDF converters allow you to convert the 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 SDK or API. In this article, you’ll learn how easy it is to convert a single or batch of HTML documents to PDF programmatically in Java. The following scenarios will be addressed in the rest of the article.
- Convert HTML document to PDF in Java
- Convert HTML to password-protected PDF in Java
- Convert HTML to PDF using URL in Java
- Convert HTML content to single-page PDF in Java
Aspose.PDF for Java API for HTML to PDF Converter
Aspose.PDF for Java is a powerful Java class library to work with PDF documents. In addition to the other PDF manipulation features, Aspose.PDF for Java also provides high-quality HTML to PDF conversion features. You can download the API’s JAR file or install it using the following configurations in your Maven-based Java project.
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository>
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-pdf</artifactId> <version>19.12</version> <classifier>jdk17</classifier> </dependency>
Convert HTML to PDF in Java
The following is the simple recipe to convert an HTML file to PDF using Aspose.PDF for 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 PDF in Java.
Input HTML File
Converted PDF Document
Convert HTML to Password-Protected PDF in Java
Aspose.PDF for Java also lets you convert HTML file to encrypted PDF document. 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 in Java.
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 in Java.
Convert HTML to a Single Page PDF in Java
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.