You are on page 1of 5

07/02/2024, 22:12 Is there a command-line tool for converting html files to pdf?

- Unix & Linux Stack Exchange

Is there a command-line tool for converting html files to


pdf? [duplicate]
Asked 4 years, 6 months ago Modified 3 years, 11 months ago Viewed 98k times

This question already has answers here:


convert a html book to a pdf file? (7 answers)
49
Closed 4 years ago.

I would like to install a command line tool within a Docker image in order to quickly convert
*html files into *pdf files.

I am surprised there is not a Unix tool to do something like this.

command-line pdf software-rec conversion html

Share Improve this question edited Aug 5, 2019 at 16:22 asked Aug 5, 2019 at 2:24
Follow terdon ♦ EB2127
240k 66 456 683 673 2 6 7

2 @muru It's arguable a duplicate, though (A) I'm looking for a command line tool to put in a Docker
image and (B) the answers below are quite useful and more helpful that the posting above from 2015.
I've edited the question to clarify this somewhat, and I'm happy to edit again. – EB2127 Aug 5, 2019
at 4:26

2 Yes, this question is focused on command line tools while the other isn't and also, the other requires a
more complex solution since it's about converting multiple, linked html documents. I don't think it's a
dupe. – terdon ♦ Aug 5, 2019 at 16:21

1 html2pdf – Barmar Aug 5, 2019 at 21:06

1 this should probably be on Software Recommendations – phuclv Aug 6, 2019 at 16:47

@phuclv This is a good point. I didn't know this existed. – EB2127 Aug 6, 2019 at 22:20

6 Answers Sorted by: Highest score (default)

pandoc is a great command-line tool for file format conversion.

The disadvantage is for PDF output, you’ll need LaTeX. The usage is
42
https://unix.stackexchange.com/questions/533886/is-there-a-command-line-tool-for-converting-html-files-to-pdf 1/5
07/02/2024, 22:12 Is there a command-line tool for converting html files to pdf? - Unix & Linux Stack Exchange

pandoc test.html -t latex -o test.pdf

If you don't have LaTeX installed, then I recommend htmldoc.

Cited from Creating a PDF

By default, pandoc will use LaTeX to create the PDF, which requires that a LaTeX
engine be installed.

Alternatively, pandoc can use ConTeXt, pdfroff, or any of the following HTML/CSS-to-
PDF-engines, to create a PDF: wkhtmltopdf, weasyprint or prince. To do this, specify
an output file with a .pdf extension, as before, but add the --pdf-engine option or -t
context, -t html, or -t ms to the command line (-t html defaults to --pdf-
engine=wkhtmltopdf).

Share Improve this answer edited Jun 11, 2020 at 14:16 answered Aug 5, 2019 at 3:02
Follow Community Bot ComplicatedPhenomeno
1 n
571 3 9

11 +1. pandoc can also use wkhtmltopdf to directly convert from html to pdf, without needing latex.
see man pandoc and search for wkhtmltopdf or --pdf-engine – cas Aug 5, 2019 at 4:15

1 @cas This is really useful. Could you answer the question with that command? I would like to keep
this answer – EB2127 Aug 5, 2019 at 4:28

@EB2127 Stack Exchange answers can easily contain more than one solution to a problem;
collaborative editing can/should make any answer better. – Jeff Schaller ♦ Aug 6, 2019 at 11:05

@cas Unfortunately wkhtmltopdf complains about QXcbConnection: Could not connect to


display localhost:12.0 and dumps core. I suspect if I figure out the display issue, then it will work
but not sure why it cares about the display. – steveb Mar 12, 2020 at 23:19

What advantage is there to using pandoc with the WeasyPrint engine vs just using WeasyPrint without
the dependency on pandoc? – Hashim Aziz Jun 4, 2020 at 23:12

You can also try wkhtmltopdf, usage and installation is pretty straightforward.

31 Share Improve this answer Follow answered Aug 5, 2019 at 12:01


guitarman
419 3 2

It doesn't support CSS3 :( – Déjà vu Apr 3, 2022 at 11:15

https://unix.stackexchange.com/questions/533886/is-there-a-command-line-tool-for-converting-html-files-to-pdf 2/5
07/02/2024, 22:12 Is there a command-line tool for converting html files to pdf? - Unix & Linux Stack Exchange

oh, that's unfortunate github.com/wkhtmltopdf/wkhtmltopdf/issues/5063 – guitarman Apr 4, 2022 at


19:23

1 No longer actively developed (repo archived) :( – BallpointBen Mar 22, 2023 at 21:27

No longer in development. – liftarn Nov 21, 2023 at 14:36

weasyprint is an option. A possible drawback is that you'll need python on your machine.

Install:
20
pip install weasyprint

Convert:

weasyprint in.html out.pdf

Share Improve this answer edited Mar 13, 2020 at 1:11 answered Aug 5, 2019 at 6:47
Follow steveb shiftas
183 5 326 1 6

2 All distributions are shipped with Python . – Paradox Aug 6, 2019 at 3:57

4 Sure, but there are custom linux systems, on embedded devices for example, that might not have
python. – shiftas Aug 7, 2019 at 6:42

Tried it, but it ignores # in url. e.g. "status.aws.amazon.com/#AP_Block" converts the wrong tab to pdf
– Pieter Aug 22, 2021 at 10:15

I've been successfully using the 1.8 branch of HTMLDOC for years. I put it in a commercial
system that has since generated hundreds of thousands of reports since 2003.
3
It's not super-versatile, but it is very efficient and reliable. It's limited to a basic set of
postscript fonts.

It does not support CSS, but instead uses a special HTML comment directive set to control
PDF specific aspects.

The source code is not too difficult to read and edit if you need to add custom facilities, if
you're comfortable with C. It is compiled with GCC or Visual Studio, depending on your target
platform.

Note that the HTML does not need to be in a file. You can generate it dynamically from a
URL, php or aspx etc. You can also hook it up in your web server for generate a PDF file
dynamically.

In my use case it generates a PDF file from an asp page which then gets attached to an
email, instead of sending the HTML to the printer and the letter stuffing machine; it's a kind of
https://unix.stackexchange.com/questions/533886/is-there-a-command-line-tool-for-converting-html-files-to-pdf 3/5
07/02/2024, 22:12 Is there a command-line tool for converting html files to pdf? - Unix & Linux Stack Exchange

print spooler.

Share Improve this answer edited Aug 5, 2019 at 23:19 answered Aug 5, 2019 at 23:03
Follow birdwes
27 4

1 Indeed, a small and usefull tool, with lots of features. Thank you for sharing! – Andrei B Apr 29, 2020 at
11:54

Absolutely useful tool HTMLDOC, we have done some custom changes and have been using it for
almost 2 decades. The main issue with HTMLDOC is not supporting CSS and SVG. I tried many open-
source options but had no success, at one point in time WKHTMLTOPDF was promising but now the
project is archived in 2020, and there are more than 1.3K issues open. I think good dev support will be
required to revive this project. I still believe that WK can be a very good substitute for HTMLDOC.
Thanks! – ppant Apr 5, 2023 at 11:38

There is also an html2ps program, and you could then easily convert the PostScript file to pdf.
I used this several years ago, and IIRC it did a pretty good job on a large manual.
3
Share Improve this answer edited Aug 6, 2019 at 15:26 answered Aug 6, 2019 at 3:46
Follow Jeff Schaller ♦ jamesqf
67k 35 116 254 185 6

PhantomJS can do the job for you. It has command line functionality and works out of the
box. You'll be required to write a simple Javascript function to tell it what to do. The site has a
2 quick start guide and there are plenty of articles online to assist you. Usage is generally as
follows:

phantomjs configFile.js htmlFile.html output.pdf

Here is a sample script to generate an A4 portrait PDF taken from here, save as your
configFile.js

var page = require('webpage').create(),


system = require('system'),
fs = require('fs');

page.paperSize = {
format: 'A4',
orientation: 'portrait',
margin: {
top: "1.5cm",
bottom: "1cm"
},
footer: {
height: "1cm",
contents: phantom.callback(function (pageNum, numPages) {
return '' +

https://unix.stackexchange.com/questions/533886/is-there-a-command-line-tool-for-converting-html-files-to-pdf 4/5
07/02/2024, 22:12 Is there a command-line tool for converting html files to pdf? - Unix & Linux Stack Exchange
'<div style="margin: 0 1cm 0 1cm; font-size: 0.65em">' +
' <div style="color: #888; padding:20px 20px 0 10px; border-
top: 1px solid #ccc;">' +
' <span>REPORT FOOTER</span> ' +
' <span style="float:right">' + pageNum + ' / ' + numPages
+ '</span>' +
' </div>' +
'</div>';
})
}
};

page.settings.dpi = "96";

page.content = fs.read(system.args[1]);

var output = system.args[2];

window.setTimeout(function () {
page.render(output, {format: 'pdf'});
phantom.exit(0);
}, 2000);

Share Improve this answer edited Aug 7, 2019 at 10:56 answered Aug 6, 2019 at 15:21
Follow Kusalananda ♦ The Betpet
330k 37 664 962 121 4

No longer in development. – liftarn Nov 21, 2023 at 14:36

https://unix.stackexchange.com/questions/533886/is-there-a-command-line-tool-for-converting-html-files-to-pdf 5/5

You might also like