7

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.

astrojams1
  • 1,523
  • 2
  • 15
  • 29
  • For a project I work on, we wanted to run headless Chrome in a FF. Couldn't get it working. Ended up running headless Chrome using AWS Lambda, which was much easier (several projects exist to facilitate this). This is the first time I know of someone running HC in a Google cloud function: https://stackoverflow.com/a/49522268/7486612 (and it required a custom compilation of Chrome) – Sidney Apr 19 '18 at 03:15
  • 1
    Headless Chrome won't work (at least not without major work), since some drivers that it requires are missing in the environment that Cloud Functions execute on. But I've used Phantom.js in one of my Cloud Functions without problems – Frank van Puffelen Apr 19 '18 at 03:51

1 Answers1

6

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();
  })
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I tried your solution but it doesn't work for me. The only difference is that I used onCall instead of onRequest. I have no errors my stream.on('data') is never called – xikimay Dec 04 '18 at 16:31
  • I do have an error 'PhantomJS exited' Not sure if I can do something about it – xikimay Dec 04 '18 at 16:48
  • Unfortunately that doesn't give me a lot to go on. All I can say is that I've definitely used this code in production before posting it here. – Frank van Puffelen Dec 04 '18 at 18:47