SVG to PNG Server side - using node.js

前端 未结 1 1741
无人共我
无人共我 2020-12-07 10:56

I\'m trying to follow this tutorial on converting a d3.js SVG Vis to a PNG server-side (using Node.js) http://eng.wealthfront.com/2011/12/converting-dynamic-svg-to-png-with.

相关标签:
1条回答
  • 2020-12-07 11:07

    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.)

    0 讨论(0)
提交回复
热议问题