How to Debug HTML in Chrome to Create Pixel Perfect PDFs
As the developers of IronPDF, we understand that PDF documents made by IronPDF not only need look perfect, but also need to look exactly how our customers expect them to. To make your PDFs look pixel perfect, you will need to develop excellent HTML templates or work with a Web Developer to do it for you. IronPDF has the options to render your PDFs to be Pixel Perfect to your HTML and appear identical to how they appear in Chrome because of our Chrome Renderer.
What is IronPDF's Chrome Renderer?
It makes IronPDF unique from other .NET PDF Libraries
IronPDF is distinct in the market as it utilizes the Google Chromium Renderer, meaning the HTML you see in Chrome browsers will look exactly the same as what you see in our PDFs. In this guide we will walk you through some settings you will need to apply in Chrome and in IronPDF to achieve this.
IronPDF compared with Competitors
To see a full list of comparisons with other products, see our blog.
Features | IronPDF's Chrome Renderer | Aspose vs IronPDF | iText vs IronPDF
IronPDF's Optimized Chrome Renderer is better than Chrome
Two examples of IronPDF surpassing Chrome itself. Buttons and Text that would otherwise be split onto two pages with Chrome remain in-tact with IronPDF.
Example of Buttons and Text being cut off in Chrome:
Another Example of Text being cut off in Chrome:
Why to use HTML to PDF?
- The layout and design of the output matches the website exactly.
- Web Developers can focus on designing with absolute precision.
- .NET developers can focus on application logic rather than layout. Back-end developers can delegate layout and design tasks to web designers.
1. Decide to use CSS Media Type Print or Screen
IronPDF's renderer has two media rendering options:
Screen. For a visual side-by-side comparison please see the end of this guide. Under the "Comparison of Screen and Print Example" section.
CssMediaType.Printis the default rendering option that optimizes your HTML for general use with a printer. This means that some background images, icons, and other ink-heavy elements on the page may render differently or be omitted. This option is good for documents without background images and is the default print-preview.
CssMediaType.Screenis the rendering option that will allow your PDFs to look exactly how they appear within Chrome on your screen. You will need to set up some options within your Chrome browser to enable the print-preview to look exactly how IronPDF will render for HTML debugging purposes.
// Pixel Perfect HTML Formatting Settings IronPdf.ChromePdfRenderer renderer = new IronPdf.ChromePdfRenderer(); renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Print; // or Screen
' Pixel Perfect HTML Formatting Settings Dim renderer As New IronPdf.ChromePdfRenderer() renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Print ' or Screen
Repeating Table Headers
If you have HTML containing a table that will span more than one page, you would probably like the Table Headers to repeat on each PDF page.
- In this case, you can only use the
Screeninstructs Chrome to only print the headers once.
Comparison of Screen and Print Example
Many images and icons will not load in Print mode which will load in Screen mode:
2. Set up Chrome
IronPDF proudly utilizes a Chrome Rendering Engine. Follow these steps for IronPDF to render your HTML exactly as you see it within Chrome. These are the few steps that need to be done beforehand within Chrome to achieve this.
To render Pixel Perfect PDFs to include all images, icons, and backgrounds that are usually omitted when in printing format, be sure to use the
Screen CSS Media option.
2a. Open DevTools and set Enable Emulate CSS media to Screen/Print in Chrome
- To do this, within Chrome open DevTools.
- Press Command+Shift+P (Mac) or Control+Shift+P (Windows, Linux, ChromeOS) to open the Command Menu. Start typing rendering, select Show Rendering, and press Enter. DevTools displays the Rendering tab at the bottom of your DevTools window. (more help)
- Navigate down and find the drop-down for
Emulate CSS mediaand choose the option
2b. Open the Print Preview window (Ctrl+P on Windows)
You may need to change the paper size and enable 'printing background images' in the Chrome Print dialog in order to get an accurate display.
2c. Set the correct Print Preview settings
- Please choose the Paper Size you are using in your IronPDF project, such as
- Under the
Customand set all four sides to (1") one inch margin.
- Enable the
- Ensure you set your
Landscapeif you intend to output a landscape document.
And that is all. You can now debug your HTML and use the print preview to see exactly how IronPDF will render your layout.
3. Set up IronPDF
3a. Set the CSS Media Type in IronPDF
In order to match the webpage design with 100% precision, we must choose the same CSS Media Type that we set in Chrome in our code.
PdfCssMediaType.Screen includes backgrounds, and larger images that may be omitted in the
PdfCssMediaType.Print format which is the default and designed for printers to save ink.
// Example using PdfCssMediaType.Screen IronPdf.ChromePdfRenderer renderer = new IronPdf.ChromePdfRenderer(); renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Screen; // or Print renderer.RenderingOptions.PrintHtmlBackgrounds = true;
' Example using PdfCssMediaType.Screen Dim renderer As New IronPdf.ChromePdfRenderer() renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Screen ' or Print renderer.RenderingOptions.PrintHtmlBackgrounds = True
3b. Setting Render Delay and Render Timeout appropriately
IronPDF's default rendering timeout is 60 seconds. Anything that takes longer than this to process will not render without editing the rendering options. To override the default settings, you will need to adjust the RenderingOptions.Timeout option.
Keep in mind that having no timeout set or having a render delay longer than your set timeout will produce a "Unable to Render PDF" Exception. So if you are encountering this error you may need to increase these values.
// Example of setting Timeout and RenderDelay options renderer.RenderingOptions.Timeout = 90; // seconds (default is 60) renderer.RenderingOptions.RenderDelay = 30000; // milliseconds
' Example of setting Timeout and RenderDelay options renderer.RenderingOptions.Timeout = 90 ' seconds (default is 60) renderer.RenderingOptions.RenderDelay = 30000 ' milliseconds
- RenderDelay (API Reference)
- Timeout (API Reference)