unpdf 1.9.0 → 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,109 +1,130 @@
1
1
  import { DocumentInitParameters, PDFDocumentProxy } from "./types/src/display/api.js";
2
- import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
2
+ import "./types/src/display/optional_content_config.js";
3
3
  import * as PDFJS from "./types/src/pdf.js";
4
- //#region src/_internal/canvas.d.ts
5
- interface CanvasFactoryContext {
6
- canvas?: HTMLCanvasElement | Canvas;
7
- context?: CanvasRenderingContext2D | SKRSContext2D | null;
8
- }
4
+ import { Metadata, Metadata as Metadata$1 } from "./types/src/display/metadata.js";
9
5
  /**
10
- * Derived from the PDF.js project by the Mozilla Foundation.
11
- * @see https://github.com/mozilla/pdf.js/blob/b8de9a372f9bbf7e33adb362eeae5ef1919dba73/src/display/canvas_factory.js#L18
12
- * @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`.
13
9
  */
14
- declare class BaseCanvasFactory {
15
- #private;
16
- constructor({ enableHWA }?: {
17
- enableHWA?: boolean | undefined;
18
- });
19
- create(width: number, height: number): {
20
- canvas: Canvas | HTMLCanvasElement;
21
- context: SKRSContext2D | CanvasRenderingContext2D | null;
22
- };
23
- reset({ canvas }: CanvasFactoryContext, width: number, height: number): void;
24
- destroy(context: CanvasFactoryContext): void;
25
- _createCanvas(width: number, height: number): HTMLCanvasElement | Canvas;
26
- }
27
- /**
28
- * Derived from the PDF.js project by the Mozilla Foundation.
29
- * @see https://github.com/mozilla/pdf.js/blob/b8de9a372f9bbf7e33adb362eeae5ef1919dba73/src/display/canvas_factory.js#L18
30
- * @license Apache-2.0
31
- */
32
- declare class DOMCanvasFactory extends BaseCanvasFactory {
33
- _document: Document;
34
- constructor({ ownerDocument, enableHWA }?: {
35
- ownerDocument?: Document | undefined;
36
- enableHWA?: boolean | undefined;
37
- });
38
- _createCanvas(width: number, height: number): HTMLCanvasElement;
10
+ interface Bounds {
11
+ x: number;
12
+ y: number;
13
+ width: number;
14
+ height: number;
39
15
  }
40
- declare class NodeCanvasFactory extends BaseCanvasFactory {
41
- constructor({ enableHWA }?: {
42
- enableHWA?: boolean | undefined;
43
- });
44
- _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";
45
23
  }
46
- //#endregion
47
- //#region src/image.d.ts
48
- interface ExtractedImageObject {
49
- 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;
50
27
  width: number;
51
28
  height: number;
52
- channels: 1 | 3 | 4;
53
- 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;
54
33
  }
55
34
  /**
56
- * Extracts images from a specific page of a PDF document, including necessary metadata,
57
- * such as width, height, and calculated color channels.
58
- *
59
- * @example
60
- * const imagesData = await extractImages(pdf, pageNum)
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.
61
40
  *
62
- * for (const imgData of imagesData) {
63
- * const imageIndex = totalImagesProcessed + 1
64
- * await sharp(imgData.data, {
65
- * raw: { width: imgData.width, height: imgData.height, channels: imgData.channels }
66
- * })
67
- * .png()
68
- * .toFile(`${imageIndex}.png`)
69
- * }
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.
70
45
  */
71
- declare function extractImages$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number): Promise<ExtractedImageObject[]>;
72
- declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number, options?: {
73
- canvasImport?: () => Promise<typeof import('@napi-rs/canvas')>;
74
- /** @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 */
75
50
  scale?: number;
76
51
  width?: number;
77
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 & {
78
60
  toDataURL?: false;
79
- }): Promise<ArrayBuffer>;
80
- declare function renderPageAsImage$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, pageNumber: number, options: {
81
- canvasImport?: () => Promise<typeof import('@napi-rs/canvas')>;
82
- /** @default 1.0 */
83
- scale?: number;
84
- width?: number;
85
- height?: number;
61
+ }): Promise<Uint8Array>;
62
+ export declare function renderPageAsImage(data: DocumentInitParameters["data"] | PDFDocumentProxy, pageNumber: number, options: RenderPageAsImageOptions & {
86
63
  toDataURL: true;
87
64
  }): Promise<string>;
88
- export declare function createIsomorphicCanvasFactory(canvasImport?: () => Promise<typeof import('@napi-rs/canvas')>): Promise<typeof DOMCanvasFactory | typeof NodeCanvasFactory>;
89
- //#endregion
90
- //#region src/links.d.ts
91
- 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<{
92
89
  totalPages: number;
93
- links: string[];
90
+ pages: PageInspection[];
94
91
  }>;
95
- //#endregion
96
- //#region src/meta.d.ts
97
- declare function getMeta$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options?: {
98
- 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;
99
123
  }): Promise<{
100
- info: Record<string, any>;
101
- metadata: import("./types/src/display/metadata.js").Metadata;
124
+ info: PDFInfo<ParseDates extends true ? Date | null : string>;
125
+ metadata: Metadata$1 | null;
102
126
  }>;
103
- //#endregion
104
- //#region src/text.d.ts
105
127
  interface StructuredTextItem {
106
- /** Text content. */
107
128
  str: string;
108
129
  /** X position in PDF coordinate space (origin: bottom-left). */
109
130
  x: number;
@@ -115,78 +136,50 @@ interface StructuredTextItem {
115
136
  height: number;
116
137
  /** Font size derived from the transformation matrix. */
117
138
  fontSize: number;
118
- /** Font family resolved from the page's style table, empty when unknown. */
139
+ /** Font family, empty when unknown. */
119
140
  fontFamily: string;
120
- /** Text direction: `"ltr"`, `"rtl"`, or `"ttb"`. */
121
- dir: string;
122
- /** Whether the text item is followed by a line break. */
141
+ dir: "ltr" | "rtl" | "ttb";
123
142
  hasEOL: boolean;
124
143
  }
125
- declare function extractTextItems$1(data: DocumentInitParameters['data'] | PDFDocumentProxy): Promise<{
144
+ export declare function extractTextItems(data: DocumentInitParameters["data"] | PDFDocumentProxy): Promise<{
126
145
  totalPages: number;
127
146
  items: StructuredTextItem[][];
128
147
  }>;
129
- declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options?: {
148
+ export declare function extractText(data: DocumentInitParameters["data"] | PDFDocumentProxy, options?: {
130
149
  mergePages?: false;
131
150
  }): Promise<{
132
151
  totalPages: number;
133
152
  text: string[];
134
153
  }>;
135
- declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options: {
154
+ export declare function extractText(data: DocumentInitParameters["data"] | PDFDocumentProxy, options: {
136
155
  mergePages: true;
137
156
  }): Promise<{
138
157
  totalPages: number;
139
158
  text: string;
140
159
  }>;
141
- declare function extractText$1(data: DocumentInitParameters['data'] | PDFDocumentProxy, options?: {
160
+ export declare function extractText(data: DocumentInitParameters["data"] | PDFDocumentProxy, options?: {
142
161
  mergePages?: boolean;
143
162
  }): Promise<{
144
163
  totalPages: number;
145
164
  text: string | string[];
146
165
  }>;
147
- //#endregion
148
- //#region src/config.d.ts
149
166
  /**
150
- * By default, unpdf will use the latest version of PDF.js compiled for
151
- * serverless environments. If you want to use a different version, you can
152
- * provide a custom resolver function.
153
- *
154
- * @example
155
- * // Use the official PDF.js build (make sure to install it first)
156
- * import { definePDFJSModule } from 'unpdf'
157
- *
158
- * await definePDFJSModule(() => import('pdfjs-dist'))
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.
159
170
  */
160
- export declare function definePDFJSModule(pdfjs: () => Promise<any>): Promise<void>;
161
- /** @deprecated Use `definePDFJSModule` instead. Will be removed in v2. */
162
- export declare function configureUnPDF(options: {
163
- pdfjs?: () => Promise<any>;
164
- }): Promise<void>;
165
- //#endregion
166
- //#region src/utils.d.ts
171
+ export declare function getDocumentProxy(data: DocumentInitParameters["data"], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
167
172
  /**
168
- * Returns a `PDFDocumentProxy` instance from a given binary data.
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.
169
176
  *
170
- * Applies the following defaults:
171
- * - `useSystemFonts: true`
177
+ * @example
178
+ * // In Node.js, the legacy build of `pdfjs-dist` (install it first)
179
+ * import { definePDFJSModule } from 'unpdf'
172
180
  *
173
- * In Node.js environments, additionally applies:
174
- * - `disableFontFace: true`
175
- * - `standardFontDataUrl` resolved from the local `pdfjs-dist` package
176
- * - `cMapUrl` and `cMapPacked` resolved from the local `pdfjs-dist` package
181
+ * definePDFJSModule(() => import('pdfjs-dist/legacy/build/pdf.mjs'))
177
182
  */
178
- export declare function getDocumentProxy(data: DocumentInitParameters['data'], options?: DocumentInitParameters): Promise<PDFDocumentProxy>;
183
+ export declare function definePDFJSModule(pdfjs: () => Promise<any>): void;
179
184
  export declare function getResolvedPDFJS(): Promise<typeof PDFJS>;
180
- export declare function resolvePDFJSImport(pdfjsResolver?: () => Promise<any>, { reload }?: {
181
- reload?: boolean | undefined;
182
- }): Promise<void>;
183
- //#endregion
184
- //#region src/index.d.ts
185
- export declare const getMeta: typeof getMeta$1;
186
- export declare const extractText: typeof extractText$1;
187
- export declare const extractTextItems: typeof extractTextItems$1;
188
- export declare const extractImages: typeof extractImages$1;
189
- export declare const renderPageAsImage: typeof renderPageAsImage$1;
190
- export declare const extractLinks: typeof extractLinks$1;
191
- //#endregion
192
- export type { StructuredTextItem };
185
+ export type { Bounds, ExtractImagesOptions, ExtractedImage, Metadata, PDFInfo, PageInspection, RenderPageAsImageOptions, StructuredTextItem };