@swc/anyformat-pdf 0.2.0 → 0.2.2

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/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  // The public type definitions of @swc/anyformat-pdf. See index.js.
2
2
 
3
+ /// <reference types="node" />
4
+
3
5
  import type { ReadableStream } from 'node:stream/web'
4
6
 
5
7
  export { detectVersionSync, isPdfSync } from './binding.js'
@@ -70,3 +72,447 @@ export interface IsPdfStreamResult {
70
72
  export declare function isPdfStream(
71
73
  stream: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array>,
72
74
  ): Promise<IsPdfStreamResult>
75
+
76
+ /**
77
+ * A Buffer that this package returns. It is backed by an ArrayBuffer, never a
78
+ * SharedArrayBuffer.
79
+ */
80
+ type OwnedBuffer = ReturnType<typeof Buffer.alloc>
81
+
82
+ /** The `code` of an error of the page functions. */
83
+ export type PdfErrorCode =
84
+ | 'ERR_PDF_PARSE'
85
+ | 'ERR_PDF_PASSWORD'
86
+ | 'ERR_PDF_UNSUPPORTED'
87
+ | 'ERR_PDF_PAGE_INDEX'
88
+ | 'ERR_INVALID_ARG_VALUE'
89
+ | 'ERR_INVALID_ARG_TYPE'
90
+ | 'ERR_INVALID_URL_SCHEME'
91
+
92
+ /**
93
+ * An error of this package. The functions do not export a class: check
94
+ * `code` to find the kind of error.
95
+ */
96
+ export interface PdfError extends Error {
97
+ code: PdfErrorCode
98
+ /**
99
+ * The index of the input in `mergePdfs` that caused the error, also for
100
+ * ERR_INVALID_URL_SCHEME and argument errors of one input.
101
+ */
102
+ input?: number
103
+ /**
104
+ * ERR_PDF_PAGE_INDEX: the page index as the caller gave it, not
105
+ * normalized (`selectPages(data, -20)` on 15 pages gives -20). From
106
+ * `parsePageRanges`, the index that the text gives (`"r20"` gives -20).
107
+ */
108
+ page?: number
109
+ /** ERR_PDF_PAGE_INDEX: the number of pages of the document. */
110
+ pageCount?: number
111
+ }
112
+
113
+ /**
114
+ * The error for a file path that cannot be read. `code` is the errno name,
115
+ * for example `ENOENT`, or `ERR_IO` for an error that has no errno name.
116
+ */
117
+ export interface PdfFileError extends Error {
118
+ code: string
119
+ /** The path of the file. */
120
+ path: string
121
+ /** The index of the input in `mergePdfs` whose file cannot be read. */
122
+ input?: number
123
+ }
124
+
125
+ /**
126
+ * A PDF file: its bytes, or the path of the file. A string is a path, not
127
+ * the content of a file or a URL. A string that starts with `%PDF`, `JVBER`
128
+ * (base64 of `%PDF`), `data:`, `http://`, or `https://` fails with
129
+ * ERR_INVALID_ARG_VALUE and a hint: `Buffer.from(text, 'base64')` for base64,
130
+ * `Buffer.from(text.slice(text.indexOf(',') + 1), 'base64')` for a data URI,
131
+ * and `new Uint8Array(await (await fetch(url)).arrayBuffer())` for a URL. To
132
+ * pass a path that starts with one of these prefixes, write `./` in front of
133
+ * it or pass `pathToFileURL(path)`. A `URL` must use the `file:` scheme;
134
+ * another scheme fails with ERR_INVALID_URL_SCHEME, as in `fs.readFile`.
135
+ */
136
+ export type PdfSource = Uint8Array | ArrayBuffer | string | URL
137
+
138
+ /**
139
+ * A range of 0-based page indices with the rules of a Python slice: `start`
140
+ * is included and `end` is excluded. A negative value counts from the end:
141
+ * -1 is the last page. `null` and `undefined` mean the default. `step` must
142
+ * not be 0. Defaults: `step` is 1. With a positive step, `start` is 0 and
143
+ * `end` is the page count. With a negative step, `start` is the last page and
144
+ * `end` is before the first page, so `{ step: -1 }` selects all pages in
145
+ * reverse order. Values outside the document are clamped, as in Python.
146
+ */
147
+ export interface PageRange {
148
+ /**
149
+ * An integer, `Infinity`, or `-Infinity` (clamped). NaN or a finite
150
+ * non-integer fails with ERR_INVALID_ARG_VALUE.
151
+ */
152
+ start?: number | null | undefined
153
+ /** As `start`. */
154
+ end?: number | null | undefined
155
+ /**
156
+ * A non-zero finite integer. 0, NaN, a non-integer, or an infinite step
157
+ * fails with ERR_INVALID_ARG_VALUE.
158
+ */
159
+ step?: number | null | undefined
160
+ }
161
+
162
+ /**
163
+ * Selects pages in a given order. A number is a 0-based page index, an
164
+ * integer; a negative index counts from the end. NaN, a non-integer, or an
165
+ * infinite index fails with ERR_INVALID_ARG_VALUE. An array concatenates its items. A
166
+ * page can occur more than once. `{}` selects all pages.
167
+ */
168
+ export type PageSelection = number | PageRange | readonly (number | PageRange)[]
169
+
170
+ export interface ParsePageRangesOptions {
171
+ /**
172
+ * The page count of the document, an integer from 0 to 2^20. Another
173
+ * value fails with ERR_INVALID_ARG_VALUE. With it, the function also
174
+ * accepts the items that need the page count, and it fails with
175
+ * ERR_PDF_PAGE_INDEX for a page number larger than the page count.
176
+ * Default: null.
177
+ */
178
+ pageCount?: number | null | undefined
179
+ }
180
+
181
+ /**
182
+ * Parses a page selection that a person typed, with 1-based page numbers, for
183
+ * example `"1-3, 5, 8-"`, to a 0-based selection:
184
+ * `[{ start: 0, end: 3 }, 4, { start: 7 }]`. The grammar is in "Page
185
+ * selection text" of the API documentation. It reads the page-range text of print dialogs, PDFium,
186
+ * pdf-merger-js, and qpdf; qpdf text has its qpdf meaning. Throws
187
+ * ERR_INVALID_ARG_VALUE for a text that is not valid, and for a text that
188
+ * needs `pageCount` when `pageCount` is not given.
189
+ */
190
+ export declare function parsePageRanges(text: string, options: ParsePageRangesOptions & { pageCount: number }): number[]
191
+ export declare function parsePageRanges(text: string, options?: ParsePageRangesOptions | null): (number | PageRange)[]
192
+
193
+ /**
194
+ * A password to open an encrypted PDF. See "Encryption" in the API
195
+ * documentation for the encoding.
196
+ */
197
+ export type PdfPassword = string | Uint8Array
198
+
199
+ /** The pages of one input of `mergePdfs`. */
200
+ export interface PdfPart {
201
+ /** The PDF file. */
202
+ data: PdfSource
203
+ /** The pages to copy, in order. Default: all pages. */
204
+ pages?: PageSelection | null | undefined
205
+ /** An angle in degrees, clockwise, a multiple of 90. Default: 0. */
206
+ rotate?: number | null | undefined
207
+ /**
208
+ * 'add' turns each copied page by `rotate` in addition to its current
209
+ * rotation. 'set' sets the rotation of each copied page to `rotate`.
210
+ * Default: 'add'.
211
+ */
212
+ rotateMode?: 'add' | 'set' | null | undefined
213
+ /**
214
+ * The password of `data`. It applies to every part with the same source,
215
+ * so give it once. Default: the password that another part with the same
216
+ * source gives, or the empty password when no part gives one. `null` and
217
+ * `undefined` do not conflict with a password; two parts of one source with
218
+ * two different passwords fail with ERR_INVALID_ARG_VALUE.
219
+ */
220
+ password?: PdfPassword | null | undefined
221
+ /**
222
+ * Add an outline item (bookmark) with this title that points at the first
223
+ * copied page of this part. The outline items of `data` that are copied
224
+ * with this part become its children. Default: null.
225
+ */
226
+ outline?: string | null | undefined
227
+ /**
228
+ * Copy the outline items of `data` with this part. Default: the `outlines`
229
+ * option of `mergePdfs`; a value set here overrides the option. For the
230
+ * base document, only the value of `inputs[base]` counts: false removes
231
+ * the base document's outline; on the other parts of the base document it
232
+ * has no effect.
233
+ */
234
+ outlines?: boolean | null | undefined
235
+ /**
236
+ * true: this part is a separate copy of its source. Its outline items,
237
+ * links, named destinations, and form fields refer to its own pages, as if
238
+ * it came from another file. The file is still read and parsed once, and
239
+ * the content streams and resources are still shared.
240
+ * false: parts with the same source share one copy of the outline and
241
+ * the named destinations, and links point at the first occurrence of a
242
+ * page; the form widgets of later occurrences follow `formFields` (see
243
+ * "Repeated sources" in the API documentation). Default: false.
244
+ */
245
+ separate?: boolean | null | undefined
246
+ }
247
+
248
+ /** An input of `mergePdfs`: all pages of a file, or the pages of a part. */
249
+ export type PdfInput = PdfSource | PdfPart
250
+
251
+ /** The options of each function that writes a PDF file. */
252
+ export interface PdfWriteOptions {
253
+ /**
254
+ * Write the objects in compressed object streams and the cross-reference
255
+ * table as a stream (PDF 1.5). false writes a classic cross-reference
256
+ * table. Default: with a base document, as the base document does (so a
257
+ * PDF/A-1 file stays without object streams); without a base document,
258
+ * true; false when `version` is lower than "1.5".
259
+ */
260
+ objectStreams?: boolean | null | undefined
261
+ /**
262
+ * The version in the header of the output, for example "1.7". Default: the
263
+ * highest version of the inputs, and at least "1.5" when the output has
264
+ * object streams. A version lower than "1.5" together with
265
+ * `objectStreams: true` fails with ERR_INVALID_ARG_VALUE.
266
+ */
267
+ version?: string | null | undefined
268
+ }
269
+
270
+ /** The options of each function that copies pages to a new page list. */
271
+ export interface PdfCopyOptions extends PdfWriteOptions {
272
+ /**
273
+ * The default of `PdfPart.outlines`: copy the outline items that point at
274
+ * copied pages. A part that sets its own `outlines` overrides it, also
275
+ * `inputs[base]`. false, with no part that sets true: the output has no
276
+ * outline items from the inputs, also with a base document; only the
277
+ * `outline` items of the parts. Default: true.
278
+ */
279
+ outlines?: boolean | null | undefined
280
+ /**
281
+ * true: each page keeps the label that it had in a labeled input. false:
282
+ * the output has no page labels, also with a base document. Default: true.
283
+ */
284
+ pageLabels?: boolean | null | undefined
285
+ /**
286
+ * What to do when a copied form field has the same full name as a field
287
+ * from another input, or from another copy of the same page. 'rename' adds
288
+ * `_2`, `_3`, and so on to the name of the later top-level field, so that
289
+ * the fields keep separate values. 'join' makes the fields one field with
290
+ * the value of the first field. Default: 'rename'.
291
+ */
292
+ formFields?: 'rename' | 'join' | null | undefined
293
+ }
294
+
295
+ export interface MergePdfsOptions extends PdfCopyOptions {
296
+ /**
297
+ * The index in `inputs` of the base document. The output keeps all of the
298
+ * document-level data of the base document, as `selectPages` does, and
299
+ * adds the pages of the other inputs to it. `inputs[base]` is always part of
300
+ * the base document. When it is not `separate`, every other part with the
301
+ * same source that is not `separate` is part of it too; when it is
302
+ * `separate`, no other part is. The base document brings all of its
303
+ * document-level data, also its embedded files, JavaScript, open action,
304
+ * and structure tree; no option copies only its Info and XMP. A negative index counts from the end, as in Python.
305
+ * An index that is not in `inputs` fails with ERR_INVALID_ARG_VALUE. null
306
+ * makes a new document that has only the data that belongs to the copied
307
+ * pages. Default: null.
308
+ */
309
+ base?: number | null | undefined
310
+ }
311
+
312
+ /**
313
+ * Copies the pages of the inputs, in order, to a new PDF file. The function
314
+ * copies the input buffers before it starts, so the caller can change them
315
+ * after the call.
316
+ */
317
+ export declare function mergePdfs(inputs: readonly PdfInput[], options?: MergePdfsOptions | null): Promise<OwnedBuffer>
318
+ /** The synchronous form of `mergePdfs`. */
319
+ export declare function mergePdfsSync(inputs: readonly PdfInput[], options?: MergePdfsOptions | null): OwnedBuffer
320
+
321
+ export interface SelectPagesOptions extends PdfCopyOptions {
322
+ /** The password of `data`. Default: the empty password. */
323
+ password?: PdfPassword | null | undefined
324
+ }
325
+
326
+ /**
327
+ * Makes a copy of the document with only the selected pages, in the order of
328
+ * the selection. Use it to extract, reorder, reverse, or duplicate pages. The
329
+ * output keeps all document-level data. `selectPages(data, pages)` is the
330
+ * same as `mergePdfs([{ data, pages }], { base: 0 })`.
331
+ */
332
+ export declare function selectPages(data: PdfSource, pages: PageSelection, options?: SelectPagesOptions | null): Promise<OwnedBuffer>
333
+ /** The synchronous form of `selectPages`. */
334
+ export declare function selectPagesSync(data: PdfSource, pages: PageSelection, options?: SelectPagesOptions | null): OwnedBuffer
335
+
336
+ export interface RemovePagesOptions extends PdfWriteOptions {
337
+ /** The password of `data`. Default: the empty password. */
338
+ password?: PdfPassword | null | undefined
339
+ }
340
+
341
+ /**
342
+ * Removes the selected pages. The other pages keep their order and their
343
+ * page labels. The output keeps all document-level data.
344
+ */
345
+ export declare function removePages(data: PdfSource, pages: PageSelection, options?: RemovePagesOptions | null): Promise<OwnedBuffer>
346
+ /** The synchronous form of `removePages`. */
347
+ export declare function removePagesSync(data: PdfSource, pages: PageSelection, options?: RemovePagesOptions | null): OwnedBuffer
348
+
349
+ export interface SplitPdfOptions extends PdfCopyOptions {
350
+ /**
351
+ * One output file for each selection. Each selection must select one page
352
+ * or more. Default: one file for each page.
353
+ */
354
+ ranges?: readonly PageSelection[] | null | undefined
355
+ /**
356
+ * One output file for each `every` pages, in order. The last file can have
357
+ * fewer pages. Do not set it together with `ranges`.
358
+ */
359
+ every?: number | null | undefined
360
+ /** The password of `data`. Default: the empty password. */
361
+ password?: PdfPassword | null | undefined
362
+ /**
363
+ * true: each output is `selectPages` of the input, with all of its
364
+ * document-level data, embedded files too, so each output repeats them.
365
+ * false: each output is a new document with only the data that belongs to
366
+ * its pages, as `mergePdfs` without `base` makes. Default: false.
367
+ */
368
+ documentData?: boolean | null | undefined
369
+ }
370
+
371
+ /** Splits a PDF file into new PDF files. */
372
+ export declare function splitPdf(data: PdfSource, options?: SplitPdfOptions | null): Promise<OwnedBuffer[]>
373
+ /** The synchronous form of `splitPdf`. */
374
+ export declare function splitPdfSync(data: PdfSource, options?: SplitPdfOptions | null): OwnedBuffer[]
375
+
376
+ /**
377
+ * The angles of `rotatePages`: one angle for the pages of `options.pages`; a
378
+ * map from a page index to the angle of that page; or an array whose item `i`
379
+ * is the angle of page `i` (`null`, `undefined`, and holes leave a page
380
+ * unchanged). A key of a `Record` that is not an integer fails with
381
+ * ERR_INVALID_ARG_VALUE; for a pdf-lib `degrees()` object the message says
382
+ * to pass its `angle`. An angle that is not a number fails with
383
+ * ERR_INVALID_ARG_TYPE.
384
+ */
385
+ export type PageAngles =
386
+ | number
387
+ | readonly (number | null | undefined)[]
388
+ | Readonly<Record<number, number>>
389
+ | ReadonlyMap<number, number>
390
+
391
+ export interface RotatePagesOptions extends PdfWriteOptions {
392
+ /**
393
+ * The pages to rotate when `angles` is a number. A page that the selection
394
+ * gives twice is rotated once. Default: all pages. Must not be set when
395
+ * `angles` is a map or an array.
396
+ */
397
+ pages?: PageSelection | null | undefined
398
+ /**
399
+ * 'add' turns each page by its angle in addition to its current rotation.
400
+ * 'set' sets the rotation of each page to its angle. Default: 'add'.
401
+ */
402
+ mode?: 'add' | 'set' | null | undefined
403
+ /** The password of `data`. Default: the empty password. */
404
+ password?: PdfPassword | null | undefined
405
+ }
406
+
407
+ /**
408
+ * Rotates pages clockwise. Each angle is in degrees and a multiple of 90.
409
+ * Changes only the rotation attribute of the pages, not their content. The
410
+ * output keeps all document-level data.
411
+ */
412
+ export declare function rotatePages(data: PdfSource, angles: number, options?: RotatePagesOptions | null): Promise<OwnedBuffer>
413
+ export declare function rotatePages(
414
+ data: PdfSource,
415
+ angles: Exclude<PageAngles, number>,
416
+ options?: (RotatePagesOptions & { pages?: null | undefined }) | null,
417
+ ): Promise<OwnedBuffer>
418
+ /** For a value of the union type `PageAngles`; `pages` must not be set. */
419
+ export declare function rotatePages(
420
+ data: PdfSource,
421
+ angles: PageAngles,
422
+ options?: (RotatePagesOptions & { pages?: null | undefined }) | null,
423
+ ): Promise<OwnedBuffer>
424
+ /** The synchronous form of `rotatePages`. */
425
+ export declare function rotatePagesSync(data: PdfSource, angles: number, options?: RotatePagesOptions | null): OwnedBuffer
426
+ export declare function rotatePagesSync(
427
+ data: PdfSource,
428
+ angles: Exclude<PageAngles, number>,
429
+ options?: (RotatePagesOptions & { pages?: null | undefined }) | null,
430
+ ): OwnedBuffer
431
+ export declare function rotatePagesSync(
432
+ data: PdfSource,
433
+ angles: PageAngles,
434
+ options?: (RotatePagesOptions & { pages?: null | undefined }) | null,
435
+ ): OwnedBuffer
436
+
437
+ /** A rectangle in PDF user space. `x` and `y` are the lower-left corner. */
438
+ export interface PdfRect {
439
+ x: number
440
+ y: number
441
+ width: number
442
+ height: number
443
+ }
444
+
445
+ /**
446
+ * The boxes and the rotation of a page. The boxes are in user space units,
447
+ * before rotation, with inherited values resolved. See "Page boxes" in the
448
+ * API documentation for missing and invalid boxes.
449
+ */
450
+ export interface PdfPageInfo {
451
+ /**
452
+ * The width of the page as a viewer shows it, in points (1/72 inch): the
453
+ * width of `cropBox` times `userUnit`, or its height times `userUnit` when
454
+ * `rotation` is 90 or 270. It is not the media box width.
455
+ */
456
+ width: number
457
+ /** The height of the page as a viewer shows it, in points. See `width`. */
458
+ height: number
459
+ /** The clockwise rotation of the page: 0, 90, 180, or 270. */
460
+ rotation: 0 | 90 | 180 | 270
461
+ /** The media box. */
462
+ mediaBox: PdfRect
463
+ /** The crop box, intersected with the media box. Default: the media box. */
464
+ cropBox: PdfRect
465
+ /** The bleed box, intersected with the media box. Default: the crop box. */
466
+ bleedBox: PdfRect
467
+ /** The trim box, intersected with the media box. Default: the crop box. */
468
+ trimBox: PdfRect
469
+ /** The art box, intersected with the media box. Default: the crop box. */
470
+ artBox: PdfRect
471
+ /** The size of a user space unit in points. Default: 1. */
472
+ userUnit: number
473
+ /**
474
+ * The boxes that the page, or a `/Pages` node above it, defines with a
475
+ * valid value, in this order: 'media', 'crop', 'bleed', 'trim', 'art'. A
476
+ * box that is not in the list has its default value. Use it to find out,
477
+ * for example, whether a page has a trim box (PDF/X).
478
+ */
479
+ definedBoxes: readonly ('media' | 'crop' | 'bleed' | 'trim' | 'art')[]
480
+ }
481
+
482
+ /** The result of `readPdfInfo`. */
483
+ export interface PdfInfo {
484
+ /** The version of the PDF header, for example "1.7". */
485
+ version: string
486
+ /** The number of pages: `pages.length`. */
487
+ pageCount: number
488
+ /**
489
+ * true when the file has an encryption dictionary, also when the empty
490
+ * password opens it (a file with only an owner password).
491
+ */
492
+ encrypted: boolean
493
+ /**
494
+ * true when the parser had to reconstruct the cross-reference table,
495
+ * because the file is damaged.
496
+ */
497
+ repaired: boolean
498
+ /**
499
+ * One item for each page, in page order, from a walk of the page tree.
500
+ * `pages.length` is the page count that every page function uses; the
501
+ * `/Count` entries of the file are not used.
502
+ */
503
+ pages: PdfPageInfo[]
504
+ }
505
+
506
+ export interface ReadPdfInfoOptions {
507
+ /** The password of an encrypted file. Default: the empty password. */
508
+ password?: PdfPassword | null | undefined
509
+ }
510
+
511
+ /**
512
+ * Reads the version, the page count, and the boxes and rotations of the
513
+ * pages. Rejects with ERR_PDF_PASSWORD when the password, or the empty
514
+ * password, does not open the file.
515
+ */
516
+ export declare function readPdfInfo(data: PdfSource, options?: ReadPdfInfoOptions | null): Promise<PdfInfo>
517
+ /** The synchronous form of `readPdfInfo`. */
518
+ export declare function readPdfInfoSync(data: PdfSource, options?: ReadPdfInfoOptions | null): PdfInfo