pdfium-native 0.8.0 → 0.9.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/CHANGELOG.md +30 -1
- package/README.md +302 -3
- package/dist/document.d.ts +43 -2
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +41 -0
- package/dist/document.js.map +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +9 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +60 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -1
- package/dist/page.d.ts +22 -1
- package/dist/page.d.ts.map +1 -1
- package/dist/page.js +41 -0
- package/dist/page.js.map +1 -1
- package/dist/types.d.ts +243 -0
- package/dist/types.d.ts.map +1 -1
- package/lib/document.ts +70 -1
- package/lib/errors.ts +10 -0
- package/lib/index.ts +159 -0
- package/lib/page.ts +52 -0
- package/lib/types.ts +268 -0
- package/package.json +1 -1
- package/scripts/generate-fixtures.mjs +252 -0
- package/scripts/prebuilds.json +10 -10
- package/src/annotations_worker.h +1 -1
- package/src/assemble_worker.h +235 -0
- package/src/attachments_worker.h +1 -39
- package/src/attachments_write_worker.h +158 -0
- package/src/characters_worker.h +197 -0
- package/src/destinations_worker.h +160 -0
- package/src/document.h +86 -0
- package/src/document_io.h +78 -0
- package/src/flatten_worker.h +133 -0
- package/src/javascript_worker.h +105 -0
- package/src/napi_helpers.h +55 -0
- package/src/page.h +50 -0
- package/src/pdfium_addon.cc +310 -0
- package/src/signatures_worker.h +248 -0
- package/src/split_merge_worker.h +2 -93
- package/src/structtree_worker.h +164 -0
package/lib/errors.ts
CHANGED
|
@@ -40,12 +40,22 @@ export class PDFiumSecurityError extends PDFiumError {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
// The native layer tags PDFium's own failures with a "CODE:message" envelope
|
|
44
|
+
// (see GetPdfiumErrorMessage in src/napi_helpers.h). Everything else a worker
|
|
45
|
+
// reports is free text that routinely contains a colon of its own — "Page index
|
|
46
|
+
// out of range: 4", "Parent directory does not exist: /tmp/x" — so only a
|
|
47
|
+
// recognized code may be stripped off. Treating any prefix as a code would eat
|
|
48
|
+
// the useful half of those messages.
|
|
49
|
+
const NATIVE_ERROR_CODES = new Set(['FILE', 'FORMAT', 'PASSWORD', 'SECURITY', 'PAGE', 'UNKNOWN']);
|
|
50
|
+
|
|
43
51
|
export function parseNativeError(err: unknown): PDFiumError {
|
|
44
52
|
const msg = err instanceof Error ? err.message : String(err);
|
|
45
53
|
const colonIdx = msg.indexOf(':');
|
|
46
54
|
if (colonIdx === -1) return new PDFiumError('UNKNOWN', msg);
|
|
47
55
|
|
|
48
56
|
const code = msg.slice(0, colonIdx);
|
|
57
|
+
if (!NATIVE_ERROR_CODES.has(code)) return new PDFiumError('UNKNOWN', msg);
|
|
58
|
+
|
|
49
59
|
const text = msg.slice(colonIdx + 1);
|
|
50
60
|
|
|
51
61
|
switch (code) {
|
package/lib/index.ts
CHANGED
|
@@ -4,9 +4,15 @@ import { fileURLToPath } from 'node:url';
|
|
|
4
4
|
import { withConcurrency } from './concurrency.js';
|
|
5
5
|
import { PDFiumDocument } from './document.js';
|
|
6
6
|
import { parseNativeError } from './errors.js';
|
|
7
|
+
import { isNativeIndex } from './validate.js';
|
|
7
8
|
import type {
|
|
9
|
+
AddAttachmentsOptions,
|
|
10
|
+
AssemblePagesOptions,
|
|
11
|
+
AttachmentInput,
|
|
12
|
+
FlattenDocumentOptions,
|
|
8
13
|
MergeDocumentInput,
|
|
9
14
|
MergeDocumentsOptions,
|
|
15
|
+
NUpPagesOptions,
|
|
10
16
|
NativeAddon,
|
|
11
17
|
SplitDocumentOptions,
|
|
12
18
|
} from './types.js';
|
|
@@ -89,6 +95,146 @@ export async function mergeDocuments(
|
|
|
89
95
|
}
|
|
90
96
|
}
|
|
91
97
|
|
|
98
|
+
/**
|
|
99
|
+
* Builds a new PDF from selected pages of `input`, in the order given.
|
|
100
|
+
*
|
|
101
|
+
* Unlike {@link splitDocument}, which only cuts a document into consecutive
|
|
102
|
+
* runs, the index list here is taken literally: pages may be reordered, left
|
|
103
|
+
* out, or repeated. `assemblePages(pdf, [3, 0, 0])` yields a three-page
|
|
104
|
+
* document whose first page is the original page 3 and whose next two are both
|
|
105
|
+
* the original page 0.
|
|
106
|
+
*/
|
|
107
|
+
export async function assemblePages(
|
|
108
|
+
input: PdfInput,
|
|
109
|
+
pages: number[],
|
|
110
|
+
options: AssemblePagesOptions & { output: string },
|
|
111
|
+
): Promise<void>;
|
|
112
|
+
export async function assemblePages(
|
|
113
|
+
input: PdfInput,
|
|
114
|
+
pages: number[],
|
|
115
|
+
options?: AssemblePagesOptions,
|
|
116
|
+
): Promise<Buffer>;
|
|
117
|
+
export async function assemblePages(
|
|
118
|
+
input: PdfInput,
|
|
119
|
+
pages: number[],
|
|
120
|
+
options?: AssemblePagesOptions,
|
|
121
|
+
): Promise<Buffer | void> {
|
|
122
|
+
// ToInt32 in the native layer would wrap a fractional or oversized index onto
|
|
123
|
+
// a different, valid page and silently assemble the wrong document.
|
|
124
|
+
for (const page of pages) {
|
|
125
|
+
if (!isNativeIndex(page)) {
|
|
126
|
+
throw new RangeError(`Page index must be a 32-bit integer, got ${page}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
return await withConcurrency(() => addon.assemblePages(input, pages, options));
|
|
131
|
+
} catch (err) {
|
|
132
|
+
throw parseNativeError(err);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Imposes `columns × rows` source pages onto each page of a new document —
|
|
138
|
+
* the classic "n-up" layout for handouts and proof sheets.
|
|
139
|
+
*
|
|
140
|
+
* The output sheet defaults to the size of the first source page, so a 2×2
|
|
141
|
+
* n-up of A4 pages lands on A4 with each source page scaled to a quarter.
|
|
142
|
+
*/
|
|
143
|
+
export async function nUpPages(
|
|
144
|
+
input: PdfInput,
|
|
145
|
+
options: NUpPagesOptions & { output: string },
|
|
146
|
+
): Promise<void>;
|
|
147
|
+
export async function nUpPages(input: PdfInput, options: NUpPagesOptions): Promise<Buffer>;
|
|
148
|
+
export async function nUpPages(input: PdfInput, options: NUpPagesOptions): Promise<Buffer | void> {
|
|
149
|
+
// Validate here rather than natively so callers get a plain RangeError
|
|
150
|
+
// instead of a PDFiumError wrapping one.
|
|
151
|
+
for (const [name, value] of [
|
|
152
|
+
['columns', options.columns],
|
|
153
|
+
['rows', options.rows],
|
|
154
|
+
] as const) {
|
|
155
|
+
if (!Number.isInteger(value) || value < 1) {
|
|
156
|
+
throw new RangeError(`${name} must be a positive integer, got ${value}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
return await withConcurrency(() => addon.nUpPages(input, options));
|
|
161
|
+
} catch (err) {
|
|
162
|
+
throw parseNativeError(err);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Embeds files into a copy of `input`, returning the new PDF.
|
|
168
|
+
*
|
|
169
|
+
* **This does not produce a conformant PDF/A-3 e-invoice.** PDFium's write API
|
|
170
|
+
* reaches only the file name, the bytes and the `/Params` dates. It cannot set
|
|
171
|
+
* the MIME type (`/Subtype`), the description (`/Desc`), the
|
|
172
|
+
* `/AFRelationship` that marks an attachment as the invoice source, the catalog
|
|
173
|
+
* `/AF` array, an OutputIntent, or the XMP metadata — all of which ZUGFeRD,
|
|
174
|
+
* Factur-X and XRechnung require. Use this for ordinary attachments; reach for
|
|
175
|
+
* a PDF/A toolchain to *produce* e-invoices. Reading them back is fully
|
|
176
|
+
* supported via {@link PDFiumDocument.getAttachments}.
|
|
177
|
+
*/
|
|
178
|
+
export async function addAttachments(
|
|
179
|
+
input: PdfInput,
|
|
180
|
+
attachments: AttachmentInput[],
|
|
181
|
+
options: AddAttachmentsOptions & { output: string },
|
|
182
|
+
): Promise<void>;
|
|
183
|
+
export async function addAttachments(
|
|
184
|
+
input: PdfInput,
|
|
185
|
+
attachments: AttachmentInput[],
|
|
186
|
+
options?: AddAttachmentsOptions,
|
|
187
|
+
): Promise<Buffer>;
|
|
188
|
+
export async function addAttachments(
|
|
189
|
+
input: PdfInput,
|
|
190
|
+
attachments: AttachmentInput[],
|
|
191
|
+
options?: AddAttachmentsOptions,
|
|
192
|
+
): Promise<Buffer | void> {
|
|
193
|
+
try {
|
|
194
|
+
return await withConcurrency(() => addon.addAttachments(input, attachments, options));
|
|
195
|
+
} catch (err) {
|
|
196
|
+
throw parseNativeError(err);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Flattens annotations and form widgets into the page content of a copy of
|
|
202
|
+
* `input`, returning the new PDF.
|
|
203
|
+
*
|
|
204
|
+
* The result renders identically in every viewer but is no longer interactive,
|
|
205
|
+
* which is the point when archiving or handing a filled form to a system that
|
|
206
|
+
* ignores annotations.
|
|
207
|
+
*
|
|
208
|
+
* This bakes in the appearance streams the document already carries rather than
|
|
209
|
+
* regenerating them, since no form-fill environment is initialised. That is
|
|
210
|
+
* correct for any PDF whose widgets have valid `/AP` entries — what every
|
|
211
|
+
* mainstream producer writes. A form flagged `/NeedAppearances`, which asks the
|
|
212
|
+
* viewer to regenerate appearances itself, is the case this cannot help with.
|
|
213
|
+
*/
|
|
214
|
+
export async function flattenDocument(
|
|
215
|
+
input: PdfInput,
|
|
216
|
+
options: FlattenDocumentOptions & { output: string },
|
|
217
|
+
): Promise<void>;
|
|
218
|
+
export async function flattenDocument(
|
|
219
|
+
input: PdfInput,
|
|
220
|
+
options?: FlattenDocumentOptions,
|
|
221
|
+
): Promise<Buffer>;
|
|
222
|
+
export async function flattenDocument(
|
|
223
|
+
input: PdfInput,
|
|
224
|
+
options?: FlattenDocumentOptions,
|
|
225
|
+
): Promise<Buffer | void> {
|
|
226
|
+
for (const page of options?.pages ?? []) {
|
|
227
|
+
if (!isNativeIndex(page)) {
|
|
228
|
+
throw new RangeError(`Page index must be a 32-bit integer, got ${page}`);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
try {
|
|
232
|
+
return await withConcurrency(() => addon.flattenDocument(input, options));
|
|
233
|
+
} catch (err) {
|
|
234
|
+
throw parseNativeError(err);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
92
238
|
export { concurrency } from './concurrency.js';
|
|
93
239
|
export { PDFiumDocument } from './document.js';
|
|
94
240
|
export {
|
|
@@ -100,24 +246,34 @@ export {
|
|
|
100
246
|
} from './errors.js';
|
|
101
247
|
export { PDFiumPage } from './page.js';
|
|
102
248
|
export type {
|
|
249
|
+
AddAttachmentsOptions,
|
|
103
250
|
Annotation,
|
|
104
251
|
AnnotationBorder,
|
|
105
252
|
AnnotationType,
|
|
253
|
+
AssemblePagesOptions,
|
|
106
254
|
Attachment,
|
|
255
|
+
AttachmentInput,
|
|
107
256
|
Bookmark,
|
|
108
257
|
DocumentMetadata,
|
|
258
|
+
DestinationView,
|
|
109
259
|
DocumentPermissions,
|
|
110
260
|
FormField,
|
|
111
261
|
FormFieldOption,
|
|
262
|
+
FlattenDocumentOptions,
|
|
112
263
|
FormFieldType,
|
|
113
264
|
GetAttachmentOptions,
|
|
265
|
+
GetCharactersOptions,
|
|
266
|
+
GetSignatureContentsOptions,
|
|
114
267
|
ImageColorspace,
|
|
115
268
|
ImagePageObject,
|
|
116
269
|
ImageRenderOptions,
|
|
270
|
+
JavaScriptAction,
|
|
117
271
|
Link,
|
|
118
272
|
LinkActionType,
|
|
119
273
|
MergeDocumentInput,
|
|
120
274
|
MergeDocumentsOptions,
|
|
275
|
+
NUpPagesOptions,
|
|
276
|
+
NamedDestination,
|
|
121
277
|
OtherPageObject,
|
|
122
278
|
PageObject,
|
|
123
279
|
PageObjectBounds,
|
|
@@ -127,7 +283,10 @@ export type {
|
|
|
127
283
|
SearchMatch,
|
|
128
284
|
SearchOptions,
|
|
129
285
|
SearchRect,
|
|
286
|
+
Signature,
|
|
287
|
+
StructElement,
|
|
130
288
|
SplitDocumentOptions,
|
|
289
|
+
TextCharacter,
|
|
131
290
|
TextPageObject,
|
|
132
291
|
TextRenderMode,
|
|
133
292
|
} from './types.js';
|
package/lib/page.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
Annotation,
|
|
5
5
|
FormField,
|
|
6
6
|
GetAttachmentOptions,
|
|
7
|
+
GetCharactersOptions,
|
|
7
8
|
ImagePageObject,
|
|
8
9
|
ImageRenderOptions,
|
|
9
10
|
Link,
|
|
@@ -13,6 +14,8 @@ import type {
|
|
|
13
14
|
PageRenderOptions,
|
|
14
15
|
SearchMatch,
|
|
15
16
|
SearchOptions,
|
|
17
|
+
StructElement,
|
|
18
|
+
TextCharacter,
|
|
16
19
|
} from './types.js';
|
|
17
20
|
|
|
18
21
|
/**
|
|
@@ -56,6 +59,55 @@ export class PDFiumPage {
|
|
|
56
59
|
return withConcurrency(() => this.native.getText());
|
|
57
60
|
}
|
|
58
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Extracts every character with its bounding box, baseline origin, font and
|
|
64
|
+
* rotation — the geometry {@link getText} throws away.
|
|
65
|
+
*
|
|
66
|
+
* Character indices line up with {@link search} results and with the string
|
|
67
|
+
* returned by `getText()`, so a match can be mapped straight back to page
|
|
68
|
+
* coordinates. Dense pages can hold tens of thousands of characters; use
|
|
69
|
+
* `start` and `count` to page through instead of materialising all of them.
|
|
70
|
+
*/
|
|
71
|
+
getCharacters(options?: GetCharactersOptions): Promise<TextCharacter[]> {
|
|
72
|
+
const start = options?.start ?? 0;
|
|
73
|
+
const count = options?.count;
|
|
74
|
+
|
|
75
|
+
// Same 32-bit guard as elsewhere: a fractional or oversized value must not
|
|
76
|
+
// ToInt32-wrap into a different, silently valid range.
|
|
77
|
+
if (!isNativeIndex(start)) {
|
|
78
|
+
return Promise.reject(
|
|
79
|
+
new RangeError(`Character start must be a 32-bit integer, got ${start}`),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (count !== undefined && !isNativeIndex(count)) {
|
|
83
|
+
return Promise.reject(
|
|
84
|
+
new RangeError(`Character count must be a 32-bit integer, got ${count}`),
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
if (start < 0) {
|
|
88
|
+
return Promise.reject(new RangeError(`Character start must not be negative, got ${start}`));
|
|
89
|
+
}
|
|
90
|
+
if (count !== undefined && count < 0) {
|
|
91
|
+
return Promise.reject(new RangeError(`Character count must not be negative, got ${count}`));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return withConcurrency(() => this.native.getCharacters(start, count ?? -1));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Returns the tagged-PDF structure tree for this page — the logical outline
|
|
99
|
+
* (headings, paragraphs, tables, figures) that screen readers follow, and
|
|
100
|
+
* where alternate text lives.
|
|
101
|
+
*
|
|
102
|
+
* The tree is per-page: these are the elements whose content is on this page,
|
|
103
|
+
* not the whole document's tree. Returns an empty array for an untagged page;
|
|
104
|
+
* `document.metadata.isTagged` says up front whether there is anything to
|
|
105
|
+
* find.
|
|
106
|
+
*/
|
|
107
|
+
getStructTree(): Promise<StructElement[]> {
|
|
108
|
+
return withConcurrency(() => this.native.getStructTree());
|
|
109
|
+
}
|
|
110
|
+
|
|
59
111
|
/** Renders the page to a file. */
|
|
60
112
|
render(options: PageRenderOptions & { output: string }): Promise<void>;
|
|
61
113
|
/** Renders the page to an encoded image buffer (JPEG or PNG). */
|
package/lib/types.ts
CHANGED
|
@@ -140,6 +140,92 @@ export interface OtherPageObject extends BasePageObject {
|
|
|
140
140
|
|
|
141
141
|
export type PageObject = TextPageObject | ImagePageObject | OtherPageObject;
|
|
142
142
|
|
|
143
|
+
/**
|
|
144
|
+
* A single character on a page, with the geometry `getText()` discards.
|
|
145
|
+
*/
|
|
146
|
+
export interface TextCharacter {
|
|
147
|
+
/** 0-based index of this character in the page's text. */
|
|
148
|
+
index: number;
|
|
149
|
+
/**
|
|
150
|
+
* The character itself. Empty string when the glyph has no Unicode mapping —
|
|
151
|
+
* check `hasUnicodeMapError` before trusting it.
|
|
152
|
+
*/
|
|
153
|
+
char: string;
|
|
154
|
+
/** Unicode code point, or 0 if the glyph has no mapping. */
|
|
155
|
+
unicode: number;
|
|
156
|
+
/**
|
|
157
|
+
* Tight bounding box in page points, hugging the glyph's ink. Absent if
|
|
158
|
+
* PDFium cannot determine it.
|
|
159
|
+
*/
|
|
160
|
+
bounds?: PageObjectBounds;
|
|
161
|
+
/** X coordinate of the baseline origin in page points. Absent if unavailable. */
|
|
162
|
+
x?: number;
|
|
163
|
+
/** Y coordinate of the baseline origin in page points. Absent if unavailable. */
|
|
164
|
+
y?: number;
|
|
165
|
+
/** Font size in points (the typographic "em" size). */
|
|
166
|
+
fontSize: number;
|
|
167
|
+
/** PostScript name of the font (e.g. 'Helvetica-Bold'). Empty string if unavailable. */
|
|
168
|
+
fontName: string;
|
|
169
|
+
/** Raw PDF font descriptor flags bitmask (see PDF spec Table 123). */
|
|
170
|
+
fontFlags: number;
|
|
171
|
+
/** Font weight (e.g. 400 = normal, 700 = bold). Absent if unavailable. */
|
|
172
|
+
fontWeight?: number;
|
|
173
|
+
/**
|
|
174
|
+
* Rotation of the character in radians, normalized to `[0, 2π)`. 0 for
|
|
175
|
+
* upright text.
|
|
176
|
+
*
|
|
177
|
+
* Note the direction: PDFium derives this as `atan2(c, a)` from the text
|
|
178
|
+
* matrix, so it runs *clockwise*. Text rotated 45° counterclockwise on the
|
|
179
|
+
* page reports ≈ 5.4978 (2π − π/4), not ≈ 0.7854.
|
|
180
|
+
*/
|
|
181
|
+
angle: number;
|
|
182
|
+
/**
|
|
183
|
+
* Whether PDFium synthesized this character rather than reading it from the
|
|
184
|
+
* content stream — line breaks between text runs, and spaces inferred from
|
|
185
|
+
* glyph spacing. Synthesized characters carry no real font, so `fontName` is
|
|
186
|
+
* empty, `fontSize` is 1 and `fontWeight` is absent. Useful when mapping text
|
|
187
|
+
* offsets back to the original PDF.
|
|
188
|
+
*/
|
|
189
|
+
isGenerated: boolean;
|
|
190
|
+
/** Whether this character is a hyphen (relevant when de-hyphenating lines). */
|
|
191
|
+
isHyphen: boolean;
|
|
192
|
+
/** Whether the glyph has no reliable Unicode mapping, making `char` unreliable. */
|
|
193
|
+
hasUnicodeMapError: boolean;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* A node in a tagged PDF's structure tree — the logical document outline
|
|
198
|
+
* (headings, paragraphs, tables, figures) behind the visual layout, which is
|
|
199
|
+
* what screen readers follow.
|
|
200
|
+
*/
|
|
201
|
+
export interface StructElement {
|
|
202
|
+
/** Structure type (/S), e.g. 'H1', 'P', 'Table', 'Figure'. */
|
|
203
|
+
type: string;
|
|
204
|
+
/** Object type (/Type), normally 'StructElem'. Absent if not set. */
|
|
205
|
+
objType?: string;
|
|
206
|
+
/** Human-readable title (/T). Absent if not set. */
|
|
207
|
+
title?: string;
|
|
208
|
+
/** Alternate text (/Alt) — the accessibility description. Absent if not set. */
|
|
209
|
+
altText?: string;
|
|
210
|
+
/** Replacement text (/ActualText) for content whose glyphs differ from its meaning. */
|
|
211
|
+
actualText?: string;
|
|
212
|
+
/** Element identifier (/ID). Absent if not set. */
|
|
213
|
+
id?: string;
|
|
214
|
+
/** Language override (/Lang) for this subtree, e.g. 'de-DE'. Absent if not set. */
|
|
215
|
+
lang?: string;
|
|
216
|
+
/** Marked-content ID tying this element to page content. Absent if the element has none. */
|
|
217
|
+
markedContentId?: number;
|
|
218
|
+
/** Child elements. Absent when the element has no element children. */
|
|
219
|
+
children?: StructElement[];
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export interface GetCharactersOptions {
|
|
223
|
+
/** First character index to return (default: 0). */
|
|
224
|
+
start?: number;
|
|
225
|
+
/** How many characters to return (default: all remaining). */
|
|
226
|
+
count?: number;
|
|
227
|
+
}
|
|
228
|
+
|
|
143
229
|
export interface SearchOptions {
|
|
144
230
|
/** Match case when searching (default: false). */
|
|
145
231
|
caseSensitive?: boolean;
|
|
@@ -413,6 +499,96 @@ export interface GetAttachmentOptions {
|
|
|
413
499
|
output?: string;
|
|
414
500
|
}
|
|
415
501
|
|
|
502
|
+
/**
|
|
503
|
+
* A digital signature as declared by the PDF.
|
|
504
|
+
*
|
|
505
|
+
* These values come straight from the signature dictionary — nothing here is
|
|
506
|
+
* cryptographically verified. A present, well-formed signature says only that
|
|
507
|
+
* the document *claims* to be signed.
|
|
508
|
+
*/
|
|
509
|
+
export interface Signature {
|
|
510
|
+
/** 0-based index of the signature in the document's AcroForm field list. */
|
|
511
|
+
index: number;
|
|
512
|
+
/**
|
|
513
|
+
* Encoding of the signature value (/SubFilter), e.g. 'adbe.pkcs7.detached'
|
|
514
|
+
* or 'ETSI.CAdES.detached'. Empty string if the PDF omits it.
|
|
515
|
+
*/
|
|
516
|
+
subFilter: string;
|
|
517
|
+
/** Reason given for signing (/Reason). Absent if not set. */
|
|
518
|
+
reason?: string;
|
|
519
|
+
/**
|
|
520
|
+
* Time of signing (/M) as a PDF date string (e.g. "D:20250101120000+01'00'").
|
|
521
|
+
* Absent if not set. For PKCS#7 signatures the authoritative time is usually
|
|
522
|
+
* inside the signature blob instead; use this only as a fallback.
|
|
523
|
+
*/
|
|
524
|
+
time?: string;
|
|
525
|
+
/**
|
|
526
|
+
* Certification level for a DocMDP signature: 1 = no changes allowed,
|
|
527
|
+
* 2 = form fill-in and signing allowed, 3 = additionally annotations allowed.
|
|
528
|
+
* Absent for an ordinary (non-certifying) signature.
|
|
529
|
+
*/
|
|
530
|
+
docMdpPermission?: 1 | 2 | 3;
|
|
531
|
+
/**
|
|
532
|
+
* The /ByteRange array covered by the signature digest: a flat list of
|
|
533
|
+
* (offset, length) pairs, exactly as stored in the PDF. A signature covers
|
|
534
|
+
* the whole file when the last pair ends at the file size — anything less
|
|
535
|
+
* means content was appended after signing.
|
|
536
|
+
*/
|
|
537
|
+
byteRange: number[];
|
|
538
|
+
/**
|
|
539
|
+
* Size in bytes of the signature value (/Contents). Read the bytes
|
|
540
|
+
* themselves with {@link PDFiumDocument.getSignatureContents}.
|
|
541
|
+
*/
|
|
542
|
+
contentsLength: number;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
export interface GetSignatureContentsOptions {
|
|
546
|
+
/** Write the signature bytes to this file path instead of returning a Buffer. */
|
|
547
|
+
output?: string;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* A document-level JavaScript action — a script a viewer runs when the
|
|
552
|
+
* document opens.
|
|
553
|
+
*/
|
|
554
|
+
export interface JavaScriptAction {
|
|
555
|
+
/** 0-based index in the document's /Names /JavaScript tree. */
|
|
556
|
+
index: number;
|
|
557
|
+
/** Entry name in the JavaScript name tree. Empty string if unnamed. */
|
|
558
|
+
name: string;
|
|
559
|
+
/** The script source, returned as inert text. Empty string if unreadable. */
|
|
560
|
+
script: string;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** How a viewer should frame the target page when following a destination. */
|
|
564
|
+
export type DestinationView =
|
|
565
|
+
'xyz' | 'fit' | 'fitH' | 'fitV' | 'fitR' | 'fitB' | 'fitBH' | 'fitBV' | 'unknown';
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* A named destination — an anchor that GoTo actions and external links target
|
|
569
|
+
* by name instead of by page number.
|
|
570
|
+
*/
|
|
571
|
+
export interface NamedDestination {
|
|
572
|
+
/** The destination's name, e.g. 'Chapter2'. */
|
|
573
|
+
name: string;
|
|
574
|
+
/** Target page index. Absent if the destination cannot be resolved to a page. */
|
|
575
|
+
pageIndex?: number;
|
|
576
|
+
/** Fit type declared by the destination. */
|
|
577
|
+
view: DestinationView;
|
|
578
|
+
/**
|
|
579
|
+
* Raw view parameters, up to four numbers whose meaning depends on `view` —
|
|
580
|
+
* for example `fitR` carries [left, bottom, right, top] and `fitH` a single
|
|
581
|
+
* top coordinate. Empty for `fit` and `fitB`, which take none.
|
|
582
|
+
*/
|
|
583
|
+
viewParams: number[];
|
|
584
|
+
/** Destination X coordinate. Only set for `xyz` destinations that specify one. */
|
|
585
|
+
destX?: number;
|
|
586
|
+
/** Destination Y coordinate. Only set for `xyz` destinations that specify one. */
|
|
587
|
+
destY?: number;
|
|
588
|
+
/** Destination zoom level. Only set for `xyz` destinations that specify one. */
|
|
589
|
+
destZoom?: number;
|
|
590
|
+
}
|
|
591
|
+
|
|
416
592
|
// native addon bindings (internal)
|
|
417
593
|
export interface NativePage {
|
|
418
594
|
readonly number: number;
|
|
@@ -425,6 +601,7 @@ export interface NativePage {
|
|
|
425
601
|
readonly cropBox?: PageObjectBounds;
|
|
426
602
|
readonly trimBox?: PageObjectBounds;
|
|
427
603
|
getText(): Promise<string>;
|
|
604
|
+
getCharacters(start?: number, count?: number): Promise<TextCharacter[]>;
|
|
428
605
|
render(options?: PageRenderOptions): Promise<Buffer | void>;
|
|
429
606
|
getObject(index: number): Promise<PageObject>;
|
|
430
607
|
renderImage(index: number, options?: ImageRenderOptions): Promise<Buffer | void>;
|
|
@@ -433,6 +610,7 @@ export interface NativePage {
|
|
|
433
610
|
getAnnotations(): Promise<Annotation[]>;
|
|
434
611
|
getAnnotationAttachment(index: number, outputPath?: string): Promise<Buffer | undefined>;
|
|
435
612
|
getFormFields(): Promise<FormField[]>;
|
|
613
|
+
getStructTree(): Promise<StructElement[]>;
|
|
436
614
|
close(): void;
|
|
437
615
|
}
|
|
438
616
|
|
|
@@ -443,6 +621,10 @@ export interface NativeDocument {
|
|
|
443
621
|
getBookmarks(): Promise<Bookmark[]>;
|
|
444
622
|
getAttachments(): Promise<Attachment[]>;
|
|
445
623
|
getAttachment(index: number, outputPath?: string): Promise<Buffer | undefined>;
|
|
624
|
+
getSignatures(): Promise<Signature[]>;
|
|
625
|
+
getSignatureContents(index: number, outputPath?: string): Promise<Buffer | undefined>;
|
|
626
|
+
getJavaScriptActions(): Promise<JavaScriptAction[]>;
|
|
627
|
+
getNamedDestinations(): Promise<NamedDestination[]>;
|
|
446
628
|
destroy(): void;
|
|
447
629
|
}
|
|
448
630
|
|
|
@@ -465,6 +647,77 @@ export interface MergeDocumentsOptions {
|
|
|
465
647
|
output?: string;
|
|
466
648
|
}
|
|
467
649
|
|
|
650
|
+
export interface AssemblePagesOptions {
|
|
651
|
+
/** Write to this file path instead of returning a Buffer. */
|
|
652
|
+
output?: string;
|
|
653
|
+
/** Password for the source PDF (if encrypted). */
|
|
654
|
+
password?: string;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* A file to embed with {@link addAttachments}.
|
|
659
|
+
*
|
|
660
|
+
* There is deliberately no `mimeType` or `description` field. The MIME type is
|
|
661
|
+
* `/Subtype` on the embedded-file stream and the description is `/Desc` on the
|
|
662
|
+
* filespec dictionary; PDFium's write API reaches neither in the bundled build,
|
|
663
|
+
* and accepting those values only to drop them silently would be worse than not
|
|
664
|
+
* offering them. Note this is an asymmetry with reading: `Attachment.mimeType`
|
|
665
|
+
* is reported when a PDF already carries it.
|
|
666
|
+
*/
|
|
667
|
+
export interface AttachmentInput {
|
|
668
|
+
/**
|
|
669
|
+
* File name to store in the /EmbeddedFiles name tree, e.g. 'report.xml'.
|
|
670
|
+
* Must be non-empty and not already present in the document.
|
|
671
|
+
*/
|
|
672
|
+
name: string;
|
|
673
|
+
/** File contents. */
|
|
674
|
+
data: Buffer;
|
|
675
|
+
/**
|
|
676
|
+
* Creation date as a PDF date string, e.g. 'D:20250101120000Z'. Defaults to
|
|
677
|
+
* the current time, so set it explicitly for reproducible output.
|
|
678
|
+
*/
|
|
679
|
+
creationDate?: string;
|
|
680
|
+
/** Modification date as a PDF date string. */
|
|
681
|
+
modDate?: string;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
export interface AddAttachmentsOptions {
|
|
685
|
+
/** Write to this file path instead of returning a Buffer. */
|
|
686
|
+
output?: string;
|
|
687
|
+
/** Password for the source PDF (if encrypted). */
|
|
688
|
+
password?: string;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
export interface FlattenDocumentOptions {
|
|
692
|
+
/**
|
|
693
|
+
* Which appearance to bake in: `'display'` (default) or `'print'`. They
|
|
694
|
+
* differ for annotations whose appearance dictionary distinguishes the two —
|
|
695
|
+
* a watermark marked print-only, for instance.
|
|
696
|
+
*/
|
|
697
|
+
usage?: 'display' | 'print';
|
|
698
|
+
/** Only flatten these page indices. Defaults to every page. */
|
|
699
|
+
pages?: number[];
|
|
700
|
+
/** Write to this file path instead of returning a Buffer. */
|
|
701
|
+
output?: string;
|
|
702
|
+
/** Password for the source PDF (if encrypted). */
|
|
703
|
+
password?: string;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
export interface NUpPagesOptions {
|
|
707
|
+
/** Number of source pages placed side by side across each output page. */
|
|
708
|
+
columns: number;
|
|
709
|
+
/** Number of source pages stacked down each output page. */
|
|
710
|
+
rows: number;
|
|
711
|
+
/** Output page width in points. Defaults to the width of the first source page. */
|
|
712
|
+
width?: number;
|
|
713
|
+
/** Output page height in points. Defaults to the height of the first source page. */
|
|
714
|
+
height?: number;
|
|
715
|
+
/** Write to this file path instead of returning a Buffer. */
|
|
716
|
+
output?: string;
|
|
717
|
+
/** Password for the source PDF (if encrypted). */
|
|
718
|
+
password?: string;
|
|
719
|
+
}
|
|
720
|
+
|
|
468
721
|
export interface NativeAddon {
|
|
469
722
|
loadDocument(input: Buffer | string, password?: string): Promise<NativeDocument>;
|
|
470
723
|
splitDocument(
|
|
@@ -476,4 +729,19 @@ export interface NativeAddon {
|
|
|
476
729
|
inputs: Array<Buffer | string | { input: Buffer | string; password?: string }>,
|
|
477
730
|
options?: { output?: string },
|
|
478
731
|
): Promise<Buffer | undefined>;
|
|
732
|
+
assemblePages(
|
|
733
|
+
input: Buffer | string,
|
|
734
|
+
pages: number[],
|
|
735
|
+
options?: AssemblePagesOptions,
|
|
736
|
+
): Promise<Buffer | undefined>;
|
|
737
|
+
nUpPages(input: Buffer | string, options: NUpPagesOptions): Promise<Buffer | undefined>;
|
|
738
|
+
addAttachments(
|
|
739
|
+
input: Buffer | string,
|
|
740
|
+
attachments: AttachmentInput[],
|
|
741
|
+
options?: AddAttachmentsOptions,
|
|
742
|
+
): Promise<Buffer | undefined>;
|
|
743
|
+
flattenDocument(
|
|
744
|
+
input: Buffer | string,
|
|
745
|
+
options?: FlattenDocumentOptions,
|
|
746
|
+
): Promise<Buffer | undefined>;
|
|
479
747
|
}
|