- HTML to PDF APIConvert HTML and URLs into PDFsExtract PDF Form Data APIEasily extract data from PDFsWatermark PDF APIAdd custom watermarks to PDFsProtect PDF APISecure your PDFs with passwordCompress PDF APIReduce file size without losing qualityFlatten PDF APIFlatten PDFs to make form permanentDigital Signature APISend documents for signing
- API DocumentationAPI DocumentationFull REST API referenceNodeJS SDKClient library for Node.jsJava SDKClient library for JavaC# SDKClient library for C#PHP SDKClient library for PHPPython SDKClient library for Python
- Pricing
- Contact Us

Converting a Web Page to PDF
Learn how to convert a web page or URL into a PDF that preserves layout, fonts, and styling. Compare browser print, headless-browser libraries, and an HTML to PDF API, with copy-paste code examples.
Converting a web page to PDF means taking a page built with standard web technologies, such as https://example.com, rendering it exactly as a browser would, and exporting that rendered result as a static, shareable PDF document. The fastest way to do it programmatically is to send the page URL to an HTML to PDF API, which loads the page, applies its styles, waits for the content to finish rendering, and returns a finished PDF.
The output preserves the layout, fonts, images, and styling of the original page, so the PDF looks the way the page does in a browser rather than a stripped-down text dump.
Key takeaways
- Conversion renders the page in a real browser engine, then captures it as a static PDF that keeps layout, fonts, and images.
- Three common methods: browser Print to PDF (manual), headless-browser libraries (self-hosted), and an HTML to PDF API (automated at scale).
- For app workflows, an API converts a URL or raw HTML to PDF in a single request from your backend.
What converting a web page to PDF actually means
Behind the scenes, the conversion process loads the page, applies its CSS, runs any JavaScript, waits for the content to finish rendering, and then captures the result as a PDF. Because this happens inside a browser engine, the PDF reflects the page as a person would see it, including web fonts, background images, and dynamically generated content.
For one-off or manual conversions, this can be done with browser tools or online utilities. When you need to generate PDFs automatically, at scale, or as part of an application workflow, a programmatic solution is the practical choice.
Ways to convert a web page to PDF
1. Print to PDF from your browser
Every modern browser can print a page to PDF from the print dialog. It is free and needs no setup, which makes it fine for the occasional export. It is manual, handles one page at a time, and cannot be triggered from your own application, so it does not scale to automated document generation.
2. Headless-browser libraries
Tools such as Puppeteer and Playwright drive a headless Chromium instance, and wkhtmltopdf renders through a WebKit engine. They give you full control and run on your own infrastructure. The trade-off is operational: you install and update the browser binaries, manage memory and concurrency, and keep the rendering environment patched, which becomes real maintenance work at volume.
3. An HTML to PDF API
An HTML to PDF API moves the rendering engine off your servers. You send a URL or a block of HTML in a single request and get a finished PDF back, with no browser binaries to maintain. This is the most direct option when conversion needs to run automatically inside a backend or service.
| Method | Setup | Automatable | Scales | Maintenance | Best for |
|---|---|---|---|---|---|
| Browser Print to PDF | None | No | No | None | Occasional manual exports |
| Headless-browser library | High | Yes | With effort | You own it | Full control, self-hosted |
| HTML to PDF API | Low | Yes | Yes | Managed | Automated generation at scale |
How to convert a URL to PDF with an API
With an API like PDFGate, converting a live web page is a single POST request. Send the page URL and a page size, and the response is the PDF itself. Here is the same conversion in cURL and Node.js.
cURL
1curl -H "Content-Type: application/json" \2 -H "Authorization: Bearer YOUR_API_KEY" \3 --request POST \4 --data '{"url":"https://example.com","pageSizeType":"a4"}' \5 https://api.pdfgate.com/v1/generate/pdf \6 -o output.pdfNode.js
1const fetch = require("node-fetch");2const fs = require("fs");34const options = {5 method: "POST",6 headers: {7 "Content-Type": "application/json",8 "Authorization": "Bearer YOUR_API_KEY",9 },10 body: JSON.stringify({11 url: "https://example.com",12 pageSizeType: "a4",13 }),14};1516fetch("https://api.pdfgate.com/v1/generate/pdf", options).then(async (result) => {17 if (result.status === 201) {18 result.body.pipe(fs.createWriteStream("output.pdf"));19 } else {20 const error = await result.json();21 console.log(error);22 }23});Swap the url field for an html field to convert a raw HTML string instead of a live page, which is useful for templated invoices and reports. For complete, copy-paste examples in each language, see our HTML to PDF API guide for Node.js, Python, and PHP.
Common use cases
Invoice generation
E-commerce platforms and SaaS tools generate and send branded invoices in PDF format automatically as each order completes. See automating invoice generation with HTML to PDF for a full workflow.
Dynamic reports
Web dashboards are converted into downloadable PDF reports on demand, giving users a portable, shareable snapshot of live data.
Legal and compliance records
Timestamped, uneditable PDFs capture web pages, agreements, and compliance documents as they existed at a point in time.
Web-to-PDF export
A one-click download lets users save pages, articles, or receipts in a clean, print-ready format.
Things to watch when converting web pages to PDF
- Rendering timing: pages that load data with JavaScript need the converter to wait until rendering finishes, or the PDF captures a half-empty page.
- Fonts and assets: web fonts, images, and stylesheets must be publicly reachable at render time, or they fall back to defaults in the PDF.
- Page size and margins: set the page size and margins explicitly so content is not clipped at the edges. PDFGate exposes
pageSizeTypeandmarginfor this. - Headers and footers: browser print chrome does not carry over, so add repeating page headers or footers through the API if you need them.
- Authentication: pages behind a login cannot be reached by URL alone; convert the rendered HTML instead, or expose a token-protected endpoint.
Frequently asked questions
Can I convert any URL to a PDF?
Yes. You pass the page URL to an HTML to PDF API, which loads the page in a real browser engine and exports the rendered result as a static PDF. The page needs to be publicly reachable, or you provide the raw HTML instead of a URL.
Does the PDF keep the page's CSS, fonts, and images?
Yes. Because the conversion runs inside a browser engine, the PDF preserves layout, fonts, images, and styling the same way the page looks in Chrome, as long as those assets are publicly reachable when the page is rendered.
Does the converter run the page's JavaScript?
A headless-browser-based converter executes JavaScript and waits for the page to finish rendering before it captures the PDF. This is why dynamic dashboards and single-page apps convert correctly, while a raw HTML string that never runs its scripts may not.
What is the difference between browser Print to PDF and an API?
Browser Print to PDF is manual and handles one page at a time, which is fine for occasional exports. An HTML to PDF API automates the same conversion from your backend, so you can generate thousands of documents on demand without anyone opening a browser.
Can I control page size, margins, and orientation?
Yes. PDFGate accepts options such as pageSizeType, margin, landscape, header, and footer, so you can match A4 or Letter sizing, add repeating headers and footers, and switch to landscape for wide reports.
Learn more about how this works on our HTML to PDF API page.