Sie sind auf Seite 1von 5

11/13/2019 How to convert a HTML file to PDF (with colors) - Ask Ubuntu

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


Asked 6 years, 4 months ago Active 1 year, 11 months ago Viewed 112k times

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

30 <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..
9
It would be great to do this via the command line.

pdf convert

edited Jul 15 '13 at 7:10 asked Jul 15 '13 at 6:51


Karthik C gasko peter
1,979 5 16 24 302 1 5 9

Related: Web page to PDF converter – kenorb Aug 24 '17 at 11:54

7 Answers

Open your html file in LibreOffice Writer and then, under File in the menu, choose export to
PDF . That's it.
36
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 .

edited Jul 15 '13 at 7:36 answered Jul 15 '13 at 7:05


user25656

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

By using our There


site, you acknowledge
is this, thatasyou
wkhtmltopdf, wellhave
but Iread
haveand understandof
no experience our
it: Cookie Policy, Privacy Policy–, user25656
cyberciti.biz/open-source/… and our Jul
15 '13
Terms of Service . at 7:16

https://askubuntu.com/questions/320195/how-to-convert-a-html-file-to-pdf-with-colors 1/5
11/13/2019 How to convert a HTML file to PDF (with colors) - Ask Ubuntu
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:

16 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

edited Nov 11 '15 at 15:19 answered May 20 '14 at 4:16


kidmose Sam Watkins
143 1 4 262 2 8

4 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!
10
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;
By using ourpadding: 0;
site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
}
Terms of Service.

https://askubuntu.com/questions/320195/how-to-convert-a-html-file-to-pdf-with-colors 2/5
11/13/2019 How to convert a HTML file to PDF (with colors) - Ask Ubuntu
edited Nov 29 '15 at 20:05 answered Nov 22 '15 at 20:51
Pithikos
731 1 7 16

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

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! – 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:
3 http://pdfcrowd.com/#convert_by_upload

answered Jul 15 '13 at 7:18


Radu Rădeanu
129k 37 273 339

using Firefox+cmd line would be better:P – gasko peter Jul 15 '13 at 7:19

@gaskopeter pdfcrowd.com/#convert_by_upload - open it in Firefox – Radu Rădeanu Jul 15 '13 at 7:21

math equations are not converted, one must use printable version – Adam Sep 3 '17 at 11:30

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

2 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

edited Aug 24 '17 at 11:48 answered May 20 '15 at 14:35


kenorb
5,630 1 45 68
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.
There is a ready to use NodeJS npm package: html-pdf — HTML to PDF converter that uses phantomjs. –
https://askubuntu.com/questions/320195/how-to-convert-a-html-file-to-pdf-with-colors 3/5
11/13/2019 How to convert a HTML file to PDF (with colors) - Ask Ubuntu
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.
2 The following Firefox addon works for me. Firefox 55. It says Windows only but it does work on
Ubuntu.

FireShot

answered Oct 14 '17 at 21:24


rapt
238 2 15

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.
1 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");

edited Dec 10 '17 at 15:16 answered Nov 14 '14 at 2:20


kenorb Jeff
5,630 1 45 68 21 1
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.

https://askubuntu.com/questions/320195/how-to-convert-a-html-file-to-pdf-with-colors 4/5
11/13/2019 How to convert a HTML file to PDF (with colors) - Ask Ubuntu

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?

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.

https://askubuntu.com/questions/320195/how-to-convert-a-html-file-to-pdf-with-colors 5/5

Das könnte Ihnen auch gefallen