Responsive HTML to PDF
IronPDF for Python allows you to control the virtual browser viewport width used during rendering, ensuring responsive HTML layouts are captured at the correct breakpoint.
Getting Started
Set CssMediaType, ViewPortWidth, and FitToPaperMode on a ChromePdfRenderer instance before calling a render method. These three settings work together to control how the page layout is evaluated and scaled to fit the PDF paper size.
Understanding the Code
CssMediaType = PdfCssMediaType.Screen: Applies@media screenCSS rules instead of print styles. Use this when the page has a responsive desktop layout you want to preserve.ViewPortWidth = 1920: Sets the width of the virtual browser window in pixels. The page will lay out as if viewed in a 1920px-wide browser, activating the appropriate responsive breakpoints.FitToPaperMode = FitToPaperModes.AutomaticFit: Scales the rendered content to fit within the physical PDF page dimensions automatically, avoiding cropped or overflowing content.RenderHtmlFileAsPdf("Responsive.html"): Renders a local HTML file using the configured viewport settings.
Choosing the Right Viewport Width
Match ViewPortWidth to the breakpoint your HTML is designed for. Common values are:
- 1920 — Full HD desktop layout
- 1280 — Standard widescreen desktop
- 768 — Tablet/medium breakpoint
- 375 — Mobile layout






