Working with Files#

Local Files#

An example of loading a local file using the openDocument method with a local string to reference the file path.

EXAMPLE

let document = mupdf.PDFDocument.openDocument(fs.readFileSync("test.pdf"),
                                                "application/pdf")

Remote Files#

An example of loading a remote file which waits for the remote data and then uses the openDocument method with the resulting buffer data.

EXAMPLE

async function loadRemoteFile(url) {
    let response = await fetch(url)
    if (!response.ok) {
        console.error(`Cannot fetch document: ${response.statusText}`)
        return
    }
    let data = await response.arrayBuffer()
    let document = mupdf.PDFDocument.openDocument(data, url)
}

loadRemoteFile("https://mupdf.com/docs/mupdf_explored.pdf")

Note

After loading a file we receive a Document instance in return.

Saving Files#

It is up the application developer to work out exactly how they may want to save their file data, however once we have a document instance we can obtain the data in a buffer and use this to save the new file.

For the simplest implementation, which saves the file locally to the current folder location, use the following:

EXAMPLE

fs.writeFileSync("output.pdf", document.saveToBuffer("incremental").asUint8Array())

For full details refer to the saveToBuffer method.