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".
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);
}
Hello @Shanky0009 ,
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?
[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)]
[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)]
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);
}
}
});
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.
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?
Failed to load PDF document

Failed to load PDF document is a different level error, never mind{debug: true} to FilesCollection Constructor) and post here all logs from both Client and Server
Nothing showing in Network mode

And in network mode:

This is Nothing, when I was clicked two time by mistake then file failed to download.
Yes, I able to download the file but the downloaded file is a text file with no content.
console.log to putObject and to getObject to check what results is returned from AWSFile 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.
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.