通过PHP在PhantomJs中打开多个html文件会引发语法错误

时间:2012-06-05 16:28:11

标签: php html apache syntax-error phantomjs

我希望我的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++;
}

1 个答案:

答案 0 :(得分:1)

您是否尝试将路径包装到报价中内爆后获得的文件?它看起来像是一个无效的参数,虽然错误本身很奇怪。