0

I was try to create a web screenshot with phantomjs,But alwaise it result a blank image.I am using Fedora OS.Here is my javascript.

screen.js

        var WebPage = require('webpage');
        page = WebPage.create();
        page.open('http://google.com');
        page.onLoadFinished = function() {
        page.render('googleScreenShot' + '.png');
        phantom.exit();}

And here is my php code

shot.php

     $exec = '/var/www/html/test/bin/phantomjs  /var/www/html/test/screen.js'; 
     $escaped_command = escapeshellcmd($exec);
     exec($escaped_command);

But the created image was blank.phantomjs have executre permission.Please help me

Shijin TR
  • 7,516
  • 10
  • 55
  • 122

3 Answers3

3

I am not sure but this may work,try it

    var WebPage = require('webpage');
    page = WebPage.create();
    page.open('http://google.com');
    page.onLoadFinished = function() {
            window.setTimeout(function () {
            page.render('googleScreenShot' + '.png');
            phantom.exit();
          }, 2000);
    }
esatilmis
  • 98
  • 10
  • yes but javascript working asycn. because of this it is unknown when work the render process so it can be work on windows.you have 2 choices first one is explained by @victory, the other choice is waitfor() method as exampled at this link : https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js – esatilmis Feb 19 '14 at 07:51
0

You have a problem because the time your code execute the next line of code is more faster than phantomjs logging that page.onLoadStarted is True.

easy and clean way to fix this is by adding a time interval of 100 ms between each function and make all your code in functions, check Vijay Boyapati method for more clarification : https://stackoverflow.com/a/9256079/1738230

Community
  • 1
  • 1
Skyliquid
  • 374
  • 1
  • 5
  • 23
0

In dealing with this problem I completed a code module, as follows:

link

You can download it locally, and then invoke it by command

phantomjs rasterize.js "http://www.google.com" 800px*800px > wait.html
luyishisi
  • 195
  • 11