Created
May 30, 2017 23:21
-
-
Save philfreo/2874a3521622c697de44c1b4f015e4f8 to your computer and use it in GitHub Desktop.
PhantomJS program to generate a PNG based on stdin (e.g. SVG image)
This file contains hidden or 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
// PhantomJS program to generate a PNG based on stdin (e.g. SVG image) | |
// Example: curl http://upload.wikimedia.org/wikipedia/commons/f/fd/Ghostscript_Tiger.svg | phantomjs png.js > test.png && open test.png | |
/* | |
From Flask/Python: | |
import os | |
from subprocess import Popen, PIPE, STDOUT | |
p = Popen(['phantomjs', '%s/png.js' % os.path.dirname(os.path.realpath(__file__))], stdout=PIPE, stdin=PIPE, stderr=STDOUT) | |
out = p.communicate(input=svg.encode('utf-8'))[0] | |
strIO = StringIO.StringIO() | |
strIO.write(out) | |
strIO.seek(0) | |
return send_file(strIO, as_attachment=True, attachment_filename='file.png') | |
*/ | |
var page = require('webpage').create(), | |
system = require('system'), | |
fs = require('fs'); | |
page.content = fs.read('/dev/stdin'); | |
window.setTimeout(function() { | |
page.render('/dev/stdout', { format: 'png' }); | |
phantom.exit(); | |
}, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For PDFs instead of PNGs, see https://gist.github.com/philfreo/5854629 and https://gist.github.com/philfreo/44e2e26a65820497db234d0c66ed58ae