Skip to content

Instantly share code, notes, and snippets.

@guilt
Last active December 28, 2022 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilt/0a48bc2a17b8b0b6ba4166e998ce50fc to your computer and use it in GitHub Desktop.
Save guilt/0a48bc2a17b8b0b6ba4166e998ce50fc to your computer and use it in GitHub Desktop.
PhantomJS based HTML to PDF renderer
#!/bin/sh
set -e
HFILE=$1
[ -f "$HFILE" ] || { echo "HTML File not found."; exit 1; }
PFILE=$( echo "${HFILE}" | sed 's/.html/.pdf/g' )
TMPDIR=${TMPDIR:-${TEMP}}
TMPDIR=${TMPDIR:-${TMP}}
TMPDIR=${TMPDIR:-.}
which phantomjs || { echo "No PhantomJS."; exit 1; }
{
cat <<EOF
var sys = require('system');
var page = require('webpage').create();
page.settings.dpi = "60";
page.paperSize = {
format: 'letter',
orientation: 'portrait',
};
page.open(sys.args[1], function() {
setTimeout(function() {
page.render(sys.args[2]);
phantom.exit();
}, 25);
});
EOF
} > "${TMPDIR}/phantom-script.js"
phantomjs "${TMPDIR}/phantom-script.js" "${HFILE}" "${PFILE}"
rm -f "${TMPDIR}/phantom-script.js"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment