所以,正如您在下面看到的,我正在使用名为pdfutils的节点的npm模块。它允许我将多页pdf拆分为单页文档。我打算把这些文件储存在沙发数据库里。我在这里的主要问题是对javascript的异步特性缺乏了解。我已经和这个摔跤3天了,我完全被难住了。
pdfutils的作者提到用管道或某种readstream替换toFile(filePath),但是没有一个例子,我就很困惑了。我只是不明白node.js中的管道数据是如何工作的。
我的目标是将生成的3个pdf文件中的每一个管道化为3个单独的http调用,以便提交给couchdb。我知道如何提交,只是不知道如何通过管道传递结果并触发http事件。
`
var pdfutils=需要('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
})

09-25 21:29