HTML to PDF

This code example illustrates how IronPDF can transform raw HTML content into high-quality, pixel-perfect PDF documents. IronPDF can render HTML markup with a simple structure (like the one given in the example above), and more complex markup that incorporates nested elements, stylesheet declarations, and JavaScript interactions.

Behind the scenes, IronPDF utilizes a built-in Chrome rendering engine to interpret HTML, JavaScript, and CSS content as proper web content before translating it accurately into its equivalent PDF-based representation. This ensures that IronPDF outputs PDFs whose content appears the same way that it would appear in a standards-compliant web browser.

IronPDF's PdfDocument class provides several static methods that allow Java developers to render HTML content from different sources. PdfDocument.renderHtmlAsPdf, as shown in the example above, transforms a string containing HTML markup into its PDF form. To convert web content located on a local network or across the Internet, call the PdfDocument.renderUrlAsPdf, which accepts a string containing the URL of the web page to be rendered. For HTML files saved to a local file system directory, specify their paths in calls to the PdfDocument.renderHtmlFileAsPdf method.

Developers can customize the appearance of the PDF documents returned from the three previous methods using the ChromePdfRenderOptions class. Refer to the ChromePdfRenderOptions API Documentation for more information.

// Import necessary packages
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;

// Example demonstrating how to convert HTML content to a PDF document using IronPDF
public class PdfGenerator {

    public static void main(String[] args) {

        // Define HTML content to be converted into PDF
        String htmlContent = "<html><body><h1>Hello World</h1><p>This is a PDF document generated from HTML content.</p></body></html>";

        try {
            // Convert the HTML string to a PdfDocument object
            PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlContent);

            // Specify options for rendering the PDF
            ChromePdfRenderOptions options = new ChromePdfRenderOptions();
            // Customize options as needed
            // e.g., options.setPaperSize(PaperSize.A4);

            // Save the generated PDF to a file
            pdf.saveAs("output.pdf");

            System.out.println("PDF Created Successfully!");

        } catch (Exception e) {
            // Handle exceptions that may occur during PDF generation
            e.printStackTrace();
        }
    }
}
// Import necessary packages
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;

// Example demonstrating how to convert HTML content to a PDF document using IronPDF
public class PdfGenerator {

    public static void main(String[] args) {

        // Define HTML content to be converted into PDF
        String htmlContent = "<html><body><h1>Hello World</h1><p>This is a PDF document generated from HTML content.</p></body></html>";

        try {
            // Convert the HTML string to a PdfDocument object
            PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlContent);

            // Specify options for rendering the PDF
            ChromePdfRenderOptions options = new ChromePdfRenderOptions();
            // Customize options as needed
            // e.g., options.setPaperSize(PaperSize.A4);

            // Save the generated PDF to a file
            pdf.saveAs("output.pdf");

            System.out.println("PDF Created Successfully!");

        } catch (Exception e) {
            // Handle exceptions that may occur during PDF generation
            e.printStackTrace();
        }
    }
}
JAVA