Skip to Content
prasasti

Node.js

Every class, method and function in the prasasti npm package, for Node.js and Bun.

const { Document } = require("prasasti")

const doc = Document.open("report.pdf")
const pages = doc.extractText(0)

Install with npm install prasasti. It loads in Node.js 18 and later and in Bun, and ships TypeScript types. Every call is synchronous.

Document

Document.open(path, password?)
Document.fromBuffer(buffer, password?)

open reads a file from disk. fromBuffer takes a Buffer or Uint8Array you already have. Both detect the format from the content. Pass password for an encrypted PDF; files with an empty user password open without one.

GetterTypeWhat it holds
formatstring"pdf" or "txt"
pageCountnumberNumber of pages
isEncryptedbooleantrue when the file was encrypted and opened with the password
wasRecoveredbooleantrue when the file was damaged and prasasti rebuilt its structure
MethodReturnsWhat it does
metadata()Record<string, string>PDF document info (Title, Author, ...), or { Encoding } for a text file
extractText(threads?)string[]The text of every page, in reading order
text(threads?)stringEvery page joined by a form feed (\f)
pageText(index)stringThe text of one page, counting from 0

threads defaults to 1. Pass 0 to use every core.

Functions

const { extractText, pageCount, version } = require("prasasti")

const text = extractText("report.pdf")   // pages joined by \f
const pages = pageCount("report.pdf")    // number
console.log(version())                   // "0.1.0"

extractText(path, password?, threads?) and pageCount(path, password?) open the file, do one thing and close it.

From a fetch or an upload

const res = await fetch("https://example.com/report.pdf")
const doc = Document.fromBuffer(Buffer.from(await res.arrayBuffer()))
console.log(doc.text())

Errors

Every call that can fail throws an Error whose message says why: io error: ... when the file can't be read, encrypted: ... for a wrong password, parse error: ... for a file that can't be read as a document, unsupported: ... for an unknown format, and page N out of range from pageText.

Platforms

Prebuilt binaries ship for Linux (x64 and arm64, glibc) and macOS (Intel and Apple silicon). The Windows x64 binary isn't on npm yet; until it is, use the Python package or the command line on Windows.

On this page