Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix reference counting error in mupdfjs.PDFPage #137

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/mupdfjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ export class PDFDocument extends mupdf.PDFDocument {
_doc: mupdf.PDFDocument | undefined

// bespoke constructor to ensure we get the correct type of PDFDocument instance
constructor(doc:mupdf.PDFDocument, isMuPDFJSDoc:boolean) {
constructor(doc:mupdf.PDFDocument) {
super(doc.pointer)
if (isMuPDFJSDoc) {
this._doc = doc;
}
this._doc = doc;
}

// creates a new blank document with one page and adds a font resource, default size is A4 @ 595x842
Expand All @@ -93,7 +91,7 @@ export class PDFDocument extends mupdf.PDFDocument {
doc.insertPage(-1, pageObj)

if (doc instanceof mupdf.PDFDocument) {
return new PDFDocument(doc, true);
return new PDFDocument(doc);
}
throw new Error("Not a PDF document");
}
Expand All @@ -102,7 +100,7 @@ export class PDFDocument extends mupdf.PDFDocument {
let doc = super.openDocument(from, magic);

if (doc instanceof mupdf.PDFDocument) {
return new PDFDocument(doc, true);
return new PDFDocument(doc);
}
throw new Error("Not a PDF document");
}
Expand Down Expand Up @@ -708,6 +706,8 @@ export class PDFDocument extends mupdf.PDFDocument {
}

export class PDFPage extends mupdf.PDFPage {
// this is required so the page reference doesn't get garbage collected
_page: mupdf.PDFPage | undefined

// note page number is zero-indexed here
constructor(doc: mupdf.PDFDocument, pno: number) {
Expand All @@ -716,6 +716,7 @@ export class PDFPage extends mupdf.PDFPage {
}
let page: mupdf.PDFPage = doc.loadPage(pno)
super(doc, page.pointer)
this._page = page
}

insertText(value: string,
Expand Down Expand Up @@ -1060,4 +1061,4 @@ interface PageLabelRule {
prefix?: string;
style?: string;
firstpagenum?: number;
}
}