PhantomJS脚本返回空白网页 [英] PhantomJS script returning blank web page

查看:793
本文介绍了PhantomJS脚本返回空白网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用phantomJS通过外壳程序(shell_exec)在PHP中创建PDF.该脚本可以在生产服务器上正常运行,如果我将PhantomJS命令直接插入终端,也可以正常运行.

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...

应该通过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

php代码:

 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;
}

推荐答案

原来在本地安装了与生产服务器上不同版本的phantomjs.有趣的是,该错误发生在较新版本(2.0.0)与1.9.8之间.

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.

这篇关于PhantomJS脚本返回空白网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆