How PdfReader works and its item types
masterThe PdfReader class is the primary interface for parsing PDF files. You can instantiate it with an optional configuration object, such as { debug: true } for troubleshooting.
Parsing is performed using either parseFileItems(filename, callback) or parseBuffer(buffer, callback). Both methods use a callback that is invoked for every item found during parsing.
An item object can be one of the following:
null: Indicates the end of the file or that an error occurred.{file: {path: string}}: File metadata, always the first item.{page: integer, width: float, height: float}: Page metadata (page numbers start at 1). This acts as a boundary for coordinate-based text processing.{text: string, x: float, y: float, w: float, ...}: A text item containing the string and its 2D AABB coordinates on the page.
import { PdfReader } from "pdfreader";
new PdfReader().parseFileItems("test/sample.pdf", (err, item) => {
if (err) console.error("error:", err);
else if (!item) console.warn("end of file");
else if (item.text) console.log(item.text);
});