ArtifexSoftware / mupdf.js

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

DocumentWriter.close WASM RuntimeError #54

Closed RythenGlyth closed 2 months ago

RythenGlyth commented 2 months ago

The DocumentWriter.close function seems malfunction and throws WASM RuntimeError.

This is the code reduced to the minimum:

import * as mupdf from 'mupdf'

const outBuffer = new mupdf.Buffer()
const out = new mupdf.DocumentWriter(outBuffer, ".pdf", "")
out.close()

It throws the following error

wasm://wasm/0239b566:1

RuntimeError: null function or function signature mismatch
    at wasm://wasm/0239b566:wasm-function[1894]:0x13ff94
    at wasm://wasm/0239b566:wasm-function[1893]:0x13ff49
    at wasm://wasm/0239b566:wasm-function[1985]:0x147d8e
    at wasm://wasm/0239b566:wasm-function[1895]:0x13fff1
    at wasm://wasm/0239b566:wasm-function[3558]:0x2669f9
    at invoke_viiii (file:///.../node_modules/mupdf/dist/mupdf-wasm.js:5784:29)
    at wasm://wasm/0239b566:wasm-function[3548]:0x25ff90
    at wasm://wasm/0239b566:wasm-function[3546]:0x25c738
    at wasm://wasm/0239b566:wasm-function[3567]:0x26cc49
    at wasm://wasm/0239b566:wasm-function[2299]:0x1797b5

Node.js v21.6.2

instead if I try to add a page:

import * as mupdf from 'mupdf'

const outBuffer = new mupdf.Buffer()
const out = new mupdf.DocumentWriter(outBuffer, ".pdf", "")
const dev = out.beginPage([0,0,200,200])
/* draw something on the Device or not, it doesn't matter */
out.endPage()
out.close()

another error is thrown:

wasm://wasm/0239b566:1

RuntimeError: memory access out of bounds
    at wasm://wasm/0239b566:wasm-function[1894]:0x13ffad
    at wasm://wasm/0239b566:wasm-function[1893]:0x13ff49
    at wasm://wasm/0239b566:wasm-function[1985]:0x147ecf
    at wasm://wasm/0239b566:wasm-function[1895]:0x13fff1
    at wasm://wasm/0239b566:wasm-function[3558]:0x2669f9
    at invoke_viiii (file:///.../node_modules/mupdf/dist/mupdf-wasm.js:5784:29)
    at wasm://wasm/0239b566:wasm-function[3548]:0x25ff90
    at wasm://wasm/0239b566:wasm-function[3546]:0x25c738
    at wasm://wasm/0239b566:wasm-function[3567]:0x26cc49
    at wasm://wasm/0239b566:wasm-function[2299]:0x1797b5

Node.js v21.6.2

No matter what, outBuffer.getLength() is always 0 after the out.close()

When recreating everything from mupdf's muconvert.c in JS, the second error is thrown as well.

jamie-lemon commented 2 months ago

Interesting, I get a different error output on your first example as follows:

5735101: ($0) => { throw new Error(UTF8ToString($0)); },  
                          ^
Error: unknown output document format: 
    at 5735101 (file:///artifex/SDK/mupdfjs/npm-0.2.0/node_modules/mupdf/dist/mupdf-wasm.js:995:27)
    at runEmAsmFunction (file:///artifex/SDK/mupdfjs/npm-0.2.0/node_modules/mupdf/dist/mupdf-wasm.js:4256:30)
    at _emscripten_asm_const_int (file:///artifex/SDK/mupdfjs/npm-0.2.0/node_modules/mupdf/dist/mupdf-wasm.js:4259:14)
    at wasm://wasm/0239b566:wasm-function[111]:0x802a
    at invoke_vi (file:///artifex/SDK/mupdfjs/npm-0.2.0/node_modules/mupdf/dist/mupdf-wasm.js:5685:29)
    at wasm://wasm/0239b566:wasm-function[325]:0x2d502
    at Object._wasm_new_document_writer_with_buffer (file:///artifex/SDK/mupdfjs/npm-0.2.0/node_modules/mupdf/dist/mupdf-wasm.js:705:12)
    at new DocumentWriter (file:///artifex/SDK/mupdfjs/npm-0.2.0/node_modules/mupdf/dist/mupdf.js:1136:24)
    at file:///artifex/SDK/mupdfjs/npm-0.2.0/node.mjs:4:13

Node.js v20.8.0
jamie-lemon commented 2 months ago

it seems like you can get the error with just 2 lines:

import * as mupdf from 'mupdf'
const outBuffer = new mupdf.Buffer()
const out = new mupdf.DocumentWriter(outBuffer, "PDF", "")

So I think this error is related to the mupdf.DocumentWriter constructor.

RythenGlyth commented 2 months ago

Interesting, I get a different error output on your first example as follows:

5735101: ($0) => { throw new Error(UTF8ToString($0)); },  
                          ^
Error: unknown output document format: 
    at 5735101 (file:///artifex/SDK/mupdfjs/npm-0.2.0/node_modules/mupdf/dist/mupdf-wasm.js:995:27)
    at runEmAsmFunction (file:///artifex/SDK/mupdfjs/npm-0.2.0/node_modules/mupdf/dist/mupdf-wasm.js:4256:30)
    at _emscripten_asm_const_int (file:///artifex/SDK/mupdfjs/npm-0.2.0/node_modules/mupdf/dist/mupdf-wasm.js:4259:14)
    at wasm://wasm/0239b566:wasm-function[111]:0x802a
    at invoke_vi (file:///artifex/SDK/mupdfjs/npm-0.2.0/node_modules/mupdf/dist/mupdf-wasm.js:5685:29)
    at wasm://wasm/0239b566:wasm-function[325]:0x2d502
    at Object._wasm_new_document_writer_with_buffer (file:///artifex/SDK/mupdfjs/npm-0.2.0/node_modules/mupdf/dist/mupdf-wasm.js:705:12)
    at new DocumentWriter (file:///artifex/SDK/mupdfjs/npm-0.2.0/node_modules/mupdf/dist/mupdf.js:1136:24)
    at file:///artifex/SDK/mupdfjs/npm-0.2.0/node.mjs:4:13

Node.js v20.8.0

That's really strange. I used the 0.2.0 npm version as you did if I interpret your error stack correctly. (I also compiled my own version of this package and it threw the same errors)

What does differ is the node version and maybe the OS? I ran mine on Windows

RythenGlyth commented 2 months ago

it seems like you can get the error with just 2 lines:

import * as mupdf from 'mupdf'
const outBuffer = new mupdf.Buffer()
const out = new mupdf.DocumentWriter(outBuffer, "PDF", "")

So I think this error is related to the mupdf.DocumentWriter constructor.

For me, the constructor is fine and doesn't throw any error.

jamie-lemon commented 2 months ago

Okay - I aligned as much as possible with you and updated my node to v21.6.2 - I still get the same error. I am on a MacOS so this probably explains the difference in our output logs. I am also just using the mupdf.js from NPM directly. I think we will have to wait until next week to figure this out as the main developer @ccxvii will be available then.

RythenGlyth commented 2 months ago

Okay, thank you

ccxvii commented 2 months ago

One problem is a typo in the DocumentWriter constructor (the option string is clobbering the format string when passing to WASM), but even if I fix that there's another problem with RuntimeError: null function or function signature mismatch that I'm trying to track down the cause for.

ccxvii commented 2 months ago

Second problem is likely in the core library which doesn't like creating a PDF document writer and then closing it before writing any pages.

jamie-lemon commented 2 months ago

@RythenGlyth This should be fixed with the latest release of mupdf.js (0.2.1) on NPM - https://www.npmjs.com/package/mupdf , please confirm!

RythenGlyth commented 2 months ago

Yes it does perfectly work. Thank you!