Quickly Convert Web Pages And HTML Files To PDF From Linux Command Line

It can be useful to directly create PDF documents from HTML files through the Linux command line.

HTMLDOC is a useful tool that does that. For using it in Debian based distros, first open Terminal and install it as follows (this example uses Linux Mint):

 sudo apt install htmldoc

installing htmldoc in Linux

Once installed, it can be directly used from the Terminal.

To convert an existing HTML file to PDF, the syntax is:

 htmldoc --webpage -f targetfile.pdf sourcefile.html

converting webpages and html files to PDF using  htmldoc in Linux

It is essential to use the –webpage parameter because HTMLDOC will then see it as an unstructured document without headings. The other HTML format is the book format which has all the headings like H1, H2 and so on. For this use the –book parameter instead of –webpage.

Once the conversion is complete, the PDF file will be available in the current directory.

converted webpage as a PDF document using htmldoc

You can also directly convert web URLs to PDF with this tool. Simply replace the source HTML file with that of the URL address:

convert web links to PDF using htmldoc in Linux

To set a user password that is required for opening the PDF file, add the –encryption and –user-password parameters. So the command will be:

 htmldoc --webpage --encryption --user-password password targetfile.pdf sourcehtmlfile.html

setting a password for converted PDF files using htmldoc

Once converted, a password will be needed to unlock and view the PDF file.

PDF file requiring password that was set during conversion using htmldoc

HTMLDOC has other parameters too like changing colors, fonts, etc. All of these can be viewed with the –help parameter.

 htmldoc --help

using the --help parameter to get a list of functions for htmldoc

Other than that, you can also check the man pages link:

 man htmldoc

using the man page for htmldoc

All done.

Comments are closed.