Skip to content

Instantly share code, notes, and snippets.

@lorin
Last active April 3, 2018 21:03
Show Gist options
  • Save lorin/9556393869b59fb80bd2 to your computer and use it in GitHub Desktop.
Save lorin/9556393869b59fb80bd2 to your computer and use it in GitHub Desktop.
Convert svg to png with styling using phantomjs
#!/usr/bin/env phantomjs
/*
* Convert svg to png using PhantomJS
*
* Usage: png.js filename.svg filename.png
*
* Asses width and height attributes are present on the svg node
*
*/
var system = require('system');
var args = system.args;
if (args.length < 3) {
console.log("Usage: png.js filename.svg filename.png");
phantom.exit(1);
}
var page = require('webpage').create();
page.open(args[1], function() {
sz = page.evaluate(function() {
width = document.getElementsByTagName('svg')[0].getAttribute("width");
height = document.getElementsByTagName('svg')[0].getAttribute("height");
return {'width': width, 'height': height};
});
page.clipRect = { top: 0, left: 0, width: sz.width, height: sz.height };
page.render(args[2]);
phantom.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment