PhantomJS 脚本返回空白网页
PhantomJS script returning blank web page
我正在使用 phantomJS 通过 shell (shell_exec
) 在 PHP 中创建 PDF。该脚本在生产服务器上运行良好,如果我将 PhantomJS 命令直接插入我的终端也能正常运行。
但是当我在我的本地开发环境中 运行 脚本时它不起作用。我想知道是否涉及权限问题。我现在不会深入了解本地环境的细节,因为我猜这是一个高级问题...
应该通过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 中。
我正在使用 phantomJS 通过 shell (shell_exec
) 在 PHP 中创建 PDF。该脚本在生产服务器上运行良好,如果我将 PhantomJS 命令直接插入我的终端也能正常运行。
但是当我在我的本地开发环境中 运行 脚本时它不起作用。我想知道是否涉及权限问题。我现在不会深入了解本地环境的细节,因为我猜这是一个高级问题...
应该通过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 中。