Web page PDF Java

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

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:

Repository:

 <repositories>
     <repository>
         <id>snapshots</id>
         <name>repo</name>
         <url>http://repository.aspose.com/repo/</url>
     </repository>
</repositories>

Dependency:

 <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:

  1. Load the input webpage using its URL.
  2. Create an object of PdfSaveOptions class.
  3. 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:

  1. Load the input HTML document with the URL.
  2. Create an object of PdfEncryptionInfo class.
  3. 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:

Conclusion

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.

See Also

Convert XHTML to PDF in Java