How to convert a HTML file to PDF (with colors)

Open your html file in LibreOffice Writer and then, under File in the menu, choose export to PDF. That's it.

If you prefer the command line, take a look at Convert HTML Page To a PDF Using Open Source Tool - Linux / OS X / Windows.

The software can be installed using sudo apt-get install wkhtmltopdf.


Webkit HTML to PDF: wkhtmltopdf:

sudo apt-get install wkhtmltopdf

Here a nixCraft tutorial (updated on 2017).

The latest version is headless (does not require X server).

Another possibility: phantomjs is a magic headless web browser, also based on webkit html. It can export a page as PDF among other things.


WeasyPrint seems promising. I tried wkhtmltopdf and although it renders things in an acceptable way, it doesn't render everything properly and it creates pdfs that take many seconds to open!

Install

pip install weasyprint

Run

weasyprint mypage.html out.pdf

As an extra it might be helpful to alter the CSS if you want to get the browser view and PDF to look identical.

/* For converting to PDF */
body {
  width: 210mm; /* A4 dimension */
}
@page {
  margin:0;
  padding: 0;
}

Tags:

Pdf

Convert