在firebase函数中使用phantomjs

ast*_*ms1 7 phantomjs firebase google-cloud-functions

我想在firebase函数中使用phantomjs的屏幕捕获功能来返回URL的PDF屏幕截图.寻找有人这样做的例子.

Fra*_*len 6

我在云函数中使用了PhantomJS而没有出现重大问题.唯一的问题是我不得不增加容器的内存,因为内存中的渲染结果是内存耗尽.

我最终使用了node-webshot,这是PhantomJS的包装:

"node-webshot": "^1.0.2",
Run Code Online (Sandbox Code Playgroud)

捕获实际的屏幕截图非常简单:

const functions = require('firebase-functions');
const webshot = require('node-webshot');

exports.screenshotTweet = functions.https.onRequest((req, res) => {
  var stream = webshot(req.query.url);
  res.writeHead(200, {'Content-Type': 'image/jpeg'});
  stream.on('data', function(data) {
    res.write(data.toString('binary'), 'binary');
  });
  stream.on('end', function() {
    res.end();
  })
});
Run Code Online (Sandbox Code Playgroud)