HTML Files to PDF

IronPDF can transform HTML files located on a computer or a network file path. To accomplish this, call the PdfDocument.renderHtmlFileAsPdf method, as the example below shows.

If successful, PdfDocument.renderHtmlFileAsPdf will return a new PdfDocument object containing the content of the HTML file found at the provided location. This method will also render all relatively referenced assets within the HTML file (such as paths to stylesheets, scripts, and images) faithfully within their proper context in the HTML file.

In short, the HTML file will appear as a PDF the same way that it appears in a browser—with all images and CSS intact!

PdfDocument.renderHtmlFileAsPdf will throw an IOException if it is unable to locate the HTML file or open it for reading.

Refer to the IronPDF API Documentation for ChromePdfRenderOptions for more information on how to use the class to customize the appearance of PDF documents created from the renderHtmlFileAsPdf method.

Here is an example of how to use the renderHtmlFileAsPdf method:

import com.ironsoftware.Ironpdf.PdfDocument;
import java.io.IOException;

public class HtmlToPdfExample {
    public static void main(String[] args) {
        try {
            // Path to the HTML file
            String htmlFilePath = "path/to/your/htmlfile.html";

            // Convert HTML file to PDF
            PdfDocument pdf = PdfDocument.renderHtmlFileAsPdf(htmlFilePath);

            // Save the converted PDF to a specified path
            pdf.saveAs("path/to/your/output.pdf");

            // Inform the user of success
            System.out.println("PDF created successfully at path/to/your/output.pdf");
        } catch (IOException e) {
            // Informs the user if the HTML file cannot be read
            System.err.println("Failed to read the HTML file: " + e.getMessage());
        }
    }
}
import com.ironsoftware.Ironpdf.PdfDocument;
import java.io.IOException;

public class HtmlToPdfExample {
    public static void main(String[] args) {
        try {
            // Path to the HTML file
            String htmlFilePath = "path/to/your/htmlfile.html";

            // Convert HTML file to PDF
            PdfDocument pdf = PdfDocument.renderHtmlFileAsPdf(htmlFilePath);

            // Save the converted PDF to a specified path
            pdf.saveAs("path/to/your/output.pdf");

            // Inform the user of success
            System.out.println("PDF created successfully at path/to/your/output.pdf");
        } catch (IOException e) {
            // Informs the user if the HTML file cannot be read
            System.err.println("Failed to read the HTML file: " + e.getMessage());
        }
    }
}
JAVA

Explanation:

  • Import Statement: The import com.ironsoftware.Ironpdf.PdfDocument; line is essential to bring in the necessary IronPDF libraries to work with PdfDocument objects.
  • Try-Catch Block: The code is wrapped in a try-catch block to handle IOException which is thrown when the HTML file cannot be read. This provides user-friendly error handling.
  • Path Specification: Ensure that the htmlFilePath and output PDF path are accurately specified.
  • PDF Creation and Saving: The PdfDocument.renderHtmlFileAsPdf(htmlFilePath) method converts an HTML file to a PDF, and the saveAs method saves it to a specified location on the file system.

This example demonstrates the basic usage of the renderHtmlFileAsPdf method to convert an HTML file into a PDF format, utilizing IronPDF's capabilities to handle assets such as CSS and images seamlessly.