M HYPE SPLASH
// news

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

By Abigail Rogers

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

<font> 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.

4

7 Answers

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.

7

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.

2

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;
}
4

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

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

3

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

phantomjs rasterize.js 

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

html-pdf example.pdf
2

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:
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");
0