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 = mupdfjs.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 = mupdfjs.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.
Code samples
Code samples are in TypeScript and assume that the following requirements are defined in your TypeScript file header as follows:
import * as fs from "fs"
import * as mupdfjs from "mupdf/mupdfjs"