HTML to PDF Converter: Convert HTML Code to PDF Free

📄 File and Document Free Forever

HTML to PDF Converter

Turn raw HTML markup into a paginated PDF with a live preview, custom margins, headers, footers, and page numbers. Renders your HTML safely in a sandboxed frame before conversion.

HTML Source
Live Preview
Page Settings
About This Tool

Why this converter renders your HTML in a hidden iframe before capturing it

Turning arbitrary HTML into a PDF is really a two step trick: paint the markup exactly as a browser would render it, then photograph that rendered layout and lay the photograph into a PDF page. This tool does the painting in a sandboxed iframe with its own document, and does the photographing with html2canvas, then hands the resulting image to jsPDF through the combined html2pdf.js library. Both steps run in your browser. The HTML you paste or upload never leaves the page.

Why an iframe, specifically

An earlier version of this approach captured content sitting directly on the main page and produced blank PDFs. html2canvas renders the entire page and then crops down to your target element’s bounding box, a documented behavior of the library. The fix was to build a completely isolated iframe with its own srcdoc document containing only the HTML being converted, positioned at true document coordinate (0,0) with position:absolute rather than position:fixed, and to force window.scrollTo(0,0) right before capture so the crop always lands in the right place regardless of how far down the page the visitor had scrolled.

Step 1 Build the render frame A hidden iframe is created, sandboxed with allow-same-origin, and loaded with your HTML via srcdoc. An opaque overlay covers it so you never see the raw unstyled flash.
Step 2 Wait for images Every <img> inside the rendered document is checked for .complete, and the tool waits for load or error events on any that are not finished, capped at a 4 second timeout so one dead image link cannot hang the conversion indefinitely.
Step 3 Capture and build the PDF html2canvas rasterizes the iframe body at your chosen scale factor, and html2pdf.js lays the resulting bitmap into a jsPDF document sized to your selected page format and orientation.
Step 4 Draw header, footer, page numbers After the visual capture, the tool loops over every generated page and draws header text, footer text, and “Page X of Y” numbering directly with jsPDF’s own text() calls, which is why those stay crisp text rather than part of the raster image.
// render frame positioning, from the tool source renderFrame.style.position = ‘absolute’; renderFrame.style.top = ‘0’; renderFrame.style.left = ‘0’; window.scrollTo(0, 0); renderFrame.srcdoc = html; // … var opts = { image: { type: ‘jpeg’, quality: 0.98 }, html2canvas: { scale: scale, useCORS: true, backgroundColor: ‘#ffffff’ }, jsPDF: { unit: ‘in’, format: pageSize, orientation: orientation } };

Options and their effect on the raster capture

OptionWhat it changes
Scalehtml2canvas’s device pixel multiplier; higher scale means a sharper but larger capture
BackgroundToggles between a forced white background and a transparent capture background
MarginNone, normal (0.5in) or wide (1in), applied as jsPDF page margin around the image
Allow scriptsAdds allow-scripts to the iframe sandbox, letting pasted JavaScript run before capture
Page numbersDraws real vector text via jsPDF, independent of the rasterized content
Complex CSS layouts and blocked cross-origin stylesheets can still fail. html2canvas approximates CSS rendering rather than using the browser’s actual paint pipeline for the capture, so advanced layout features and any external stylesheet blocked by CORS can render incorrectly or not at all, which is why the tool’s own error message names both as known limitations rather than claiming universal fidelity.
Live preview

A separate sandboxed iframe mirrors your input as you type, debounced by 350ms, so you can check the markup before spending time on a full conversion.

File upload

An .html file can be loaded directly with FileReader.readAsText(), populating the same textarea the paste option uses.

html2canvas rasterization jsPDF page assembly Sandboxed srcdoc iframe Vector header, footer, page numbers

Rendering engines and specs

Documents people generate this way

Archiving an email template or landing page as a fixed document, generating a printable invoice from a small HTML snippet, converting an internal report built as an HTML fragment into something you can attach to an email, or producing a quick PDF preview of a web component during development without setting up a headless browser toolchain.

Common Questions

FAQ: HTML to PDF Converter

Switch to the From URL tab and paste the page address. The tool will attempt to load it inside a sandboxed frame. Some sites set headers that block being embedded this way, in which case pasting the page HTML directly is the reliable option.

Yes. Both inline styles and style tags included in your HTML render exactly as a browser would display them, since the conversion uses an actual browser rendering pass rather than a simplified parser.

Yes. Enter text in the Header Text and Footer Text fields in Page Settings, and leave Add Page Numbers checked. All three are stamped onto every page of the resulting PDF.

Your markup is rendered inside a sandboxed iframe with scripting restricted, so the tool focuses purely on visual layout, fonts, colors, and structure, rather than running arbitrary code.

No. Rendering and PDF generation both happen locally in your browser. The only exception is if you choose the From URL option, which asks your own browser to load that address directly.

Privacy Overview

Cookies let this site remember your preferences and show us which tools people actually use. Full detail sits in our Privacy Policy.