I am using Dropbox and PDFJs library inside a Google Cloud Function
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
I do not known what exactly is a fileBinary
because
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
How can I convert my Dropbox fileBinary
structure into something accettable from PDFJS.getDocument
?
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)
i solved
instead of using
const PDFJS = require("pdfjs-dist");
I do now
const PDFJS = require("pdfjs-dist/legacy/build/pdf.js");
the npm package is the same, pdfjs-dist
var pdf = PDFJS.getDocument(new Uint8Array(response.result.fileBinary)).promise
.then(function(pdf) {
console.log ("Letto il PDF !!!!", pdf)
res.json({done: true})
})
Note
new Uint8Array
.promise
before of .then