unpdf 1.9.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,98 +1,130 @@
1
1
  import { DocumentInitParameters, PDFDocumentProxy } from "./types/src/display/api.js";
2
- import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
3
- import { Metadata } from "./types/src/display/metadata.js";
2
+ import "./types/src/display/optional_content_config.js";
4
3
  import * as PDFJS from "./types/src/pdf.js";
5
- //#region src/_internal/canvas.d.ts
6
- interface CanvasFactoryContext {
7
- canvas?: HTMLCanvasElement | Canvas;
8
- context?: CanvasRenderingContext2D | SKRSContext2D | null;
9
- }
4
+ import { Metadata, Metadata as Metadata$1 } from "./types/src/display/metadata.js";
10
5
  /**
11
- * Derived from the PDF.js project by the Mozilla Foundation.
12
- * @see https://github.com/mozilla/pdf.js/blob/b8de9a372f9bbf7e33adb362eeae5ef1919dba73/src/display/canvas_factory.js#L18
13
- * @license Apache-2.0
6
+ * Axis-aligned box in PDF user space, origin bottom-left: in the coordinates of
7
+ * the page's `/MediaBox`, unrotated by its `/Rotate`, and unscaled by its
8
+ * `/UserUnit`.
14
9
  */
15
- declare class BaseCanvasFactory {
16
- #private;
17
- constructor({ enableHWA }?: {
18
- enableHWA?: boolean | undefined;
19
- });
20
- create(width: number, height: number): {
21
- canvas: Canvas | HTMLCanvasElement;
22
- context: SKRSContext2D | CanvasRenderingContext2D | null;
23
- };
24
- reset({ canvas }: CanvasFactoryContext, width: number, height: number): void;
25
- destroy(context: CanvasFactoryContext): void;
26
- _createCanvas(width: number, height: number): HTMLCanvasElement | Canvas;
27
- }
28
- /**
29
- * Derived from the PDF.js project by the Mozilla Foundation.
30
- * @see https://github.com/mozilla/pdf.js/blob/b8de9a372f9bbf7e33adb362eeae5ef1919dba73/src/display/canvas_factory.js#L18
31
- * @license Apache-2.0
32
- */
33
- declare class DOMCanvasFactory extends BaseCanvasFactory {
34
- _document: Document;
35
- constructor({ ownerDocument, enableHWA }?: {
36
- ownerDocument?: Document | undefined;
37
- enableHWA?: boolean | undefined;
38
- });
39
- _createCanvas(width: number, height: number): HTMLCanvasElement;
10
+ interface Bounds {
11
+ x: number;
12
+ y: number;
13
+ width: number;
14
+ height: number;
40
15
  }
41
- declare class NodeCanvasFactory extends BaseCanvasFactory {
42
- constructor({ enableHWA }?: {
43
- enableHWA?: boolean | undefined;
44
- });
45
- _createCanvas(width: number, height: number): Canvas;
16
+ interface ExtractImagesOptions {
17
+ /**
18
+ * Encoding of each image's `data`: raw RGBA pixels, or a PNG file.
19
+ *
20
+ * @default 'rgba'
21
+ */
22
+ format?: "rgba" | "png";
46
23
  }
47
- //#endregion
48
- //#region src/image.d.ts
49
- interface ExtractedImageObject {
50
- data: Uint8ClampedArray;
24
+ interface ExtractedImage {
25
+ /** RGBA pixels, four bytes per pixel, row by row from the top – or PNG bytes with `format: 'png'`. */
26
+ data: Uint8Array;
51
27
  width: number;
52
28
  height: number;
53
- channels: 1 | 3 | 4;
54
- key: string;
29
+ /** PDF.js object ID of the image, `null` for a small inline image, which PDF.js passes without one. */
30
+ key: string | null;
31
+ /** Box the image covers on the page, in PDF user space with the origin at the bottom left. */
32
+ bounds: Bounds;
55
33
  }
56
34
  /**
57
- * Extracts images from a specific page of a PDF document, including necessary metadata,
58
- * such as width, height, and calculated color channels.
35
+ * Extracts the images painted on a page – image XObjects, inline images, and
36
+ * image masks – as RGBA pixels. Image masks come out opaque black where they
37
+ * paint and transparent elsewhere, since their color is the fill color at the
38
+ * time of painting. A 1×1 image mask that paints its pixel, which PDF.js
39
+ * turns into a solid fill, is skipped.
40
+ *
41
+ * @remarks
42
+ * A caller-supplied document proxy created with `isOffscreenCanvasSupported`
43
+ * or `isImageDecoderSupported` (both on by default outside of Node.js) hands
44
+ * some images out as bitmaps instead of pixels, which are skipped.
59
45
  */
60
- declare function extractImages$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number): Promise<ExtractedImageObject[]>;
61
- declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number, options?: {
62
- canvasImport?: () => Promise<typeof import('@napi-rs/canvas')>;
63
- /** @default 1.0 */
46
+ export declare function extractImages(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number, options?: ExtractImagesOptions): Promise<ExtractedImage[]>;
47
+ interface RenderPageAsImageOptions {
48
+ canvasImport?: () => Promise<typeof import("@napi-rs/canvas")>;
49
+ /** @default 1 */
64
50
  scale?: number;
65
51
  width?: number;
66
52
  height?: number;
53
+ /** @default 'png' */
54
+ format?: "png" | "jpeg" | "webp";
55
+ /** Encoder quality from 0 to 1, for `jpeg` and `webp` only. */
56
+ quality?: number;
57
+ toDataURL?: boolean;
58
+ }
59
+ export declare function renderPageAsImage(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number, options?: RenderPageAsImageOptions & {
67
60
  toDataURL?: false;
68
- }): Promise<ArrayBuffer>;
69
- declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number, options: {
70
- canvasImport?: () => Promise<typeof import('@napi-rs/canvas')>;
71
- /** @default 1.0 */
72
- scale?: number;
73
- width?: number;
74
- height?: number;
61
+ }): Promise<Uint8Array>;
62
+ export declare function renderPageAsImage(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number, options: RenderPageAsImageOptions & {
75
63
  toDataURL: true;
76
64
  }): Promise<string>;
77
- export declare function createIsomorphicCanvasFactory(canvasImport?: () => Promise<typeof import('@napi-rs/canvas')>): Promise<typeof DOMCanvasFactory | typeof NodeCanvasFactory>;
78
- //#endregion
79
- //#region src/links.d.ts
80
- declare function extractLinks$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
65
+ export declare function renderPageAsImage(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number, options?: RenderPageAsImageOptions): Promise<Uint8Array | string>;
66
+ interface PageInspection {
67
+ /** Characters of all text on the page, visible or not, whitespace excluded. */
68
+ textLength: number;
69
+ /**
70
+ * Characters that paint nothing: drawn with text rendering mode 3 or 7 –
71
+ * typically the OCR layer of a scan – or in a layer the document hides.
72
+ * Whitespace excluded.
73
+ */
74
+ invisibleTextLength: number;
75
+ /** Image XObjects, inline images, and image masks painted on the page. */
76
+ imageCount: number;
77
+ /**
78
+ * Fraction of the page's view box covered by images, from 0 to 1. Overlapping
79
+ * images count once, and a rotated or skewed image counts with its
80
+ * axis-aligned bounds.
81
+ */
82
+ imageCoverage: number;
83
+ }
84
+ /**
85
+ * Measures the text and images of every page, such as to tell scanned pages
86
+ * from born-digital ones. Where that line lies is left to the caller.
87
+ */
88
+ export declare function inspectPages(data: DocumentInitParameters["data"] | PDFDocumentProxy): Promise<{
81
89
  totalPages: number;
82
- links: string[];
90
+ pages: PageInspection[];
83
91
  }>;
84
- //#endregion
85
- //#region src/meta.d.ts
86
- declare function getMeta$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options?: {
87
- parseDates?: boolean;
92
+ export declare function extractLinks(data: DocumentInitParameters["data"] | PDFDocumentProxy): Promise<{
93
+ totalPages: number;
94
+ links: string[][];
95
+ }>;
96
+ interface PDFInfo<DateValue = string> {
97
+ PDFFormatVersion: string;
98
+ Language: string | null;
99
+ EncryptFilterName: string | null;
100
+ IsLinearized: boolean;
101
+ IsAcroFormPresent: boolean;
102
+ IsXFAPresent: boolean;
103
+ IsCollectionPresent: boolean;
104
+ IsSignaturesPresent: boolean;
105
+ Title?: string;
106
+ Author?: string;
107
+ Subject?: string;
108
+ Keywords?: string;
109
+ Creator?: string;
110
+ Producer?: string;
111
+ CreationDate?: DateValue;
112
+ ModDate?: DateValue;
113
+ Trapped?: {
114
+ name: string;
115
+ };
116
+ /** Non-standard `/Info` entries, keyed by their PDF name. */
117
+ Custom?: Map<string, string | number | boolean | {
118
+ name: string;
119
+ }>;
120
+ }
121
+ export declare function getMeta<ParseDates extends boolean = false>(data: DocumentInitParameters["data"] | PDFDocumentProxy, options?: {
122
+ parseDates?: ParseDates;
88
123
  }): Promise<{
89
- info: Record<string, any>;
90
- metadata: Metadata;
124
+ info: PDFInfo<ParseDates extends true ? Date | null : string>;
125
+ metadata: Metadata$1 | null;
91
126
  }>;
92
- //#endregion
93
- //#region src/text.d.ts
94
127
  interface StructuredTextItem {
95
- /** Text content. */
96
128
  str: string;
97
129
  /** X position in PDF coordinate space (origin: bottom-left). */
98
130
  x: number;
@@ -104,77 +136,50 @@ interface StructuredTextItem {
104
136
  height: number;
105
137
  /** Font size derived from the transformation matrix. */
106
138
  fontSize: number;
107
- /** Font family resolved from the page's style table, empty when unknown. */
139
+ /** Font family, empty when unknown. */
108
140
  fontFamily: string;
109
- /** Text direction: `"ltr"`, `"rtl"`, or `"ttb"`. */
110
- dir: string;
111
- /** Whether the text item is followed by a line break. */
141
+ dir: "ltr" | "rtl" | "ttb";
112
142
  hasEOL: boolean;
113
143
  }
114
- declare function extractTextItems$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
144
+ export declare function extractTextItems(data: DocumentInitParameters["data"] | PDFDocumentProxy): Promise<{
115
145
  totalPages: number;
116
146
  items: StructuredTextItem[][];
117
147
  }>;
118
- declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options?: {
148
+ export declare function extractText(data: DocumentInitParameters["data"] | PDFDocumentProxy, options?: {
119
149
  mergePages?: false;
120
150
  }): Promise<{
121
151
  totalPages: number;
122
152
  text: string[];
123
153
  }>;
124
- declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options: {
154
+ export declare function extractText(data: DocumentInitParameters["data"] | PDFDocumentProxy, options: {
125
155
  mergePages: true;
126
156
  }): Promise<{
127
157
  totalPages: number;
128
158
  text: string;
129
159
  }>;
130
- declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options?: {
160
+ export declare function extractText(data: DocumentInitParameters["data"] | PDFDocumentProxy, options?: {
131
161
  mergePages?: boolean;
132
162
  }): Promise<{
133
163
  totalPages: number;
134
164
  text: string | string[];
135
165
  }>;
136
- //#endregion
137
- //#region src/config.d.ts
138
166
  /**
139
- * Sets the PDF.js build every other method uses – call it before any of them.
140
- * Without it, the serverless build is used.
167
+ * Creates a `PDFDocumentProxy` from binary PDF data, with `useSystemFonts:
168
+ * true`. In Node.js with `pdfjs-dist` installed, it uses that package's
169
+ * standard fonts instead of system fonts, and its packed CMaps.
170
+ */
171
+ export declare function getDocumentProxy(data: DocumentInitParameters["data"], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
172
+ /**
173
+ * Sets the PDF.js build every other method uses. The build loads on first use,
174
+ * so call this before any other method. Without it, the serverless build is
175
+ * used.
141
176
  *
142
177
  * @example
143
178
  * // In Node.js, the legacy build of `pdfjs-dist` (install it first)
144
179
  * import { definePDFJSModule } from 'unpdf'
145
180
  *
146
- * await definePDFJSModule(() => import('pdfjs-dist/legacy/build/pdf.mjs'))
147
- */
148
- export declare function definePDFJSModule(pdfjs: () => Promise<any>): Promise<void>;
149
- /** @deprecated Use `definePDFJSModule` instead. Will be removed in v2. */
150
- export declare function configureUnPDF(options: {
151
- pdfjs?: () => Promise<any>;
152
- }): Promise<void>;
153
- //#endregion
154
- //#region src/utils.d.ts
155
- /**
156
- * Creates a `PDFDocumentProxy` from binary PDF data.
157
- *
158
- * Applies the following defaults:
159
- * - `useSystemFonts: true`
160
- *
161
- * In Node.js environments, additionally applies:
162
- * - `disableFontFace: true`
163
- * - `standardFontDataUrl` resolved from the local `pdfjs-dist` package
164
- * - `cMapUrl` and `cMapPacked` resolved from the local `pdfjs-dist` package
181
+ * definePDFJSModule(() => import('pdfjs-dist/legacy/build/pdf.mjs'))
165
182
  */
166
- export declare function getDocumentProxy(data: DocumentInitParameters['data'], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
183
+ export declare function definePDFJSModule(pdfjs: () => Promise<any>): void;
167
184
  export declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
168
- export declare function resolvePDFJSImport(pdfjsResolver?: () => Promise<any>, { reload }?: {
169
- reload?: boolean | undefined;
170
- }): Promise<void>;
171
- //#endregion
172
- //#region src/index.d.ts
173
- export declare const getMeta: typeof getMeta$1;
174
- export declare const extractText: typeof extractText$1;
175
- export declare const extractTextItems: typeof extractTextItems$1;
176
- export declare const extractImages: typeof extractImages$1;
177
- export declare const renderPageAsImage: typeof renderPageAsImage$1;
178
- export declare const extractLinks: typeof extractLinks$1;
179
- //#endregion
180
- export type { StructuredTextItem };
185
+ export type { Bounds, ExtractImagesOptions, ExtractedImage, Metadata, PDFInfo, PageInspection, RenderPageAsImageOptions, StructuredTextItem };