Liam Arbel
Liam Arbel

Reputation: 557

PhantomJS screenshot always blank

I have PhantomJS installed and example scripts from the official Phantom website are working fine.

I'm trying to get this screenshot example to work:

var page = require('webpage').create();
page.open('http://github.com/', function() {
  page.render('github.png');
  phantom.exit();
});

but all I keep getting is a blank github.png file weighing 582Kb

Any ideas?

Upvotes: 2

Views: 3862

Answers (3)

Dendeze
Dendeze

Reputation: 184

Had the same issue with this library: https://github.com/juliangruber/url-to-screenshot

setting the sslCertificatesPath('any') fixed the problem

new Screenshot('https://somerandomsite.com/')
  .width(320)
  .height(320)
  .sslCertificatesPath('any')
  .capture()
  .then(img => {
    // do stuff with img
  })

Upvotes: 0

Badacadabra
Badacadabra

Reputation: 8497

@ced-b's suggestion is wise, but I do not think your problem will be solved using a timeout because the real issue is most likely with SSL/TLS. GitHub is an HTTPS website. So if you want to get your screenshot, you may need to add the --ignore-ssl-errors=true option when you run your PhantomJS script. By default, you should receive a narrow screenshot, but you can of course set a larger viewport:

var page = require('webpage').create();

page.viewportSize = {
  width: 1920,
  height: 1080
};

page.open('https://github.com/', function () {
  page.render('github.png');
  phantom.exit();
});

Upvotes: 2

ced-b
ced-b

Reputation: 4065

You might have to wait for the page to actually load and render. Try adding a timeout:

var page = require('webpage').create();
page.open('http://github.com/', function() {
  setTimeout(function() {
     page.render('github.png');
     phantom.exit();
  }, 500);
});

Upvotes: 1

Related Questions