我希望我的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++;
}
最佳答案
您是否尝试过将引文内含在引号中的文件包装路径?尽管错误本身很奇怪,但对我来说这似乎是无效的参数。