How to Convert HTML File to PDF in Java

This tutorial describes how to convert HTML file to PDF in java where an existing HTML file is loaded and saved as a PDF file. Different attributes of the PDF file can also be set like the page size and background color. By convertHTML() function, you can write HTML to PDF converter using java.

Steps to Generate PDF from HTML in Java

  1. Configure the project to add Aspose.HTML library from the Maven repository
  2. Declare and initialize the PdfSaveOptions object to customize the output PDF file
  3. Create and initialize the Page and PageSetup classes to set the output PDF page size
  4. Set the background color of the output PDF using the PdfSaveOptions object
  5. Convert the source HTML file to PDF by providing the input HTML file name and desired options configured above

These steps provide guidance to convert HTML to PDF in Java by configuring the project to add Aspose.HTML library from the Maven repository. The customization using the PdfSaveOptions is optional and you can skip this step if required. A number of many other properties can also be set along with the page size and background color mentioned in example.

Code to Convert HTML File to PDF in Java

This code uses PdfSaveOptions, PageSetup, Page, and Converter classes for performing this task. You can set the page size in inches, pixels, points, dots per inch, dots per pixel, and many more. You can also set image compression, image transparent color, Jpeg quality, digital signature details, text compression, etc. using PdfSaveOptions.

This topic shows how using Java HTML to PDF converter can be written. There are many other options available also like converting HTML to text as described in the article on how to convert HTML to text in Java.

 English