Skip to content

Commit

Permalink
Removed removal of listeners when reading multiple pdf files with a s…
Browse files Browse the repository at this point in the history
…ingle object
  • Loading branch information
nicolabaesso committed Dec 30, 2024
1 parent 0b2d99a commit 024ff52
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export default class PDFJSClass extends EventEmitter {
process.nextTick(() => this.emit("pdfjs_parseDataReady", data));
if(data===null){
//reset the state of the PDF reader
this.destroy();
this.resetCurrentObject();
}
return data;
}
Expand Down Expand Up @@ -406,6 +406,14 @@ export default class PDFJSClass extends EventEmitter {
return { Pages: this.pages };
}

resetCurrentObject(){
if (this.pdfDocument) this.pdfDocument.destroy();
this.pdfDocument = null;

this.pages = [];
this.rawTextContents = [];
}

destroy() {
this.removeAllListeners();

Expand Down
6 changes: 2 additions & 4 deletions test/_test_testMultipleDataPDF.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("Multiple PDFs with same structure",()=>{
const secondPDFBuffer=fs.readFileSync(secondPDFLocation);
//we need to check if buffers are indeed different, otherwise it's useless!
expect(firstPDFBuffer).not.toBe(secondPDFBuffer);
const firstPDFData=await new Promise((resolve,reject)=>{
const firstData=await new Promise((resolve,reject)=>{
parser.parseBuffer(firstPDFBuffer,5);
parser.on("pdfParser_dataReady", (evtData) => {
resolve(evtData);
Expand All @@ -24,7 +24,7 @@ describe("Multiple PDFs with same structure",()=>{
reject(evtData);
});
});
const secondPDFData=await new Promise((resolve,reject)=>{
const secondData=await new Promise((resolve,reject)=>{
parser.parseBuffer(secondPDFBuffer,5);
parser.on("pdfParser_dataReady", (evtData) => {
resolve(evtData);
Expand All @@ -34,8 +34,6 @@ describe("Multiple PDFs with same structure",()=>{
reject(evtData);
});
});
const firstData=await firstPDFData;
const secondData=await secondPDFData;
//first, make sure the files are read
expect(firstData).toBeDefined();
expect(firstData.Pages[0]).toBeDefined();
Expand Down

0 comments on commit 024ff52

Please sign in to comment.