{"id":14967,"date":"2024-11-13T02:47:31","date_gmt":"2024-11-12T23:47:31","guid":{"rendered":"https:\/\/www.sunucun.com.tr\/blog\/?post_type=dt_articles&#038;p=14967"},"modified":"2026-11-15T22:52:00","modified_gmt":"2026-11-15T19:52:00","slug":"how-to-convert-html-to-pdf","status":"publish","type":"post","link":"https:\/\/sunucun.com.tr\/blog\/how-to-convert-html-to-pdf\/","title":{"rendered":"How to Convert HTML to PDF: Browser, Command Line and Code (4 Methods)"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_90 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/sunucun.com.tr\/blog\/how-to-convert-html-to-pdf\/#1_Converting_HTML_to_PDF_with_a_Browser_the_Easiest\" >1. Converting HTML to PDF with a Browser (the Easiest)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/sunucun.com.tr\/blog\/how-to-convert-html-to-pdf\/#2_From_the_Command_Line_with_Headless_Chrome\" >2. From the Command Line with Headless Chrome<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/sunucun.com.tr\/blog\/how-to-convert-html-to-pdf\/#3_With_Code_Puppeteer_or_Playwright_Nodejs\" >3. With Code: Puppeteer or Playwright (Node.js)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/sunucun.com.tr\/blog\/how-to-convert-html-to-pdf\/#4_With_Code_WeasyPrint_Python\" >4. With Code: WeasyPrint (Python)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/sunucun.com.tr\/blog\/how-to-convert-html-to-pdf\/#Tips_for_a_Good-Looking_PDF\" >Tips for a Good-Looking PDF<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/sunucun.com.tr\/blog\/how-to-convert-html-to-pdf\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/sunucun.com.tr\/blog\/how-to-convert-html-to-pdf\/#How_do_I_convert_an_HTML_page_to_PDF_on_a_phone\" >How do I convert an HTML page to PDF on a phone?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/sunucun.com.tr\/blog\/how-to-convert-html-to-pdf\/#Is_it_possible_to_convert_PDF_to_HTML\" >Is it possible to convert PDF to HTML?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">The quickest way to convert an HTML file or a web page to PDF, without installing any program, is to choose <strong>&#8220;Save as PDF&#8221; in the browser&#8217;s print dialog<\/strong>. If you need to convert many pages automatically, use <strong>headless Chrome<\/strong> from the command line, or libraries such as <strong>Puppeteer, Playwright or WeasyPrint<\/strong> from code. Below we explain four methods step by step.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Converting_HTML_to_PDF_with_a_Browser_the_Easiest\"><\/span>1. Converting HTML to PDF with a Browser (the Easiest)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>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.<\/li>\n<li>Press <strong>Ctrl + P<\/strong> (<strong>Command + P<\/strong> on a Mac).<\/li>\n<li>Under <strong>Destination<\/strong>, choose <strong>Save as PDF<\/strong>.<\/li>\n<li>If needed, set the paper size, margins and <strong>Background graphics<\/strong> under <strong>More settings<\/strong>.<\/li>\n<li>Click <strong>Save<\/strong>.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Firefox and Safari also have a save-as-PDF option in the print dialog. If the page&#8217;s colours and backgrounds do not appear in the PDF, turn on the &#8220;Background graphics&#8221; option.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_From_the_Command_Line_with_Headless_Chrome\"><\/span>2. From the Command Line with Headless Chrome<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Chrome can run without opening a window (headless) and print a page straight to PDF:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chrome --headless --print-to-pdf=output.pdf https:\/\/example.com\/page.html<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On Windows, use Chrome&#8217;s full path instead of <code>chrome<\/code> (for example <code>\"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\"<\/code>); on Linux, use <code>google-chrome<\/code> or <code>chromium<\/code>. For a local file, you can use an address in the form <code>file:\/\/\/C:\/folder\/page.html<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_With_Code_Puppeteer_or_Playwright_Nodejs\"><\/span>3. With Code: Puppeteer or Playwright (Node.js)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To generate documents such as invoices, reports or certificates automatically in your web application, Puppeteer&#8217;s <code>page.pdf()<\/code> function is widely used:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const puppeteer = require('puppeteer');\n(async () =&gt; {\n  const browser = await puppeteer.launch();\n  const page = await browser.newPage();\n  await page.goto('https:\/\/example.com\/invoice.html', { waitUntil: 'networkidle0' });\n  await page.pdf({ path: 'invoice.pdf', format: 'A4', printBackground: true });\n  await browser.close();\n})();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Playwright has a function with the same name, <code>page.pdf()<\/code>; PDF generation works with the Chromium browser. To pass the HTML as text instead of loading it from an address, you can use <code>page.setContent(html)<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_With_Code_WeasyPrint_Python\"><\/span>4. With Code: WeasyPrint (Python)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install weasyprint\nweasyprint page.html output.pdf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">WeasyPrint 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 <strong>wkhtmltopdf<\/strong>, a tool seen in older projects, has been archived and is no longer developed; for new projects, the options above should be preferred.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Tips_for_a_Good-Looking_PDF\"><\/span>Tips for a Good-Looking PDF<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set the page size and margins with CSS: <code>@page { size: A4; margin: 15mm; }<\/code><\/li>\n<li>Use <code>@media print { ... }<\/code> for rules that apply only when printing; hide menus and buttons.<\/li>\n<li>Add <code>break-inside: avoid;<\/code> so that tables and images are not split across pages.<\/li>\n<li>Wait for web fonts to load (in Puppeteer, <code>waitUntil: 'networkidle0'<\/code>).<\/li>\n<li>Do not upload documents containing confidential or personal information to unknown online converter sites.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To run PDF generation on the server, inside your application, you can use a <a href=\"https:\/\/sunucun.com.tr\/en\/vds-server\">VDS server<\/a> with root access where you can install Node.js or Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_do_I_convert_an_HTML_page_to_PDF_on_a_phone\"><\/span>How do I convert an HTML page to PDF on a phone?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">On Android, open the page in Chrome, tap the three dots &gt; <strong>Share &gt; Print<\/strong> and choose <strong>Save as PDF<\/strong> as the printer. On iPhone, tap the <strong>Share<\/strong> button in Safari and choose <strong>Print<\/strong>, then save the PDF to Files from the share menu.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Is_it_possible_to_convert_PDF_to_HTML\"><\/span>Is it possible to convert PDF to HTML?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>The quickest way to convert an HTML file or a web page to PDF, without installing any program, is to choose &#8220;Save as PDF&#8221; in the browser&#8217;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&hellip;<\/p>\n","protected":false},"author":1,"featured_media":14964,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[1521],"tags":[1527],"class_list":["post-14967","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-teknoloji","tag-teknoloji"],"_links":{"self":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/14967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/comments?post=14967"}],"version-history":[{"count":2,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/14967\/revisions"}],"predecessor-version":[{"id":18491,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/14967\/revisions\/18491"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/media\/14964"}],"wp:attachment":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/media?parent=14967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/categories?post=14967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/tags?post=14967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}