Responsive HTML to PDF
Rendering an HTML page into a PDF is pretty straightforward and easy to do. However, when the page is responsive, we have to be more careful and the page can change with time and page navigation. IronPDF can render responsive HTML into PDF documents.
In this example, we will show you how to render a responsive HTML page into a PDF.
By using the UseResponsiveCssRendering
function of the PaperFit
property of the PdfRenderingOptions
class, you can specify the size of the virtual Chrome window, which will render your responsive HTML.
This allows developers to programmatically choose a specific responsive layout of a webpage designed using responsive CSS3, and supports modern responsive HTML5 frameworks such as Bootstrap.
IronPdf.ChromePdfRenderer
allows any web page or HTML snippet to be turned into a PDF document using an up-to-date embedded Chrome browser.
As of 2021, Iron Software recommends ChromePdfRenderer
as our best PDF Renderer and a drop-in replacement for this class. You can also apply cookies from the specified login credentials class to the specified URL, create a PDF file from an HTML string, and return it as a PdfDocument
object which can be edited and saved to disk or served on a website. For further details on how to utilize the ChromePdfRenderer, visit the IronPDF product page.
Here is a sample code with comments to illustrate how to render a responsive HTML page into a PDF using IronPDF:
using IronPdf;
class Program
{
static void Main()
{
// Create an instance of ChromePdfRenderer
var Renderer = new ChromePdfRenderer();
// Set up rendering options to use responsive CSS rendering
var renderingOptions = new PdfRenderingOptions
{
PaperFit = IronPdf.Rendering.PaperFitMode.UseResponsiveCssRendering
};
// Assign the options to the renderer
Renderer.RenderingOptions = renderingOptions;
// Define the HTML content or URL that you want to convert to PDF
string htmlString = "<html><body><h1>Responsive HTML to PDF Example</h1></body></html>";
// Render the HTML string to a PDF document
PdfDocument pdf = Renderer.RenderHtmlAsPdf(htmlString);
// Save the PDF document to disk
pdf.SaveAs("ResponsiveExample.pdf");
// Output a completion message
Console.WriteLine("PDF has been successfully created!");
}
}
using IronPdf;
class Program
{
static void Main()
{
// Create an instance of ChromePdfRenderer
var Renderer = new ChromePdfRenderer();
// Set up rendering options to use responsive CSS rendering
var renderingOptions = new PdfRenderingOptions
{
PaperFit = IronPdf.Rendering.PaperFitMode.UseResponsiveCssRendering
};
// Assign the options to the renderer
Renderer.RenderingOptions = renderingOptions;
// Define the HTML content or URL that you want to convert to PDF
string htmlString = "<html><body><h1>Responsive HTML to PDF Example</h1></body></html>";
// Render the HTML string to a PDF document
PdfDocument pdf = Renderer.RenderHtmlAsPdf(htmlString);
// Save the PDF document to disk
pdf.SaveAs("ResponsiveExample.pdf");
// Output a completion message
Console.WriteLine("PDF has been successfully created!");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Create an instance of ChromePdfRenderer
Dim Renderer = New ChromePdfRenderer()
' Set up rendering options to use responsive CSS rendering
Dim renderingOptions = New PdfRenderingOptions With {.PaperFit = IronPdf.Rendering.PaperFitMode.UseResponsiveCssRendering}
' Assign the options to the renderer
Renderer.RenderingOptions = renderingOptions
' Define the HTML content or URL that you want to convert to PDF
Dim htmlString As String = "<html><body><h1>Responsive HTML to PDF Example</h1></body></html>"
' Render the HTML string to a PDF document
Dim pdf As PdfDocument = Renderer.RenderHtmlAsPdf(htmlString)
' Save the PDF document to disk
pdf.SaveAs("ResponsiveExample.pdf")
' Output a completion message
Console.WriteLine("PDF has been successfully created!")
End Sub
End Class
Key Highlights:
Responsive Rendering: The use of
UseResponsiveCssRendering
ensures that the PDF mirrors the view of a responsive HTML page as it would appear in the specified virtual window size.ChromePdfRenderer: A powerful tool from IronPDF that leverages a real Chrome browser for rendering HTML, ensuring the latest web standards are supported.
- Flexibility: Options related to the rendering can be fine-tuned using
PdfRenderingOptions
, making it versatile for different HTML content needs.
For further details on how to utilize the ChromePdfRenderer, visit the IronPDF product page.