Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using phantomjs in a firebase function

I would like to use phantomjs' screen capture capability within a firebase function to return a PDF screenshot of a URL. Looking for examples of someone having done this.

like image 905
astrojams1 Avatar asked Apr 19 '18 02:04

astrojams1


1 Answers

I've used PhantomJS in Cloud Functions without major issues. The only concern was that I had to increase the memory for the containers, since the in-memory rendering turns out to be a memory hog.

I ended up using node-webshot, which is a wrapper around PhantomJS:

"node-webshot": "^1.0.2",

Capturing the actual screenshot was simple enough:

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();
  })
});
like image 150
Frank van Puffelen Avatar answered Oct 21 '22 03:10

Frank van Puffelen