Node js,将 pdfkit 管道传输到内存流 [英] Node js, piping pdfkit to a memory stream

查看:73
本文介绍了Node js,将 pdfkit 管道传输到内存流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的节点服务器上使用 pdfkit (https://github.com/devongovett/pdfkit),通常创建 pdf文件,然后将它们上传到 s3.问题是 pdfkit 示例将 pdf 文档通过管道传输到节点写入流中,该流将文件写入磁盘,我按照示例操作并正常工作,但是我现在的要求是将 pdf 文档通过管道传输到内存流而不是保存它在磁盘上(我正在上传到 s3).我遵循了一些节点内存流程序,但它们似乎都不能与我一起使用 pdf 管道,我只能将字符串写入内存流.所以我的问题是:如何将 pdf kit 输出通过管道传输到内存流(或类似的东西),然后将其作为对象读取以上传到 s3?

I am using pdfkit (https://github.com/devongovett/pdfkit) on my node server, typically creating pdf files, and then uploading them to s3. The problem is that pdfkit examples pipe the pdf doc into a node write stream, which writes the file to the disk, I followed the example and worked correctly, however my requirement now is to pipe the pdf doc to a memory stream rather than save it on the disk (I am uploading to s3 anyway). I've followed some node memory streams procedures but none of them seem to work with pdf pipe with me, I could just write strings to memory streams. So my question is: How to pipe the pdf kit output to a memory stream (or something alike) and then read it as an object to upload to s3?

var fsStream = fs.createWriteStream(outputPath + fileName); 
doc.pipe(fsStream);

提前致谢.

推荐答案

无需使用中间内存流1 –只需将 pdfkit 输出流直接通过管道传输到 HTTP 上传流.

There's no need to use an intermediate memory stream1 – just pipe the pdfkit output stream directly into a HTTP upload stream.

根据我的经验,AWS SDK 在处理流方面是垃圾,所以我通常使用 request.

In my experience, the AWS SDK is garbage when it comes to working with streams, so I usually use request.

var upload = request({
    method: 'PUT',
    url: 'https://bucket.s3.amazonaws.com/doc.pdf',
    aws: { bucket: 'bucket', key: ..., secret: ... }
});

doc.pipe(upload);

1 - 事实上,使用内存流通常是不可取的,因为这意味着在 RAM 中缓冲整个事物,而这正是流应该避免的!

这篇关于Node js,将 pdfkit 管道传输到内存流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆