因此,正如您在下面看到的,我正在为名为 pdfutils 的节点使用 npm 模块。它允许我将多页 pdf 拆分为单页文档。我计划将这些文档存储在沙发数据库中。我这里的主要问题是对 javascript 的异步特性缺乏理解。我已经为此苦苦挣扎了 3 天,但我完全被难住了。
pdfutils 的作者提到将 toFile(filePath)
替换为管道或某种类型的读取流,但没有示例,我很难过。我只是不明白管道数据在 node.js 中是如何工作的。
我的目标是将生成的 3 个 pdf 文件中的每一个通过管道传输到 3 个单独的 http 调用中,以提交给 couchDB。我知道如何提交,只是不知道如何通过管道传输结果并触发 http 事件。
` var pdfutils = require('pdfutils').pdfutils;
// This splits one file into separate pages
pdfutils(req.files.file.path, function(err, doc) {
for ( var i=0 ; i<doc.length; i++) {
var document = JSON.parse(req.body.document);
var page = i+1;
var filePath = 'app/images/'+ document.id + '_' + page + '.pdf';
// Write the files to disk (but I'd rather pipe to an http call)
doc[i].asPDF().toFile(filePath);
}
`
最佳答案
你需要使用流事件
var converter = doc[0].asPNG({ maxWidth: 200, maxHeight: 200}),
data = new Buffer(0);
converter.on('data', Meteor.bindEnvironment(function( chunk ){
data = Buffer.concat([ data , chunk ]);
}));
converter.on('end', Meteor.bindEnvironment(function(){
//Send the buffer to AWS or other service
})
关于http - 我想将文档通过管道传输到 pdfutils 并通过 http POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23638003/