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

import com.aspose.html.License;
import com.aspose.html.drawing.Color;
import com.aspose.html.drawing.Size;
import com.aspose.html.drawing.Page;
import com.aspose.html.drawing.Length;
import com.aspose.html.rendering.PageSetup;
import com.aspose.html.saving.PdfSaveOptions;
public class ConvertHtmlFileToPdfInJava {
public static void main(String[] args) throws Exception { //main function to convert HTML to PDF in Java
// Load Aspose.Html license to avoid watermark in the output PDF file
License licenseForHtmlToPdf = new License();
licenseForHtmlToPdf.setLicense("Aspose.Html.lic");
// Initialize PdfSaveOptions class object to customize PDF generated from HTML
PdfSaveOptions pdfSaveOptionsObj = new PdfSaveOptions();
// Initialize PageSetup and Page classes object
PageSetup pageSetupObj = new PageSetup();
Page pageObj = new Page();
// Set page size to A4 i.e. 8.25 x 11.75 inches
pageObj.setSize(new Size(Length.fromInches(8.25f),Length.fromInches(11.75f)));
// Set the page for the page setup object
pageSetupObj.setAnyPage(pageObj);
// Set the page setup for the PdfSaveOptions class object
pdfSaveOptionsObj.setPageSetup(pageSetupObj);
// Now, applying Tan color to background
pdfSaveOptionsObj.setBackgroundColor(Color.getTan());
// Convert HTML document to PDF
com.aspose.html.converters.Converter.convertHTML("FirstFile.html",pdfSaveOptionsObj,
"outputPdfForGeneratedHtml.pdf");
}
}

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