我是node.js和phantom.js的新手,所以我不知道如何更好地利用它們,比我在下面做的更好。使用phantom.js和node.js計劃PDF生成
我有100多所學校的服裝價目表,可以從各自的學校頁面下載爲PDF格式。我們所做的是生成PDF並在一夜之間上傳到服務器。
現在我們想要使用node.js和phantom.js來批量生成PDF,並儘可能地自動執行該過程。
以下鏈接不是價格表網頁,而是測試PDF的示例網址。
```
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);
});
});
});
```
我想知道的是爲什麼console.log('New PDF - "' + childArgs[2] + '" generated!');
始終打印相同的輸出。即「新PDF - 」dltr.pdf「生成!」
2.有沒有更好的方式來實現與node.js & phantom.js和任何改進你想建議的相同的東西?
謝謝!
感謝。測試但結果相同。 –
嗯奇怪。你是否可以添加'console.log(cArgs);'justow bellow'(function(cArgs){'並且實際上是否創建了2個不同的PDF文件? – Molda
是用正確的內容創建的2個不同的PDF文件。 。 –