【发布时间】:2016-06-30 06:38:58
【问题描述】:
我是 node.js 和 phantom.js 的新手,所以我不确定如何比我在下面所做的更好地利用它们。
我有 100 所学校的着装价格表,可以从相应的学校页面下载为 PDF 格式。我们所做的是生成 PDF 并在一夜之间上传到服务器。
现在我们想使用 node.js 和 phantom.js 批量生成 PDF 并尽可能自动化该过程。
下面的链接不是价目表页面,而是用于测试 PDF 的示例 URL。
```
var schedule = require('node-schedule'),
path = require('path'),
childProcess = require('child_process'),
phantomjs = require('phantomjs'),
binPath = phantomjs.path,
childArgs = [
// phantomjs rasterize.js http://codefight.org codefight.pdf
path.join(__dirname, 'rasterize.js'),
'http://codefight.org/',
'codefight.pdf',
'400*300'
]
// add all the URLs and name of PDF here
var pdfSources = [
['codefight.pdf', 'http://codefight.org/'],
['dltr.pdf', 'http://dltr.org/']
];
// schedule generating PDFs
// running every minute for now to test
var j = schedule.scheduleJob('* * * * *', function(){
// loop through the pdfSources and generate new PDFs
pdfSources.forEach(function(item, index){
// update childArgs
childArgs[1] = item[1]; // pdf content source url
childArgs[2] = item[0]; // pdf filename
childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
// for some reason childArgs[2] always prints last item of pdfSources
// not sure how to make it work :(
console.log('New PDF - "' + childArgs[2] + '" generated!');
console.log(err + stdout + stderr);
});
});
});
```
1. 我想知道为什么console.log('New PDF - "' + childArgs[2] + '" generated!'); 总是打印相同的输出。即“新 PDF - “dltr.pdf”生成!”
2. 是否有更好的方法来使用 node.js 和 phantom.js 实现相同的目标以及您想提出的任何改进建议?
谢谢!
【问题讨论】:
标签: javascript node.js performance pdf phantomjs