我正在尝试从动态 Handlebars 网页生成一个pdf文件,该文件将显示数据库中的数据。一旦在本地计算机上运行该代码,我便希望将代码移至弹性beantalk。该代码似乎在运行,最初使生成了对spawn的调用,但是,对phantomjs的调用似乎并未启动传递给它的脚本。只是为了确认,我在文件的开头添加了console.log语句,并且从未打印过。更不用说脚本文件中的其他打印语句了。代码如下:
code that performs the spawn:
var childArgs = [
//'--debug=true',
path.join(__dirname, '../phantom/capture.js'),
id,
filename
];
let child = spawn(phantomjs.path, childArgs, {detached: false});
console.log('set up stderr.....');
child.stderr.on('data', (data) => {
console.log('ps stderr: ${data}');
});
console.log('set up stdout.....');
child.stdout.on('data', (data) => {
console.log('ps stdout: ${data}');
});
console.log('set up close.....');
child.on('close', (code) => {
console.log('this is a test 2.....');
console.log(code);
if (code !== 0){
let error = new Error('An error occurred while creating the current report.');
error.status = 500;
reject(error);
}
resolve(fname);
return;
});
capture.js:
console.log('we are inside....');
let os = require('os');
var system = require('system');
console.log('we are inside.... 1');
var page = require('webpage').create();
let phantom = require('phantomjs-prebuilt');
console.log('we are inside....2');
var args = system.args;
console.log('we are inside....3');
if (args.length === 1) {
phantom.exit(0);
} else {
page.viewportSize = { width: 2000, height: 800 };
//page.paperSize = { format: 'Letter', orientation: 'landscape', margin: '1cm' };
page.paperSize = { width: '1280px', height: '800px', margin: '0px' };
page.settings.localToRemoteUrlAccessEnabled = true;
page.settings.loadImages = true;
page.settings.javascriptEnabled = true;
let done = false;
//"http://example.com/order/" + args[1] + '?token=' + args[2]
page.open("http://example.com/order/" + args[1], function start(status) {
if (status === "success"){
page.render(os.tmpdir() + '/' + args[2], { format: 'pdf' });
}
done = true;
});
var intervalId = setInterval(function(){
if (done){
phantom.exit();
}
}, 100);
}
我对问题可能出在哪里一无所知。 capture.js的第一行似乎没有被调用。 1)我缺少什么问题? 2)我该如何解决?
最佳答案
问题可能是由于安全原因,PhantomJS无法使用带有点的路径运行capture.js。例如,尝试从当前目录运行capture.js。
关于node.js - 使用spawn从node.js进行的phantomjs调用似乎未执行脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42967821/