LoginSignup
35
36

More than 5 years have passed since last update.

node.jsでWebサイトのスクリーンショットを保存 その2

Posted at

以前、wkhtmltoimageを用いたWebサイトのスクリーンショットの保存を書いたのですが、バックエンドにphantom.jsを利用してさらにお手軽にできる方法があったのでメモ

node.jsならwebshotというモジュールを入れればいいだけです

screenshot.js
var webshot = require('webshot');

var options = {}; // サイトを参考に色々設定
webshot('<<URL>>', options, function(err, renderStream){
    var chunks = new Array();
    renderStream.on('data', function(data){
        chunks.push(new Buffer(data));
    });
    renderStream.on('error', function(e){
        console.log(e);
    });
    renderStream.on('end', function(){
        var data = Buffer.concat(chunks);
        // この時点でdataにスクリーンショットの画像データがバイナリで入ってる
    });
});

例によって、レンダリングまで時間がかかるものもあるのでそこは考慮すること

35
36
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
36