Upload pdf generated to AWS S3 using nodejs aws sdk

末鹿安然 提交于 2019-12-01 04:48:22
Shivendra Soni

I'll try to be precise here. I will not be covering usage of pdfKit's nodejs sdk in much detail.

IF you want your generated pdf as a file.

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

});

However if you want to stream it ( to say S3 bucket in the context of question), then it is worth remembering that every pdfkit instance is a readable stream.

And S3 expects a file, a buffer or a readable stream. So,

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

});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!