[Bash] HTML to PDF via wkhtmltopdf

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


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

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


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