ArtifexSoftware / mupdf.js

JavaScript bindings for MuPDF
https://mupdfjs.readthedocs.io
GNU Affero General Public License v3.0
318 stars 17 forks source link

Journal does not work as expected #88

Closed xeladotbe closed 3 weeks ago

xeladotbe commented 1 month ago

Hi, first of all, thank you for this great library - I'm currently experimenting with the journal and it is not working as expected.

This is what I am trying:

    const doc = mupdf.Document.openDocument(buffer, 'application/pdf');

    doc.enableJournal();

    console.log(doc.countPages()); // => 62
    console.log(doc.hasUnsavedChanges()); // => false

    doc.beginOperation('rearrangePages');
    doc.rearrangePages([0, 1, 2, 3]);
    doc.endOperation();
    doc.undo();

    console.log(doc.countPages()); // => 4
    console.log(doc.hasUnsavedChanges()); // => true

If I then try to load a page (doc.loadPage(0)) I get the following exception.

    mupdf-wasm.js:1550 format error: too many kids in page tree
    mupdf-wasm.js:1550 warning: Page tree load failed. Falling back to slow lookup

Furthermore, after the undo call the number of pages should be 62 again and hasUnsavedChanges should return false.

Here is another example:

    const doc = mupdf.Document.openDocument(buffer, 'application/pdf');

    doc.enableJournal();

    console.log(doc.countPages()); // 62
    console.log(doc.hasUnsavedChanges()); // false

    doc.beginOperation('deletePage');
    doc.deletePage(0);
    doc.endOperation();

    console.log(doc.countPages()); // 61 

    doc.undo();

    console.log(doc.countPages()); // 62
    console.log(doc.hasUnsavedChanges()); // true

    doc.loadPage(0);

The doc.loadPage(0) call leads to the following exceptions/warnings:

    mupdf-wasm.js:1550 warning: Document claims to have 62 pages, but only has 61.
    mupdf-wasm.js:1550 argument error: Can't alter an object other than in an operation
    mupdf-wasm.js:1550 warning: Page tree load failed. Falling back to slow lookup

The page number is correct, but hasUnsavedChanges still returns true, and no exceptions should be thrown.

ccxvii commented 3 weeks ago

Fixed in 23b9f1bcce1c4f23d12c50e28362e2dbbd8fa891.