Pipe generated javascript via commandline

We use PhantomJS to create a screenshot of a webpage. It is run from the commandline, because for some reason php-phantomjs and screen-master don’t work on our server. The solution is to run the commandline script with a PHP shell_exec() command.

PhantomJS uses a javascript file which tells what to do, which page to load, and where to write the file. As multiple users will use this functionality, I need to pass a user ID or something onto the javascript file, so it can load a user specific page and save the screenshot with a user ID in the name.

One option is to generate the javascript by PHP, and then use that. Take the following javascript:

var id = 1234;
var page = require('webpage').create();
page.viewportSize = { width: 1024, height: 768 };
page.clipRect = { top: 0, left: 0, width: 1024, height: 768 };
page.open('http://test.local/screenshot.php?id='+id, function(status) {
  console.log("Status: " + status);
  if(status === "success") {

The shell command is like this:

/path/to/phantomjs /var/www/test/js/phantom.js

The variable id has value 1234. This value is not fixed but changes per user. The javascript file is not dynamic, but can be generated using PHP. I could generate the javascript file, or just generate the script.

Can I pipe this generated script (or file) to PhantomJS via the commandline?

What other options do I have that may work better to get this working?

Source: shell

Leave a Reply