Convert HTML to PDF using Java

Convert HTML to PDF using Java

As a Java developer, you can easily convert HTML files or web pages from live web URLs into PDF documents programmatically in your Java applications. In this article, you will learn how to convert HTML to PDF using Java.

The following topics are discussed/covered in this article:

HTML to PDF Conversion Java API

For converting HTML to PDF, I will be using GroupDocs.Conversion for Java API. It is a fast, efficient, and reliable file conversion solution for your Java applications without installing any external software. You can convert among all popular business document formats such as PDF, HTML, Email, Word, Excel, PowerPoint, Project, raster image file formats, and many more. It also allows you to display the whole ‎document, or render it partially to speed up the process. The API is compatible with all Java versions and supports popular operating systems (Windows, Linux, macOS) that are capable to run Java runtime.

You can download the JAR of the API or just add the following pom.xml configuration in your Maven-based Java application to try the below-mentioned code examples.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-conversion</artifactId>
        <version>21.7</version> 
</dependency>

Convert HTML to PDF using Java

You can easily convert HTML files to PDF documents by following the simple steps given below:

  1. Create an instance of the Converter class with the input file path
  2. Create an instance of the PdfConvertOptions
  3. Call the convert() method with the output file path and convert options

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

Convert HTML to PDF using Java
Convert HTML to PDF using Java

The Converter class is the main class that controls the document conversion process. It provides various methods to fulfill the conversion requests. The convert() method of this class converts the source document into the specified target format and saves the converted document at the given file path. It provides several overloaded convert() methods to convert supported file formats.

Convert HTML to PDF with Advanced Options

You can use some advanced settings while converting the HTML files to PDF documents by following the steps given below:

  1. Create an instance of the Converter class with the input file path
  2. Create an instance of PdfConvertOptions
  3. Set various options such as Rotation, Dpi, Width, Height, etc.
  4. Call the convert() method with the output file path and convert options

The following code sample shows how to convert an HTML file to a PDF document with advanced settings.

Convert HTML to PDF with Advanced Options using Java
Convert HTML to PDF with Advanced Options using Java

The PdfConvertOptions class provides several options to convert specified input files to PDF documents. I have used the following options:

  • Width — the setWidth() property sets the image width after conversion
  • Height — the setHeight() property sets the desired image height after conversion
  • Dpi — the setDpi() property sets the desired page DPI after conversion
  • Password — the setPassword() property protects the converted document with a password
  • Rotate — the setRotate() property allows page rotation with the following available options: None, On90, On180, On270

You can find more details about “Convert to PDF with Advanced Options” in the documentation.

HTML to PDF Conversion from a URL

You can convert the HTML web pages from the live URLs to the PDF documents by following the steps given below:

  1. Provide URL to an input stream object and open the stream
  2. Create an instance of the Converter class with the input stream object
  3. Create an instance of the PdfConvertOptions
  4. Call the convert() method with the output file path and convert options

The following code sample shows how to convert HTML from a web URL to a PDF document using Java.

Convert Specific Page Range of HTML to PDF

You can convert specific pages from multipage HTML documents to PDF documents by following the steps given below:

  1. Create an instance of the Converter class with the input file path
  2. Create an instance of PdfConvertOptions
  3. Set page number to start conversion from
  4. Set pages count to convert total number of pages
  5. Call the convert() method with the output file path and convert options

The following code sample shows how to convert specific pages from HTML to a PDF document using Java.

Convert HTML to PDF and Add Watermark

You can convert HTML files to watermarked PDF documents by following the steps given below:

  1. Create an instance of the Converter class
  2. Provide the input file path to the constructor
  3. Create an instance of the PdfConvertOptions
  4. Create an instance of the WatermarkOptions
  5. Set various options such as Text, Color, Width, Height, RotatationAngle, etc.
  6. Call the convert() method with the output file path and convert options

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

Convert HTML to PDF and Add Watermark using Java
Convert HTML to PDF and Add Watermark using Java

The WatermarkOptions class provides several options to add watermark to the converted documents. It enables you to add text or image watermarks to the converted documents.

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to convert HTML to PDF documents using Java. You have also learned how to add watermarks to the converted PDF documents. Moreover, you have learned how to convert HTML using advanced PDF convert options programmatically. This article also explained how to convert HTML webpages from a live URL to a PDF document using Java. You can learn more about GroupDocs.Conversion Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also