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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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.
pandoc is a great command-line tool for file format conversion.
The disadvantage is for PDF output, you’ll need LaTeX. The usage is
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).
wkhtmltopdf
to directly convert from html to pdf, without needing latex. see man pandoc
and search for wkhtmltopdf
or --pdf-engine
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.
weasyprint is an option. A possible drawback is that you'll need python on your machine.
Install:
pip install weasyprint
Convert:
weasyprint in.html out.pdf
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.
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 print spooler.
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.
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 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 '' +
'<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);