Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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.

share|improve this question

6 Answers 6

up vote 13 down vote accepted

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.

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

Webkit HTML to PDF:

sudo apt-get install wkhtmltopdf

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.

share|improve this answer
On OS X: brew install Caskroom/cask/wkhtmltopdf. – kenorb May 13 at 11:10

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

Or, you can just only use this site: For local files:

share|improve this answer
using Firefox+cmd line would be better:P – gasko peter Jul 15 '13 at 7:19
@gaskopeter - open it in Firefox – Radu Rădeanu Jul 15 '13 at 7:21

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

phantomjs rasterize.js
share|improve this answer

Try Dompdf (check out at GitHub page or source code) 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:
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/';

$htmlString = '';
$htmlString .= ob_get_clean();

$dompdf = new DOMPDF();
share|improve this answer
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – David Foerster Nov 14 '14 at 3:58

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;
share|improve this answer
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 Nov 28 at 14:43
@clarkk my bad. It's actually weasyprint. So pip install weasyprint – Pithikos 2 days ago
Thanks.. Found out myself :) Looks like a great tool – clarkk yesterday

protected by Community Dec 9 '14 at 7:49

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.