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.

up vote 33 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.

  • 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: cyberciti.biz/open-source/… – 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

http://wkhtmltopdf.org/

http://www.cyberciti.biz/open-source/html-to-pdf-freeware-linux-osx-windows-software/

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.

http://phantomjs.org

  • 3
    On OS X: brew install Caskroom/cask/wkhtmltopdf. – kenorb May 13 '15 at 11:10

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;
}
  • 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 '15 at 14:43
  • 1
    @clarkk my bad. It's actually weasyprint. So pip install weasyprint – Pithikos Nov 29 '15 at 20:04
  • 2
    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! – Sam Watkins Feb 17 '16 at 13:36
  • Awesome it preserves the css and is awesome.. – Aditya ultra Jul 17 '16 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

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 Aug 24 '17 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) – Martin Thoma Sep 20 '17 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.

FireShot

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:

<?php
// 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 = '';
ob_start();
include('html_to_dpf.html');
$htmlString .= ob_get_clean();


$dompdf = new DOMPDF();
$dompdf->load_html($htmlString);
$dompdf->render();
$dompdf->stream("sample.pdf");

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 (the association bonus does not count).

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.