Skip to content

Instantly share code, notes, and snippets.

@vbarbarosh
Last active April 5, 2019 18:34
Show Gist options
  • Select an option

  • Save vbarbarosh/a291ebf78a093c415ba4b189219a7a3d to your computer and use it in GitHub Desktop.

Select an option

Save vbarbarosh/a291ebf78a093c415ba4b189219a7a3d to your computer and use it in GitHub Desktop.
shell_docker_browserless_htmlpdf – Convert html to pdf using Browserless https://codescreens.com
#!/bin/bash
# http://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -o nounset -o errexit -o pipefail
# usage: cat input.html | ./shell_docker_browserless_htmlpdf > a.pdf
html=`base64 -w0`
docker run --rm -i browserless/chrome bash << EOF
node build/index.js &
sleep 1
node > a << 'EOF2'
const code = \`
module.exports = async function (v) {
await v.page.setContent(v.context.html);
return {data: await v.page.pdf()};
};
\`;
const context = {html: new Buffer('$html', 'base64').toString()};
console.log(JSON.stringify({code, context}));
EOF2
curl 127.0.0.1:3000/function -H Content-Type:application/json \
-o /dev/stdout -sf -d@a
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment