Context
I am using Dropbox and PDFJs library inside a Google Cloud Function
What I'm doing
Inside my functions
folder i run
npm i --save pdfjs-dist
Then I download a pdf content from dropbox (this works)
exports.readAProgram = functions.https.onRequest(async(req, res) => {
var dbx = new Dropbox.Dropbox({ accessToken: ACCESS_TOKEN });
dbx.filesDownload({ path: "/full/path/20220702.pdf" })
.then(function(response) {
console.log('response', response)
res.json(response.result.fileBinary);
})
.catch(function(error) {
// console.error(error);
res.json({"error-1": error})
});
});
I got this
Formatted is this
Note
I do not known what exactly is a fileBinary
because
- in the official doc (https://www.dropbox.com/developers/documentation/http/documentation#files-download) I cannot see the fileBinary
- in the official example of a download they are using a diffent method (https://github.com/dropbox/dropbox-sdk-js/blob/main/examples/javascript/download/index.html)
Next step: pass data to PDF.js.getDocument
I'm looking at the sourcecode, because obviously official api doc is useless.
See here: https://github.com/mozilla/pdf.js/blob/master/src/display/api.js#L232
The getDocument
function accepts
string|URL|TypedArray|PDFDataRangeTransport|DocumentInitParameters
Question
How can I convert my Dropbox fileBinary
structure into something accettable from PDFJS.getDocument
?
I tried
dbx.filesDownload({ path: "/full/path/20220702.pdf" })
.then(function(response) {
var loadingTask = PDFJS.getDocument(response.result.fileBinary)
.then(function(pdf) {
console.log ("OK !!!!")
res.json(response.result.fileBinary);
})
.catch(function (error) {
console.log ("error)
res.json({"error_2": error})
});
But I got this on console
> C:\laragon\www\test-pdf-dropbox\functions\node_modules\pdfjs-dist\build\pdf.js:2240
> data: structuredClone(obj, transfers)
> ^
>
> ReferenceError: structuredClone is not defined
> at LoopbackPort.postMessage (C:\laragon\www\test-pdf-dropbox\functions\node_modules\pdfjs-dist\build\pdf.js:2240:13)
> at MessageHandler.sendWithPromise (C:\laragon\www\test-pdf-dropbox\functions\node_modules\pdfjs-dist\build\pdf.js:8555:19)
> at _fetchDocument (C:\laragon\www\test-pdf-dropbox\functions\node_modules\pdfjs-dist\build\pdf.js:1356:48)
> at C:\laragon\www\test-pdf-dropbox\functions\node_modules\pdfjs-dist\build\pdf.js:1302:29
> at processTicksAndRejections (node:internal/process/task_queues:96:5)