Beruflich Dokumente
Kultur Dokumente
I have a HTML file that is using a style.css and has colors, ex.:
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
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.
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
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
http://wkhtmltopdf.org/
http://www.cyberciti.biz/open-source/html-to-pdf-freeware-linux-osx-windows-software/
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
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
Run
As an extra it might be helpful to alter the CSS if you want to get the browser view and PDF to look
identical.
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
Or, you can just only use this site: http://pdfcrowd.com/. For local files:
3 http://pdfcrowd.com/#convert_by_upload
using Firefox+cmd line would be better:P – gasko peter Jul 15 '13 at 7:19
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:
Or use NodeJS html-pdf npm package (see GitHub, install via: npm install -g html-pdf ) as
suggested in the comment. Usage:
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
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
<?php
// somewhere early in your project's loading, require the Composer autoloader
// see: http://getcomposer.org/doc/00-intro.md
require 'vendor/autoload.php';
$htmlString = '';
ob_start();
include('html_to_dpf.html');
$htmlString .= ob_get_clean();
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
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