Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhantomJS script returning blank web page

I'm using phantomJS to create PDFs in PHP via the shell (shell_exec). The script works fine on the production server, and also works fine if I plug the PhantomJS command directly into my terminal.

But it does not work when I run the script in my local dev environment. I'm wondering if there's a permissions issue involved. I won't dive into the specifics of my local environment for now since I'm guessing this is a high-level issue...

The command that is supposed to execute via shell_exec():

/usr/local/bin/phantomjs --ignore-ssl-errors=true --debug=true ../scripts/renderTeamProfile.js https://127.0.0.1/app_dev.php/pdf/enterprise-lpc-enterprise/profile/render /private/var/tmp/pjsK2N16E.pdf

The php code:

 public function pdfResponse($url, $script, $remote_filename)
 {
    $tempFile = tempnam('/tmp', 'pjs');
    // The extension specifies output format. Use pdf
    $tempFilePdf = $tempFile . '.pdf';
    rename($tempFile, $tempFilePdf);

    # nginx should restrict access to the localhost URL
    $urlLocal = preg_replace('/^https:..[^\/]+/', 'https://127.0.0.1', $url);

    $phantomJs = $this->container->getParameter('testsite.phantomjs_cmd');
    $command = $phantomJs.' --debug=true '.$script.' '.$urlLocal.' '.$tempFilePdf;
    $output = shell_exec($command);
    $content = file_get_contents($tempFilePdf);
    $response = new Response($content, 200);
    $response->headers->set('Content-Type', 'application/pdf');
    $response->headers->set('Content-Disposition',
    ('inline; filename="' .    $remote_filename . '"'));
    return $response;
}
like image 753
Ben Davidow Avatar asked Aug 24 '15 22:08

Ben Davidow


People also ask

How do I debug a PhantomJS script?

To debug inside a target web page requires two inspectors and a multi-step process using code like this: Instrument your PhantomJS script with two debugger; lines as in the example above. Start PhantomJS on the command line with the --remote-debugger-port=9000 option.

Why is PhantomJS not working on https?

Thus, if PhantomJS works well with HTTP but it shows some problem when using HTTPS, the first useful thing to check it whether the SSL libraries, usually OpenSSL, have been installed properly. On Windows, the default proxy setting may cause a massive network latency (see Known Issues in the release note).

Why are some PhantomJS examples not working in the terminal?

If some examples do not work, make sure that there is not more than one version of PhantomJS installed in the system. Multiple versions may lead to conflict as to which one is being invoked in the terminal.

How do I set custom encoding in PhantomJS?

As of PhantomJS 1.9, the open function can get an object of settings. and with a use of “encoding” key, you can set the custom encoding to your app. In this example, we’ve set the encoding to UTF8, and set the Content-Typeheader to application/jsonfor making our server know the request has information in jsonformat and not in urlencodedformat.


1 Answers

Turns out there was a different version of phantomjs installed locally vs. on the prod server. Interestingly, the bug occurred with the newer version (2.0.0) vs. 1.9.8.

like image 61
Ben Davidow Avatar answered Sep 18 '22 16:09

Ben Davidow