Web pages are used to show all kinds of information on the websites. In certain scenarios, you may need to save a webpage as a PDF in Java. In accordance with such scenarios, this article covers how to save a webpage as a PDF in Java.
- Save Webpage to PDF – Java API Configuration
- Save Webpage as PDF File Programmatically in Java
- Print Web page to PDF with Password Protection in Java
Save Webpage to PDF – Java API Configuration
Aspose.HTML for Java API can be used to work with HTML, PDF, and many other supported file formats. Simply configure the API by downloading its JAR file from New Releases page, or with the following configurations to access it from Aspose Repository:
<repositories> <repository> <id>snapshots</id> <name>repo</name> <url>http://repository.aspose.com/repo/</url> </repository> </repositories>
<dependencies> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-html</artifactId> <version>21.12</version> <classifier>jdk16</classifier> </dependency> </dependencies>
Save Webpage as PDF File Programmatically in Java
You need to follow the steps below to save a webpage as a PDF file in Java:
- Load the input webpage using its URL.
- Create an object of PdfSaveOptions class.
- Save the webpage to a PDF file.
The following code shows how to save webpage as PDF programmatically using Java:
Print web page to PDF with Password Protection in Java
You can easily print a web page to PDF while protecting it with a password. The steps below explain the process to create an encrypted PDF document:
- Load the input HTML document with the URL.
- Create an object of PdfEncryptionInfo class.
- Print the web page to PDF with PdfSaveOptions class.
The following code snippet demonstrates how to print a web page to PDF with password protection in Java:
In conclusion, you have learned how to save web pages as PDF in Java. Moreover, you have also understood the protection and encryption of the PDF document. Furthermore, you may visit the documentation section to learn other features supported by the API. In case of any concerns, please write to us at the forum.