Rendering Settings for HTML to PDF
This code example demonstrates the power and flexibility that IronPDF provides developers in converting HTML content into PDF documents.
ChromePdfRenderOptions
ensures that developers have total control over how web pages are converted into PDFs.
With the ChromePdfRenderOptions
class, developers can customize page margins, specify page orientations, add annotations, include bookmarks, change document metadata, adjust zoom scaling, modify CSS rule sets, and much more. Once defined, objects of this class can be supplied as a second (optional) parameter to any of the static PDF document rendering methods (renderHtmlAsPdf
, renderHtmlFileAsPdf
, and renderUrlAsPdf
) to alter how IronPDF interprets website content, as well as how it renders the PDF content during these methods' execution.
Here's an example of how to use the ChromePdfRenderOptions
class:
import com.ironsoftware.ironpdf.ChromePdfRenderOptions;
import com.ironsoftware.ironpdf.PdfDocument;
public class Example {
public static void main(String[] args) {
// Create a new ChromePdfRenderOptions object
ChromePdfRenderOptions options = new ChromePdfRenderOptions();
// Customize the options for rendering PDF
// Set margins (in inches)
options.setMarginTop(1.0);
options.setMarginBottom(1.0);
options.setMarginLeft(0.75);
options.setMarginRight(0.75);
// Set page orientation to landscape
options.setLandscape(true);
// Set zoom level for the content
options.setZoom(1.2);
// Add metadata to the document
options.setTitle("Sample PDF Document");
options.setAuthor("IronPDF");
// Render the HTML content into a PDF
PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>Hello, IronPDF!</h1>", options);
// Save the rendered PDF to a file
pdf.saveAs("output.pdf");
}
}
import com.ironsoftware.ironpdf.ChromePdfRenderOptions;
import com.ironsoftware.ironpdf.PdfDocument;
public class Example {
public static void main(String[] args) {
// Create a new ChromePdfRenderOptions object
ChromePdfRenderOptions options = new ChromePdfRenderOptions();
// Customize the options for rendering PDF
// Set margins (in inches)
options.setMarginTop(1.0);
options.setMarginBottom(1.0);
options.setMarginLeft(0.75);
options.setMarginRight(0.75);
// Set page orientation to landscape
options.setLandscape(true);
// Set zoom level for the content
options.setZoom(1.2);
// Add metadata to the document
options.setTitle("Sample PDF Document");
options.setAuthor("IronPDF");
// Render the HTML content into a PDF
PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>Hello, IronPDF!</h1>", options);
// Save the rendered PDF to a file
pdf.saveAs("output.pdf");
}
}
- Import Required Libraries: Import the necessary classes (e.g.,
ChromePdfRenderOptions
andPdfDocument
) from the IronPDF library. - Create an Instance of
ChromePdfRenderOptions
: This object allows for the customization of rendering parameters such as margins, orientation, metadata, and zoom. - Set Margin Sizes: Define custom margin sizes to suite document layout requirements.
- Specify Page Orientation: Use
setLandscape(true)
to set the document in landscape mode. - Adjust Zoom Level: Customize the zoom scale to improve readability.
- Define Metadata: Include document title and author information using
setTitle()
andsetAuthor()
. - Render HTML to PDF: Utilize the
renderHtmlAsPdf
method to convert the HTML content into a PDF, applying the specified options. - Save the PDF: Save the resulting PDF document to the desired file path using
saveAs()
.
Explore ChromePdfRenderOptions Customizations in API Reference for the detailed list of available customizations in the API Reference.