我正在使用pdfkit生成包含自定义内容的pdf文件,然后将其发送到AWS S3存储桶。
如果我将整个文件生成并上传,则可以正常工作,但是,如果我想将生成的文件流式传输,例如作为八位字节流,我找不到任何相关的指针。
我正在寻找一个基于Node.js的解决方案(或建议)。
我正在使用pdfkit生成包含自定义内容的pdf文件,然后将其发送到AWS S3存储桶。
如果我将整个文件生成并上传,则可以正常工作,但是,如果我想将生成的文件流式传输,例如作为八位字节流,我找不到任何相关的指针。
我正在寻找一个基于Node.js的解决方案(或建议)。
我会尽力准确表达。我不会详细介绍pdfKit的 nodejs sdk 的用法。
如果你想将生成的 PDF 文件作为文件保存。
var PDFDocument = require('pdfkit');
// Create a document
doc = new PDFDocument();
// Pipe it's output somewhere, like to a file or HTTP response
doc.pipe(fs.createWriteStream('output.pdf'));
doc.text('Whatever content goes here');
doc.end();
var params = {
key : fileName,
body : './output.pdf',
bucket : 'bucketName',
contentType : 'application/pdf'
}
s3.putObject(params, function(err, response) {
});
然而,如果您想将其流式传输(在问题的上下文中说S3存储桶),则值得记住每个pdfkit实例都是可读流。
S3希望获得一个文件、缓冲区或可读流。
var doc = new PDFDocument();
// Pipe it's output somewhere, like to a file or HTTP response
doc.text("Text for your PDF");
doc.end();
var params = {
key : fileName,
body : doc,
bucket : 'bucketName',
contentType : 'application/pdf'
}
//notice use of the upload function, not the putObject function
s3.upload(params, function(err, response) {
});
aws-sdk
或其他模块吗?现在尝试使用 knox
并使用 putStream
来完成此操作。我的文件没有保存在文件系统中,只是流式传输到了 s3。 - Nigel Earle我尝试了这个方法并且成功了。我创建了一个readFileSync然后上传到S3中。我还使用了"writeStream.on('finish'"来确保在上传之前完全创建pdf文件,否则它会上传部分文件。
const PDFDocument = require('pdfkit');
const fs = require('fs');
const AWS = require('aws-sdk');
const path = require('path')
async function createPDF() {
const doc = new PDFDocument({size: 'A4'});
let writeStream = fs.createWriteStream('./output.pdf')
doc.pipe(writeStream);
// Finalize PDF file
doc.end();
writeStream.on('finish', function () {
var appDir = path.dirname(require.main.filename);
const fileContent = fs.readFileSync(appDir + '/output.pdf');
var params = {
Key : 'filName',
Body : fileContent,
Bucket : process.env.AWS_BUCKET,
ContentType : 'application/pdf',
ACL: "public-read"
}
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY
});
//notice use of the upload function, not the putObject function
s3.upload(params, function(err, response) {
});
});
}
var pdf = require('html-pdf');
import aws from 'aws-sdk';
const s3 = new aws.S3();
pdf.create(html).toStream(function(err, stream){
stream.pipe(fs.createWriteStream('foo.pdf'));
const params = {
Key: 'foo.pdf',
Body: stream,
Bucket: 'Bucket Name',
ContentType: 'application/pdf',
};
s3.upload(params, (err, res) => {
if (err) {
console.log(err, 'err');
}
console.log(res, 'res');
});
});
Error: ENOENT: no such file or directory, open './2020-2021/11761.pdf']
。 - Lalitesh Upadhyaya这是我如何使用Cypress将PDF文件上传到AWS S3的方法:
cy.readFile("cypress/fixtures/Document.pdf", "base64").then((fileBase64) => {
const fileBlob = Cypress.Blob.base64StringToBlob(fileBase64, "application/pdf");
uploadFileToS3(blob);
});
request
模块吗?你应该可以将doc
管道传输到request
。 - edin-m