1

I am using Gridfs to store large files in mognodb. Now I am using PDFTron for pdf editing and want to watermark pdf. The problem is i am not able to read file from Gridfs stream in pdftron nodejs sdk. also i want to send it back to the client without storing it locally or anywhere else.

I am doing something like this...

const bucket = new mongodb.GridFSBucket(db);
const stream = bucket.openDownloadStream(ObjectId(file_id))
const pdfdoc = await PDFNet.PDFDoc.createFromFilter(stream);

the error i am getting is ... TypeError: 1st input argument in function 'createFromFilter' is of type 'object'. Expected type 'Filter'. Function Signature: createFromFilter(PDFNet.Filter)

1 Answers1

1

The PDFDoc.createFromFilter API is expecting a PDFNet Filter, not whatever GridFS is returning. https://www.pdftron.com/api/pdfnet-node/PDFNet.PDFDoc.html#.createFromFilter__anchor

You can see this sample on creating a PDFDoc object from a Filter https://www.pdftron.com/documentation/samples/node/js/PDFDocMemoryTest

Though the easiest is to write your GridFD stream to a buffer, and then pass that buffer to PDFDoc.createFromBuffer. https://www.pdftron.com/api/pdfnet-node/PDFNet.PDFDoc.html#.createFromBuffer__anchor

Ryan
  • 2,473
  • 1
  • 11
  • 14