UNPKG

ppu-pdf

Version:

Easily extract text from digital PDF files with coordinate and font size included, and optionally group text by lines or render scanned pdf to canvas/png.

70 lines (69 loc) 3.21 kB
import "pdfjs-dist/build/pdf.worker.min.mjs"; import "./pdfjs-workaround"; import { type Canvas } from "@napi-rs/canvas"; import * as pdfjs from "pdfjs-dist/legacy/build/pdf.mjs"; import { PdfReaderCommon } from "./pdf-reader-common"; import { type CanvasMap, type CompactPageLines, type PageLines, type PageTexts, type PdfCompactLineAlgorithm, type PdfReaderOptions, type PdfScannedThreshold } from "./pdf.interface"; /** * PdfReaderLegacy class based on pdfjs-dist for reading and processing PDF documents. */ export declare class PdfReaderLegacy extends PdfReaderCommon { private options; private startIndex; constructor(options?: Partial<PdfReaderOptions>); /** * Opens a PDF document from a file path or an ArrayBuffer. * @param filename - The file path or ArrayBuffer of the PDF document. * @returns The opened PDFDocument instance. */ open(filename: string | ArrayBuffer): Promise<pdfjs.PDFDocumentProxy>; /** * Renders all pages of a PDF document into canvases. * @param doc - The PDFDocumentProxy to render. * @returns A map of page numbers to Canvas instances. */ renderAll(doc: pdfjs.PDFDocumentProxy): Promise<CanvasMap>; private getCanvas; /** * Extracts text from all pages of a PDF document. * @param doc - The PDFDocumentProxy to extract text from. * @returns A map of page numbers to extracted text data. */ getTexts(pdf: pdfjs.PDFDocumentProxy): Promise<PageTexts>; private extractTexts; private mapTokenToPdfWord; private mergeTextContent; private filterTextContent; /** * Converts extracted text into structured lines. * @param pageTexts - The extracted text data from a PDF. * @returns A map of page numbers to structured lines. */ getLinesFromTexts(pageTexts: PageTexts): PageLines; /** * Converts extracted text into compact structured lines using a specified algorithm. * @param pageTexts - The extracted text data from a PDF. * @param algorithm - The algorithm for compacting lines (default: "middleY"). * @returns A map of page numbers to compact structured lines. */ getCompactLinesFromTexts(pageTexts: PageTexts, algorithm?: PdfCompactLineAlgorithm): CompactPageLines; /** * Determines if the PDF document is scanned based on text thresholds. * @param pageTexts - The extracted text data from a PDF. * @param options - The threshold options for scanned detection. * @returns True if the document is likely scanned, false otherwise. */ isScanned(pageTexts: PageTexts, options?: PdfScannedThreshold): boolean; /** * Saves rendered canvases as image files. * @param canvasMap - The map of canvases to save. * @param filename - The base filename for the output images. * @param foldername - The folder to save the images in (default: "out"). */ dumpCanvasMap(canvasMap: Map<number, Canvas>, filename: string, foldername?: string): Promise<void>; /** * Destroys the PDF document instance to free memory. * @param doc - The PDFDocumentProxy instance to destroy. */ destroy(pdf: pdfjs.PDFDocumentProxy): Promise<void>; }