【问题标题】:I'd like to pipe a document to and http POST from pdfutils我想从 pdfutils 将文档通过管道传输到 http POST
【发布时间】:2014-07-01 12:37:03
【问题描述】:

所以,正如您在下面看到的,我正在使用名为 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);
  }

`

【问题讨论】:

    标签: http pdf stream fs


    【解决方案1】:

    你需要使用流事件

    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
    })
    

    【讨论】:

      猜你喜欢
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      相关资源
      最近更新 更多