Upload pdf generated to AWS S3 using nodejs aws sdk

前端 未结 2 1608
深忆病人
深忆病人 2021-01-04 11:50

I am using pdfkit to generate a pdf with some custom content and then sending it to an AWS S3 bucket.

While if I generate the file as a whole and upload it works per

2条回答
  •  我在风中等你
    2021-01-04 11:57

    If you are using html-pdf package and aws-sdk than it's very easy...

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

提交回复
热议问题