[Bash] HTML to PDF via wkhtmltopdf


Convert HTML files in directory to PDF recursively via wkhtmltopdf and Bash script. Please install wkhtmltopdf first.

#!/bin/bash

# $1 is the directory in which files to be processed
# $2 is the css file
for path in $(find $1 -name "*.html")
do
  echo -e "\033[92mProcessing ${path}\033[0m"
  wkhtmltopdf ${path} --disable-javascript --user-style-sheet $2 "${path%.html}.pdf"
done

If you get error message wkhtmltopdf: cannot connect to X server, see this question on Stack Overflow [3] for solution.


Tested on: Ubuntu Linux 16.04, wkhtmltopdf 0.12.2.4


References:

[1]
[2]
[3]xserver - wkhtmltopdf: cannot connect to X server - Stack Overflow