Meteor-files: Failed to load PDF document from AWS

Created on 4 May 2017  路  16Comments  路  Source: veliovgroup/Meteor-Files

I'm having an issue:

I am using PDFKit(A JavaScript PDF generation library for Node and the browser)

PDF document created successfully and also upload on AWS successfully but when i am trying to opne the document the document not loading and showing message "Failed to load document".

My Code is:

let details = Async.runSync(function (done) {
            doc = new PDFDocument({size: 'A4', margin: 50});
            doc.info.Title = 'Study Report';
            doc.info.Author = '';
                  doc.fontSize(18);   
            doc.font('Times-Roman')
            .text(title,{align: 'center',height:100}).moveDown(1.0);
            doc.fontSize(12);
            doc.text(content,{align: 'justify'}).moveDown(2.0);
                doc.fontSize(12);
            doc.text(signature,{align: 'right'});
            //doc.text('PDFKit is simple', 10, 30, {align: 'justify', width: 200});
            let path = process.env["PWD"] +`/.meteor/local/files/report-${title}.pdf`;
        doc.pipe(fs.createWriteStream(path));
            done(null, path);
            doc.end();
        });


        if(details.result){
            otps = {
                fileName: `${title}`,
                type: 'application/pdf',
            }

            Files.addFile(details.result, otps,function(error, fileRef) {
                    if(!error){

                        //Meteor.users.update({_id:userId},{$set:{'paymentDetails.invoicePathId':fileRef._id}});
                        Meteor.call('manageStudyReport',fileRef._id,studyInstanceUid,function(error,response){
                            if(error) {
                                console.log('Error',error);
                            }
                        });
                    }
                    //return true;
                }, true);
        }
question

Most helpful comment

File was uploading to AWS:S3 but there was no contents.

Right now I resolved my problem and your codes working properly.

Thank you very much for support.

All 16 comments

Hello @Shanky0009 ,

  1. Is it Server or Client?
  2. Make sure you're not run into #392 (should be fixed soon)
  3. Is there any error stack? or any other useful logs?
  4. Could you enable debug mode, and post all logs?
  1. This is server side.
  2. No its not that.
  3. There are no logs
    There is no error while uploading or loading the file, if we don't upload to AWS the file is saved in local and it works fine but as soon it gets uploaded to AWS we are unable to open the file, showing "Failed to load document".

Hello @Shanky0009 ,

Sorry for delayed response.
Anyways, I would like to see all logs with enabled debug: true.

Could you post AWS related code? How files is uploaded? How file is requested?

After file uploading logs is:

[FilesCollection] [find(undefined, undefined)]
I20170522-17:26:47.759(5.5)? [FilesCollection] [remove({"_id":"oG2CwGYtnSPNAzZ78"})]
I20170522-17:26:47.764(5.5)? [FilesCollection] [unlink(oG2CwGYtnSPNAzZ78, undefined)]
I20170522-17:27:14.191(5.5)? /home/deligence/bit-radiology-portal/.meteor/local/files/Technology.pdf ........path
I20170522-17:27:14.202(5.5)? { fileName: 'Technology.pdf', type: 'application/pdf' } '...............otps'
I20170522-17:27:14.203(5.5)? [FilesCollection] [addFile(/home/deligence/bit-radiology-portal/.meteor/local/files/Technology.pdf)]
I20170522-17:27:14.211(5.5)? [FilesCollection] [addFile]: Technology.pdf -> Files
I20170522-17:27:16.728(5.5)? [FilesCollection] [unlink(9CRNaZrZeihjeQGGx, original)]

And when I tried to open pdf file the logs is:

[FilesCollection] [find(undefined, undefined)]
I20170522-17:29:10.049(5.5)? [FilesCollection] [download(/cdn/storage/Files/9CRNaZrZeihjeQGGx/original/9CRNaZrZeihjeQGGx.pdf?download=true, original)]
I20170522-17:29:10.219(5.5)? [FilesCollection] [download(/cdn/storage/Files/9CRNaZrZeihjeQGGx/original/9CRNaZrZeihjeQGGx.pdf?download=true, original)]

File creation and addFiles code is:

Meteor.methods({
    createStudyReport(title,content,signature,studyInstanceUid){
           doc = new PDFDocument({size: 'A4', margin: 50});
        doc.info.Title = 'Study Report';
        doc.info.Author = '';
        doc.fontSize(18);   
            doc.font('Times-Roman')
            .text(title,{align: 'center',height:100})
            .moveDown(1.0);

            doc.fontSize(12);
            doc.text(content,{align: 'justify'})
            .moveDown(2.0);

            doc.fontSize(12);
            doc.text(signature,{align: 'right'});
            //doc.text('PDFKit is simple', 10, 30, {align: 'justify', width: 200});
            let path = process.env["PWD"] +`/.meteor/local/files/${title}.pdf`;
        doc.pipe(fs.createWriteStream(path));
        doc.end();

        if(path){
            Files.addFile(path,  {
                fileName: `${title}.pdf`,
                type: 'application/pdf',
            }, function(error, fileRef) {
                 if(!error){
                             Meteor.call('manageStudyReport',fileRef._id,studyInstanceUid,function(error,response){
                    if(error) {
                       console.log('Error',error);
                    }
                });
            }
             }, true);
        }
    }
}); 

And file is uploaded thourgh files.js code :

import {FilesCollection} from "meteor/ostrio:files";

var knox, bound, client, Request, cfdomain, Collections = {};

if (Meteor.isServer) {
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;

  knox    = require('knox');
  Request = require('request');
  bound = Meteor.bindEnvironment(function(callback) {
    return callback();
  });
  cfdomain = ''********************************','; // <-- Change to your Cloud Front Domain
  client = knox.createClient({
    key: '**************************',
    secret: '********************************',
    bucket: ''********************************',',
    region: ''********************************','
  });
}

Files = new FilesCollection({
  debug: true, // Change to `true` for debugging
  throttle: false,
  storagePath: 'assets/app/uploads/uploadedFiles',
  collectionName: 'Files',
  allowClientCode: false,
  onBeforeUpload: function (file) {
        // Allow upload files under 10MB, and only in png/jpg/jpeg formats
        if (file.size <= 10485760 && /png|jpg|jpeg|pdf/i.test(file.extension)) {
            return true;
        } else {
            return 'Please upload image or pdf, with size equal or less than 5 Mb !';
        }
    },
  onAfterUpload: function(fileRef) {
    // In onAfterUpload callback we will move file to AWS:S3
    var self = this;
    _.each(fileRef.versions, function(vRef, version) {
      // We use Random.id() instead of real file's _id 
      // to secure files from reverse engineering
      // As after viewing this code it will be easy
      // to get access to unlisted and protected files
      var filePath = "files/" + (Random.id()) + "-" + version + "." + fileRef.extension;
      client.putFile(vRef.path, filePath, function(error, res) {
        bound(function() {
          var upd;
          if (error) {
            console.error(error);
          } else {
            upd = {
              $set: {}
            };
            upd['$set']["versions." + version + ".meta.pipeFrom"] = cfdomain + '/' + filePath;
            upd['$set']["versions." + version + ".meta.pipePath"] = filePath;
            self.collection.update({
              _id: fileRef._id
            }, upd, function(error) {
              if (error) {
                console.error(error);
              } else {
                // Unlink original files from FS
                // after successful upload to AWS:S3
                self.unlink(self.collection.findOne(fileRef._id), version);
              }
            });
          }
        });
      });
    });
  },
  interceptDownload: function(http, fileRef, version) {
    var path, ref, ref1, ref2;
    path = (ref = fileRef.versions) != null ? (ref1 = ref[version]) != null ? (ref2 = ref1.meta) != null ? ref2.pipeFrom : void 0 : void 0 : void 0;
    if (path) {
      Request({
        url: path,
        headers: _.pick(http.request.headers, 'range', 'accept-language', 'accept', 'cache-control', 'pragma', 'connection', 'upgrade-insecure-requests', 'user-agent')
      }).pipe(http.response);
      return true;
    } else {

      return false;
    }
  }
});

if (Meteor.isServer) {
  var _origRemove = Files.remove;

 Files.remove = function(search) {
    var cursor = this.collection.find(search);
    cursor.forEach(function(fileRef) {
      _.each(fileRef.versions, function(vRef) {
        var ref;
        if (vRef != null ? (ref = vRef.meta) != null ? ref.pipePath : void 0 : void 0) {
          client.deleteFile(vRef.meta.pipePath, function(error) {
            bound(function() {
              if (error) {
                console.error(error);
              }
            });
          });
        }
      });
    });
    // Call original method
    _origRemove.call(this, search);
  };
}

export default Files; 

Hello @Shanky0009 ,

We've updated recommended integration with AWS:S3, please update to the new guidelines

Hello dr.dimitru,

First of all thank you.

Here file uploaded successfully but when I uploading file the following error display on the terminal:
ReferenceError: bound is not defined:

````
ReferenceError: bound is not defined
at Response. (imports/files/files.js:84:13)

 at Request.<anonymous> (/home/deligence/bit-radiology-portal/node_modules/aws-sdk/lib/request.js:360:18)

 at Request.callListeners (/home/deligence/bit-radiology-portal/node_modules/aws-sdk/lib/sequential_executor.js:105:20)

 at Request.emit (/home/deligence/bit-radiology-portal/node_modules/aws-sdk/lib/sequential_executor.js:77:10)

 at Request.emit (/home/deligence/bit-radiology-portal/node_modules/aws-sdk/lib/request.js:678:14)

 at Request.transition (/home/deligence/bit-radiology-portal/node_modules/aws-sdk/lib/request.js:22:10)

 at AcceptorStateMachine.runTo (/home/deligence/bit-radiology-portal/node_modules/aws-sdk/lib/state_machine.js:14:12)

 at /home/deligence/bit-radiology-portal/node_modules/aws-sdk/lib/state_machine.js:26:10

 at Request.<anonymous> (/home/deligence/bit-radiology-portal/node_modules/aws-sdk/lib/request.js:38:9)

 at Request.<anonymous> (/home/deligence/bit-radiology-portal/node_modules/aws-sdk/lib/request.js:680:12)

Perhaps you're missing this line:

const bound  = Meteor.bindEnvironment((callback) => {
  return callback();
});

Which is mentioned here

After mention this code successfully uploading and downloading but when I trying to open the PDF file then showing an error Failed to load PDF document

Failed to load PDF document where exactly this error is shown?
What "Network" tab in a browser dev.tools shows?

Exactly error is: Failed to load PDF document

screenshot from 2017-05-26 12-16-36

And In Network mode after downloading file:

screenshot from 2017-05-26 12-07-25

  1. Aww... Failed to load PDF document is a different level error, never mind
  2. In a "Network" tab, there is ability to check response headers, could you show it too?
  3. Please enable debug mode (pass {debug: true} to FilesCollection Constructor) and post here all logs from both Client and Server

After uploading log on terminal:

after uploading

Nothing showing in Network mode

After file downloading logs on terminal:

after downloding

And in network mode:

response

  1. What is the failed (__red__) file in a Network tab on the last screenshot?
  2. Do you able to download file and then open with other application, not with a browser (for testing purpose only)?
  1. This is Nothing, when I was clicked two time by mistake then file failed to download.

  2. Yes, I able to download the file but the downloaded file is a text file with no content.

  1. Are you able to confirm file is uploaded to AWS:S3? Could you locate it there?
  2. Add console.log to putObject and to getObject to check what results is returned from AWS
  3. Make sure your implementation is the same as at this tutorial, note: it was updated various time during last weeks

File was uploading to AWS:S3 but there was no contents.

Right now I resolved my problem and your codes working properly.

Thank you very much for support.

Was this page helpful?
0 / 5 - 0 ratings