I have a HTML file that is using a style.css and has colors, ex.:

<font style="background-color: red; color: white;"> FOO </font>

How can I "export" this local file on my Ubuntu 12.04 to a PDF? (the look and color should stay the same). I tried ex.: Ctrl+P -> print to PDF, but it didn't preserve the colours. I tried htmldoc with the --color option.. but it's the same problem..

It would be great to do this via the command line.

  • Related: Web page to PDF converter
    – kenorb
    Commented Aug 24, 2017 at 11:54
  • Just open it in a browser and right click and save page as pdf
    – Harley
    Commented Aug 7, 2021 at 14:10
  • @Harley That option is not available on all browsers and all platforms. I'm an Ubuntu 18 and that option doesn't show up in Chrome or Firefox.
    – cazort
    Commented Jan 20, 2022 at 16:06
  • hmmm., ok, yeah I use Opera
    – Harley
    Commented Jan 21, 2022 at 23:08

7 Answers 7


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.

  • I think this can be automated on the command line as well with some efforts. :)
    – gertvdijk
    Commented Jul 15, 2013 at 7:08
  • @gertvdijk, That is beyond my competence. I've never used LibO via CLI.
    – user25656
    Commented Jul 15, 2013 at 7:14
  • There is this, wkhtmltopdf, as well but I have no experience of it: cyberciti.biz/open-source/…
    – user25656
    Commented Jul 15, 2013 at 7:16
  • Please post wkhtmltopdf as ANSWER, it resolved the problem Commented Jul 15, 2013 at 7:28
  • @gaskopeter, done as requested by editing the answer.
    – user25656
    Commented Jul 15, 2013 at 7:36

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.

  • 4
    On OS X: brew install Caskroom/cask/wkhtmltopdf.
    – kenorb
    Commented May 13, 2015 at 11:10
  • On Windows it is available on scoop or download the official binaries from the site Commented Aug 27, 2021 at 8:28
  • wkhtmltopdf doesnt work does not support css formatting Commented Apr 11 at 7:42

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!


pip install weasyprint


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 {
  padding: 0;
  • I have just tried to install weasypeasey but didn't work.. Maybe you could help? :) First I did this: apt-get install python-dev python-pip python-lxml libcairo2 libpango1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info and then installing pip with pip install weasypeasy but gets this error: Could not find any downloads that satisfy the requirement weasypeasy
    – clarkk
    Commented Nov 28, 2015 at 14:43
  • 1
    @clarkk my bad. It's actually weasyprint. So pip install weasyprint
    – Pithikos
    Commented Nov 29, 2015 at 20:04
  • 3
    weasyprint is good but about 15 times slower than wkhtmltopdf as I recall, so it was not suitable for us to generate reports on demand for our clients. wkhtmltopdf can be persuaded to do a good job even for complex reports... with some considerable effort! Commented Feb 17, 2016 at 13:36
  • Awesome it preserves the css and is awesome.. Commented Jul 17, 2016 at 13:41

Web2PDFConverter extension for Chromium or Chrome convert any webpage to PDF.

Or, you can just only use this site: http://pdfcrowd.com/. For local files: http://pdfcrowd.com/#convert_by_upload

  • using Firefox+cmd line would be better:P Commented Jul 15, 2013 at 7:19
  • @gaskopeter pdfcrowd.com/#convert_by_upload - open it in Firefox Commented Jul 15, 2013 at 7:21
  • math equations are not converted, one must use printable version
    – Adam
    Commented Sep 3, 2017 at 11:30
  • wow this pdfcrowd.com is superb it gives the best results Commented Apr 11 at 7:46

You may try to use PhantomJS and some code, example using rasterize.js:

phantomjs rasterize.js http://example.com/

Or use NodeJS html-pdf npm package (see GitHub, install via: npm install -g html-pdf) as suggested in the comment. Usage:

html-pdf http://example.com/ example.pdf
  • There is a ready to use NodeJS npm package: html-pdf — HTML to PDF converter that uses phantomjs.
    – ruvim
    Commented Aug 24, 2017 at 11:24
  • I could not install it with npm due to missing phantomjs-prebuild (which could not propperly be installed due to access rights, even with root) Commented Sep 20, 2017 at 16:41

I have tried WeasyPrint as was suggested by others. It does not convert well in many pages, and in some pages it just fails with an error.

The following Firefox addon works for me. Firefox 55. It says Windows only but it does work on Ubuntu.



Try Dompdf (check the source code at GitHub) which is an HTML to PDF converter. This library is very easy to use, and also very easy to install. Using composer you can set it up pretty fast.

Requirements: PHP 5.0+ (5.3+ recommended), DOM extension, GD extension

Example PHP code:

// somewhere early in your project's loading, require the Composer autoloader
// see: http://getcomposer.org/doc/00-intro.md
require 'vendor/autoload.php';
// disable DOMPDF's internal autoloader if you are using Composer
define('DOMPDF_ENABLE_AUTOLOAD', false);
// include DOMPDF's default configuration
require_once 'vendor/dompdf/dompdf/dompdf_config.inc.php';
$htmlString = '';
$htmlString .= ob_get_clean();
$dompdf = new DOMPDF();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .