我正在尝试使用 Codeigniter、PhantomJS 创建网页的 pdf,我正在使用 Linux 操作系统。我有一个带有 exec() 的 phantom_helper 文件。我从 here 得到了一些代码.当我运行 phantomjs/var/www/PhantomJS/js/rasterize.js http://google.com/var/www/PhantomJS/pdf/Test.pdf
时,我能够创建 pdf 文件在 linux 终端窗口。我在这里添加我拥有的代码,
Controller :
public function index()
{
$this->load->helper('phantomjs');
$this->load->helper('url');
$viewbox['generationStatus'] = 'PDF Generation successful';
$url = 'http://google.com';
$filename = strtotime(date('Y-m-d H:i:s')).'.pdf';
$resp = rasterize_wrapper($url,$filename);
$viewbox['filename'] = $resp;
if($resp == 0)
{
$viewbox['filename'] = '';
$viewbox['generationStatus'] = 'PDF Generation failed';
}
$this->load->view('welcome_message',$viewbox);
}
Phanthom_helper:
if ( ! function_exists('rasterize_wrapper'))
{
function rasterize_wrapper($url='', $output=''){
if($url=='' || $output=='')
{
show_error('URL or Output file name not defined correctly');
log_message('error','rasterize_wrapper: not initialized');
exit;
}
$url = escapeshellcmd($url);
exec('phantomjs '.realpath('js/rasterize.js').' '.$url.' '.realpath('pdf').'/'.$output,$output_status, $return_status);
if($return_status == '0'){ return $output;}
return 0;
}
}
光栅化.js
var page = new WebPage();
if (phantom.args.length < 2 || phantom.args.length > 3) {
console.log('Usage: rasterize.js URL filename');
phantom.exit();
} else {
address = phantom.args[0];
output = phantom.args[1];
page.viewportSize = { width: 600, height: 600 };
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 200);
}
});
}
最佳答案
我的问题已经解决了。这段代码确实有效。问题是因为我使用 Linux,所以我必须为 pdf 保存文件夹设置文件夹权限。我已经设置好并获得了pdf。 Css 问题也可以通过执行 MEDIA QUERY @media print {....}
Ty 来解决。
关于php - PhantomJS 不适用于 codeigniter + exec(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33563465/