Last active
December 28, 2022 22:27
-
-
Save guilt/0a48bc2a17b8b0b6ba4166e998ce50fc to your computer and use it in GitHub Desktop.
PhantomJS based HTML to PDF renderer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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