...master 2026-11-12 (1037e2069+unstaged)

Buffer

Buffer objects are used for working with binary data, e.g. returned from asJPEG. They can be used much like arrays, but are much more efficient since they only store bytes.

Constructors

class Buffer(data)

Creates a new Buffer initialized with contents from data.

var buffer = new mupdf.Buffer("hello world")

Instance properties

Buffer.prototype.length

The number of bytes in this buffer (read-only).

Buffer.prototype.[n]

only mutool run

Get or set the byte at index n.

See readByte() and writeByte() for the equivalent in mupdf.js.

var byte = buffer[0]

Instance methods

Buffer.prototype.asString()

Returns the contents of this buffer as a string.

var str = buffer.asString()

Buffer.prototype.asUint8Array()

only mupdf.js

Returns the contents of this buffer as a Uint8Array.

var arr = buffer.asUint8Array()

Buffer.prototype.getLength()

Returns the number of bytes in this buffer.

var length = buffer.getLength()

Buffer.prototype.readByte(at)

Returns the byte at the specified index at if within 0 <= at < getLength(). Otherwise returns undefined.

buffer.readByte(0)

Buffer.prototype.slice(start, end)

Create a new buffer containing a (subset of) the data in this buffer. Start and end are offsets from the beginning of this buffer, and if negative from the end of this buffer. If start points to the end of this buffer, or if end point to at or before start, then an empty buffer will be returned.

var buffer = new mupdf.Buffer()
buffer.write("hello world") // buffer contains "hello world"
var newBuffer = buffer.slice(1, -1) // newBuffer contains "ello worl"

Buffer.prototype.write(str)

Append the string as UTF-8 to the end of this buffer.

buffer.write("hello world")

Buffer.prototype.writeBuffer(data)

Append the contents of the data buffer to the end of this buffer.

buffer.writeBuffer(anotherBuffer)

Buffer.prototype.writeByte(byte)

Append a single byte to the end of this buffer. Only the least significant 8 bits of the value are appended.

buffer.writeByte(0x2a)

Buffer.prototype.writeLine(str)

Append string to the end of this buffer ending with a newline.

buffer.writeLine("a line")

Buffer.prototype.writeRune(c)

only mutool run

Encode a unicode character as UTF-8 and append to the end of the buffer.

buffer.writeRune(0x4f60) // To append U+4f60, 你
buffer.writeRune(0x597d) // To append U+597d, 好
buffer.writeRune(0xff01) // To append U+ff01, !

Buffer.prototype.save(filename)

Write the contents of the buffer to a file.

buffer.save("buffer.dat")