Skip to content

Instantly share code, notes, and snippets.

@philfreo
Created May 30, 2017 23:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save philfreo/2874a3521622c697de44c1b4f015e4f8 to your computer and use it in GitHub Desktop.
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)
// 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);
@philfreo
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment