SVG to PNG Server side - using node.js SVG to PNG Server side - using node.js node.js node.js

SVG to PNG Server side - using node.js


This may prove to be a useful answer to your question if you take out that "using node.js" stipulation. If it doesn't help you, maybe later visitors will find it interesting.

I've been working for some time to solve this same problem (server-side d3 rasterizing), and I've found PhantomJS to be the best solution.

server.js:

var page = require('webpage').create(),    renderElement = require('./renderElement.js'),    Routes = require('./Routes.js'),    app = new Routes();page.viewportSize = {width: 1000, height: 1000};page.open('./d3shell.html');app.post('/', function(req, res) {    page.evaluate(new Function(req.post.d3));    var pic = renderElement(page, '#Viewport');    res.send(pic);});app.listen(8000);console.log('Listening on port 8000.');

Routes.js: https://gist.github.com/3061477
renderElement.js: https://gist.github.com/3176500

d3shell.html should look something like:

<!DOCTYPE HTML><html><head>    <title>Shell</title></head><body>    <div id="Viewport" style="display: inline-block"></div>    <script src="http://cdnjs.cloudflare.com/ajax/libs/d3/2.8.1/d3.v2.min.js" type="text/javascript"></script></body></html>

You can then start the server with phantomjs server.js and POST d3=[d3 code that renders to #Viewport], and the server will respond with a base64-encoded png.

(Requires PhantomJS 1.7 or higher.)