The quickest way to convert an HTML file or a web page to PDF, without installing any program, is to choose “Save as PDF” in the browser’s print dialog. If you need to convert many pages automatically, use headless Chrome from the command line, or libraries such as Puppeteer, Playwright or WeasyPrint from code. Below we explain four methods step by step.
1. Converting HTML to PDF with a Browser (the Easiest)
- Open the HTML file in Chrome or Edge (you can drag the file into the browser window) or go to the web page you want to convert.
- Press Ctrl + P (Command + P on a Mac).
- Under Destination, choose Save as PDF.
- If needed, set the paper size, margins and Background graphics under More settings.
- Click Save.
Firefox and Safari also have a save-as-PDF option in the print dialog. If the page’s colours and backgrounds do not appear in the PDF, turn on the “Background graphics” option.
2. From the Command Line with Headless Chrome
Chrome can run without opening a window (headless) and print a page straight to PDF:
chrome --headless --print-to-pdf=output.pdf https://example.com/page.htmlOn Windows, use Chrome’s full path instead of chrome (for example "C:\Program Files\Google\Chrome\Application\chrome.exe"); on Linux, use google-chrome or chromium. For a local file, you can use an address in the form file:///C:/folder/page.html.
3. With Code: Puppeteer or Playwright (Node.js)
To generate documents such as invoices, reports or certificates automatically in your web application, Puppeteer’s page.pdf() function is widely used:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com/invoice.html', { waitUntil: 'networkidle0' });
await page.pdf({ path: 'invoice.pdf', format: 'A4', printBackground: true });
await browser.close();
})();Playwright has a function with the same name, page.pdf(); PDF generation works with the Chromium browser. To pass the HTML as text instead of loading it from an address, you can use page.setContent(html).
4. With Code: WeasyPrint (Python)
pip install weasyprint
weasyprint page.html output.pdfWeasyPrint converts HTML and CSS to PDF without a browser; it supports print-oriented CSS rules (page size, page numbers) well but does not run JavaScript. The repository of wkhtmltopdf, a tool seen in older projects, has been archived and is no longer developed; for new projects, the options above should be preferred.
Tips for a Good-Looking PDF
- Set the page size and margins with CSS:
@page { size: A4; margin: 15mm; } - Use
@media print { ... }for rules that apply only when printing; hide menus and buttons. - Add
break-inside: avoid;so that tables and images are not split across pages. - Wait for web fonts to load (in Puppeteer,
waitUntil: 'networkidle0'). - Do not upload documents containing confidential or personal information to unknown online converter sites.
To run PDF generation on the server, inside your application, you can use a VDS server with root access where you can install Node.js or Python.
Frequently Asked Questions
How do I convert an HTML page to PDF on a phone?
On Android, open the page in Chrome, tap the three dots > Share > Print and choose Save as PDF as the printer. On iPhone, tap the Share button in Safari and choose Print, then save the PDF to Files from the share menu.
Is it possible to convert PDF to HTML?
It is possible, but the result is usually not clean code that is suitable for editing, because PDF is a format designed for page layout. If you are going to reuse the content, copying the text and building the HTML from scratch usually gives a better result.
Categories
-
CentOS 7 guides
-
cPanel 20 guides
-
CyberPanel 18 guides
-
Operating Systems 81 guides
-
Juniper Networks 18 guides
-
Control Panels 50 guides
-
Linux 17 guides
-
MySQL 29 guides
-
Network 95 guides
-
Plesk Panel 12 guides
-
Virtual Servers 273 guides
-
SEO 3 guides
-
Cyber Security 8 guides
-
Technology 162 guides
-
VMware 36 guides
-
WordPress 20 guides
-
Artificial Intelligence 22 guides