Render HTML to an image

后端 未结 17 1796
栀梦
栀梦 2020-11-22 16:06

Is there a way to render html to image like PNG? I know that it is possible with canvas but I would like to render standard html element like div for example.

17条回答
  •  心在旅途
    2020-11-22 16:38

    Install phantomjs

    $ npm install phantomjs
    

    Create a file github.js with following code

    var page = require('webpage').create();
    //viewportSize being the actual size of the headless browser
    page.viewportSize = { width: 1024, height: 768 };
    page.open('http://github.com/', function() {
        page.render('github.png');
        phantom.exit();
    });
    

    Pass the file as argument to phantomjs

    $ phantomjs github.js
    

提交回复
热议问题