我希望我的php脚本从命令行调用phantomjs,这将从许多html文件中生成多个pdf。所以我有一个带有 html 文件路径的串联字符串,接下来我调用exec
命令:
$myFile1 = dirname(__FILE__)."/testFile0.html";
$myFile2 = dirname(__FILE__)."/testFile1.html";
$myFile3 = dirname(__FILE__)."/testFile2.html";
$files = array($myFile1, $myFile2, $myFile3);
$command = 'phantomjs '.dirname(__FILE__).'/render.js '.implode('|', $files);
exec($command, $phantomOut);
echo print_r($phantomOut);
当我调用"phantomjs render.js"file1.html|file2.html"时,一切正常。但是,当尝试从 php 脚本执行此操作时,我在 apache error_log 中收到以下错误:
testFile1.html: line 1: syntax error near unexpected token `<'
testFile1.html: line 1: `<!DOCTYPE HTML...
我正在使用的HTML文件看起来不错,所以我不知道是什么会导致这种情况。在浏览器中运行此脚本时的输出是:Array ( ) 1
而不是我在命令行中获取的文本。
下面是PhantomJs渲染脚本:
var page = require('webpage').create(),
addresses = phantom.args[0],
outputPath = '/path_to_pdf/',
outputFilename,
filesArray, outputArray = [],
loadInProgress = false,
pageIndex = 0,
interval;
page.viewportSize = { width: 600, height: 600};
if(addresses.indexOf('|') !== -1){
filesArray = addresses.split('|');
} else{
filesArray = [addresses];
}
interval = setInterval(function() {
if (!loadInProgress && pageIndex < filesArray.length) {
page.open(filesArray[pageIndex]);
}
if (pageIndex === filesArray.length) {
console.log('OUTPUT: ', outputArray.join('|'));
phantom.exit();
}
}, 250);
page.onLoadStarted = function() {
loadInProgress = true;
};
page.onLoadFinished = function() {
loadInProgress = false;
outputFilename = 'print'+pageIndex+'.pdf';
page.render(outputPath+outputFilename);
outputArray.push(outputFilename);
pageIndex++;
}
您是否尝试过将路径包装到引号内爆后获得的那些文件?对我来说,它看起来像一个无效的参数,尽管错误本身很奇怪。