【发布时间】: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);
}
`
【问题讨论】: