0

I managed to get my generated pdf uploaded to s3 from my node JS server. Pdf looks okay on my local folder but when I tried to access it from the AWS console, it indicates "Failed to load PDF document".

I have tried uploading it via the s3.upload and s3.putObject APIs, (for the putObject I also used an .on finish checker to ensure that the file has been fully loaded before sending the request). But the file in the S3 bucket is still the same (small) size, 26 bytes and cannot be loaded. Any help is greatly appreciated!!!

    var pdfDoc = printer.createPdfKitDocument(inspectionReport);
    var writeStream = fs.createWriteStream('pdfs/inspectionReport.pdf');
    pdfDoc.pipe(writeStream);
    pdfDoc.end();
    writeStream.on('finish', function(){
        const s3 = new aws.S3();
        aws.config.loadFromPath('./modules/awsconfig.json');

    var s3Params = {
        Bucket: S3_BUCKET,
        Key: 'insp_report_test.pdf',
        Body: '/pdf/inspectionReport.pdf',
        Expires: 60,
        ContentType: 'application/pdf'
    };
    s3.putObject(s3Params, function(err,res){
        if(err) 
            console.log(err);
        else
            console.log(res);
    })
jlyh
  • 681
  • 9
  • 32
  • 2
    open the file in a text editor and look what is the message - most probably its telling your about the error – Frederic Henri Jul 25 '16 at 16:29
  • I take it the file is supposed to be larger than 26 bytes. Does the `console.log(res);` line in your code execute and print anything? It sounds like your app is exiting before the `s3.putObject()` call completes. – Mark B Jul 25 '16 at 16:52
  • `Body: '/pdf/inspectionReport.pdf',` How many bytes are there in the *string* `/pdf/inspectionReport.pdf`? Looks like 26. `Body` expects the *body* -- not a path. – Michael - sqlbot Jul 26 '16 at 00:05
  • Michael-sqlbot I think you're right.. I opened a copy of the downloaded file in my computer with notepad and it reads '/pdf/inspectionReport.pdf'. However, when I replaced the value for Body: pdfDoc, AWS API gives me an error - [Error: Cannot determine length of [object PDFDocument]]. How can I pass in the pdf body? Also, I suppose even if I only have the '/pdf/inspectionReport.pdf' string in my body, I should have a proper pdf saved in my S3 bucket that reads that string and not a file that can't be loaded. Could it be that these are 2 separate issues altogether? – jlyh Jul 26 '16 at 00:57
  • @jlyh did you solve the issue ?? please share with us – nur farazi Aug 01 '16 at 06:21
  • @nurfarazi No I haven't managed to solve this yet and decided to circumvent the issue in the interests of time. Doing so, I stored all the pieces of information that I needed to generate a pdf, and when it's required to retrieve the doc, I would to generate the pdf on the client-side for download. I'd still be interested to resolve this issue if you are able to find a solution! :) – jlyh Aug 02 '16 at 02:34
  • @nurfarazi I managed to solve this, see answer below. – jlyh Aug 09 '16 at 13:09

1 Answers1

1

I realised that pdfDoc.end() must come before piping starts. Have also used a callback to ensure that s3 upload is called after pdf write is finished. See code below, hope it helps!

var pdfDoc = printer.createPdfKitDocument(inspectionReport);
pdfDoc.end();    

async.parallel([

        function(callback){
            var writeStream = fs.createWriteStream('pdfs/inspectionReport.pdf');
            pdfDoc.pipe(writeStream);
            console.log('pdf write finished!');
            callback();
        }

    ], function(err){

        const s3 = new aws.S3();
        var s3Params = {
            Bucket: S3_BUCKET,
            Key: 'insp_report_test.pdf',
            Body: pdfDoc,
            Expires: 60,
            ContentType: 'application/pdf'
        };

        s3.upload(s3Params, function(err,result){
            if(err) console.log(err);
            else console.log(result);
        });
    }
);
jlyh
  • 681
  • 9
  • 32