Uncaught (in promise) Error: already ended using pdf-merger-js module

940 Views Asked by At

Context

I'm developping a vueJS component dedicated to pdf manipulation. The goal is to allow the user to upload multiple pdf files, delete some pages and reorganize all the pages in a specific order.

Code

I have an array of file nammed pdfFiles. Every time the user drop a new pdf file i store the blob in this array.

There is an array nammed pages. This array store every pages of every imported pdf files. The objects strored in this array have the following structure :

pageObject: {
    pdfFileIndex: Integer,
    index: Integer,
    id: Integer,
    page: PDFPageProxy
}

page which is an autogenerated object from PDF.js lib and id are not important for the exemple.

pdfFileIndex is an Interger corresponding to the index of the file in the pdfFiles array the page is associated to.

index simply is the index of the page in his pdf file.

When the user ended the manipulation of the files i use the following code to generate a pdf containing only the pages keept by the user :

exportPdf() {
    (async () => {
        this.pages.forEach(page => {
            console.log(this.pdfFiles[page.pdfFileIndex]);
            console.log([page.index]);
            this.merger.add(this.pdfFiles[page.pdfFileIndex], [page.index]);
        });

        return await this.merger.saveAsBlob();
    })();
}

this.merger have been initialized as : merger: new PDFMerger() in the data scope.

So this.pdfFiles[page.pdfFileIndex] contains the blob of the file and [page.index] contains the index of the page i want to add to the merger object.

I used some console.log to check if both of these values were corresponding to what the user did in the editor and it is.

Issue

I have an issue on the this.merger.saveAsBlob() step which is :

Uncaught (in promise) Error: already ended
at eval (fragment.js?2c29:10)
at Object../node_modules/pdfjs/lib/fragment.js (chunk-vendors.js:3656)
at __webpack_require__ (app.js:854)
at fn (app.js:151)
at Object.eval (document.js?07b2:4)
at eval (document.js:813)
at Object../node_modules/pdfjs/lib/document.js (chunk-vendors.js:3572)
at __webpack_require__ (app.js:854)
at fn (app.js:151)
at eval (index.js?4e59:3)

There is only one post that relate this issue on the github of the module which is this one : https://github.com/nbesli/pdf-merger-js/issues/29.

The fact is that my problem isn't the same because i only call the "exportPdf" function once in the application.

If more code or explanations are needed just ask me. Thank for any help

1

There are 1 best solutions below

0
On

merger.add is an asynchronous call.

await merger.add(file)