pdfium-native 0.8.0 → 0.9.1

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/lib/types.ts CHANGED
@@ -140,6 +140,114 @@ 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.
151
+ *
152
+ * Empty string when the glyph has no Unicode mapping — check
153
+ * `hasUnicodeMapError` before trusting it — and also on the *second* entry of
154
+ * a non-BMP character. PDFium reports one UTF-16 code unit per index, so an
155
+ * emoji occupies two entries; the whole character is emitted on the first and
156
+ * the second is an empty continuation. That keeps indices aligned with
157
+ * `getText()` while `chars.map((c) => c.char).join('')` still reproduces it
158
+ * exactly.
159
+ */
160
+ char: string;
161
+ /**
162
+ * The raw UTF-16 code unit at this index, or 0 if the glyph has no mapping.
163
+ * For a non-BMP character this is the high surrogate on the first entry and
164
+ * the low surrogate on the second, not the combined code point.
165
+ */
166
+ unicode: number;
167
+ /**
168
+ * Tight bounding box in page points, hugging the glyph's ink. Absent if
169
+ * PDFium cannot determine it.
170
+ */
171
+ bounds?: PageObjectBounds;
172
+ /** X coordinate of the baseline origin in page points. Absent if unavailable. */
173
+ x?: number;
174
+ /** Y coordinate of the baseline origin in page points. Absent if unavailable. */
175
+ y?: number;
176
+ /** Font size in points (the typographic "em" size). */
177
+ fontSize: number;
178
+ /** PostScript name of the font (e.g. 'Helvetica-Bold'). Empty string if unavailable. */
179
+ fontName: string;
180
+ /** Raw PDF font descriptor flags bitmask (see PDF spec Table 123). */
181
+ fontFlags: number;
182
+ /**
183
+ * Font weight (e.g. 400 = normal, 700 = bold). Absent when the font declares
184
+ * none — which is the case for the non-embedded standard fonts, so do not
185
+ * treat its absence as an error.
186
+ */
187
+ fontWeight?: number;
188
+ /**
189
+ * Rotation of the character in radians, normalized to `[0, 2π)`. 0 for
190
+ * upright text.
191
+ *
192
+ * Note the direction: PDFium derives this as `atan2(c, a)` from the text
193
+ * matrix, so it runs *clockwise*. Text rotated 45° counterclockwise on the
194
+ * page reports ≈ 5.4978 (2π − π/4), not ≈ 0.7854.
195
+ */
196
+ angle: number;
197
+ /**
198
+ * Whether PDFium synthesized this character rather than reading it from the
199
+ * content stream — line breaks between text runs, and spaces inferred from
200
+ * glyph spacing. Synthesized characters carry no real font, so `fontName` is
201
+ * empty, `fontSize` is 1 and `fontWeight` is absent. Useful when mapping text
202
+ * offsets back to the original PDF.
203
+ */
204
+ isGenerated: boolean;
205
+ /** Whether this character is a hyphen (relevant when de-hyphenating lines). */
206
+ isHyphen: boolean;
207
+ /** Whether the glyph has no reliable Unicode mapping, making `char` unreliable. */
208
+ hasUnicodeMapError: boolean;
209
+ }
210
+
211
+ /**
212
+ * A node in a tagged PDF's structure tree — the logical document outline
213
+ * (headings, paragraphs, tables, figures) behind the visual layout, which is
214
+ * what screen readers follow.
215
+ */
216
+ export interface StructElement {
217
+ /** Structure type (/S), e.g. 'H1', 'P', 'Table', 'Figure'. */
218
+ type: string;
219
+ /** Object type (/Type), normally 'StructElem'. Absent if not set. */
220
+ objType?: string;
221
+ /** Human-readable title (/T). Absent if not set. */
222
+ title?: string;
223
+ /** Alternate text (/Alt) — the accessibility description. Absent if not set. */
224
+ altText?: string;
225
+ /** Replacement text (/ActualText) for content whose glyphs differ from its meaning. */
226
+ actualText?: string;
227
+ /** Element identifier (/ID). Absent if not set. */
228
+ id?: string;
229
+ /** Language override (/Lang) for this subtree, e.g. 'de-DE'. Absent if not set. */
230
+ lang?: string;
231
+ /** Marked-content ID tying this element to page content. Absent if the element has none. */
232
+ markedContentId?: number;
233
+ /**
234
+ * Child elements. Absent when the element has no element children.
235
+ *
236
+ * The walk is capped at 100,000 elements per page. PDFium's structure tree is
237
+ * a DAG, so a document whose `/K` arrays repeat the same child expands
238
+ * exponentially when flattened into a tree; the cap bounds that. Real
239
+ * documents do not come close to it.
240
+ */
241
+ children?: StructElement[];
242
+ }
243
+
244
+ export interface GetCharactersOptions {
245
+ /** First character index to return (default: 0). */
246
+ start?: number;
247
+ /** How many characters to return (default: all remaining). */
248
+ count?: number;
249
+ }
250
+
143
251
  export interface SearchOptions {
144
252
  /** Match case when searching (default: false). */
145
253
  caseSensitive?: boolean;
@@ -413,6 +521,96 @@ export interface GetAttachmentOptions {
413
521
  output?: string;
414
522
  }
415
523
 
524
+ /**
525
+ * A digital signature as declared by the PDF.
526
+ *
527
+ * These values come straight from the signature dictionary — nothing here is
528
+ * cryptographically verified. A present, well-formed signature says only that
529
+ * the document *claims* to be signed.
530
+ */
531
+ export interface Signature {
532
+ /** 0-based index of the signature in the document's AcroForm field list. */
533
+ index: number;
534
+ /**
535
+ * Encoding of the signature value (/SubFilter), e.g. 'adbe.pkcs7.detached'
536
+ * or 'ETSI.CAdES.detached'. Empty string if the PDF omits it.
537
+ */
538
+ subFilter: string;
539
+ /** Reason given for signing (/Reason). Absent if not set. */
540
+ reason?: string;
541
+ /**
542
+ * Time of signing (/M) as a PDF date string (e.g. "D:20250101120000+01'00'").
543
+ * Absent if not set. For PKCS#7 signatures the authoritative time is usually
544
+ * inside the signature blob instead; use this only as a fallback.
545
+ */
546
+ time?: string;
547
+ /**
548
+ * Certification level for a DocMDP signature: 1 = no changes allowed,
549
+ * 2 = form fill-in and signing allowed, 3 = additionally annotations allowed.
550
+ * Absent for an ordinary (non-certifying) signature.
551
+ */
552
+ docMdpPermission?: 1 | 2 | 3;
553
+ /**
554
+ * The /ByteRange array covered by the signature digest: a flat list of
555
+ * (offset, length) pairs, exactly as stored in the PDF. A signature covers
556
+ * the whole file when the last pair ends at the file size — anything less
557
+ * means content was appended after signing.
558
+ */
559
+ byteRange: number[];
560
+ /**
561
+ * Size in bytes of the signature value (/Contents). Read the bytes
562
+ * themselves with {@link PDFiumDocument.getSignatureContents}.
563
+ */
564
+ contentsLength: number;
565
+ }
566
+
567
+ export interface GetSignatureContentsOptions {
568
+ /** Write the signature bytes to this file path instead of returning a Buffer. */
569
+ output?: string;
570
+ }
571
+
572
+ /**
573
+ * A document-level JavaScript action — a script a viewer runs when the
574
+ * document opens.
575
+ */
576
+ export interface JavaScriptAction {
577
+ /** 0-based index in the document's /Names /JavaScript tree. */
578
+ index: number;
579
+ /** Entry name in the JavaScript name tree. Empty string if unnamed. */
580
+ name: string;
581
+ /** The script source, returned as inert text. Empty string if unreadable. */
582
+ script: string;
583
+ }
584
+
585
+ /** How a viewer should frame the target page when following a destination. */
586
+ export type DestinationView =
587
+ 'xyz' | 'fit' | 'fitH' | 'fitV' | 'fitR' | 'fitB' | 'fitBH' | 'fitBV' | 'unknown';
588
+
589
+ /**
590
+ * A named destination — an anchor that GoTo actions and external links target
591
+ * by name instead of by page number.
592
+ */
593
+ export interface NamedDestination {
594
+ /** The destination's name, e.g. 'Chapter2'. */
595
+ name: string;
596
+ /** Target page index. Absent if the destination cannot be resolved to a page. */
597
+ pageIndex?: number;
598
+ /** Fit type declared by the destination. */
599
+ view: DestinationView;
600
+ /**
601
+ * Raw view parameters, up to four numbers whose meaning depends on `view` —
602
+ * for example `fitR` carries [left, bottom, right, top] and `fitH` a single
603
+ * top coordinate. Empty for `fit` and `fitB`, which take none.
604
+ */
605
+ viewParams: number[];
606
+ /** Destination X coordinate. Only set for `xyz` destinations that specify one. */
607
+ destX?: number;
608
+ /** Destination Y coordinate. Only set for `xyz` destinations that specify one. */
609
+ destY?: number;
610
+ /** Destination zoom level. Only set for `xyz` destinations that specify one. */
611
+ destZoom?: number;
612
+ }
613
+
416
614
  // native addon bindings (internal)
417
615
  export interface NativePage {
418
616
  readonly number: number;
@@ -425,6 +623,7 @@ export interface NativePage {
425
623
  readonly cropBox?: PageObjectBounds;
426
624
  readonly trimBox?: PageObjectBounds;
427
625
  getText(): Promise<string>;
626
+ getCharacters(start?: number, count?: number): Promise<TextCharacter[]>;
428
627
  render(options?: PageRenderOptions): Promise<Buffer | void>;
429
628
  getObject(index: number): Promise<PageObject>;
430
629
  renderImage(index: number, options?: ImageRenderOptions): Promise<Buffer | void>;
@@ -433,6 +632,7 @@ export interface NativePage {
433
632
  getAnnotations(): Promise<Annotation[]>;
434
633
  getAnnotationAttachment(index: number, outputPath?: string): Promise<Buffer | undefined>;
435
634
  getFormFields(): Promise<FormField[]>;
635
+ getStructTree(): Promise<StructElement[]>;
436
636
  close(): void;
437
637
  }
438
638
 
@@ -443,6 +643,10 @@ export interface NativeDocument {
443
643
  getBookmarks(): Promise<Bookmark[]>;
444
644
  getAttachments(): Promise<Attachment[]>;
445
645
  getAttachment(index: number, outputPath?: string): Promise<Buffer | undefined>;
646
+ getSignatures(): Promise<Signature[]>;
647
+ getSignatureContents(index: number, outputPath?: string): Promise<Buffer | undefined>;
648
+ getJavaScriptActions(): Promise<JavaScriptAction[]>;
649
+ getNamedDestinations(): Promise<NamedDestination[]>;
446
650
  destroy(): void;
447
651
  }
448
652
 
@@ -465,6 +669,83 @@ export interface MergeDocumentsOptions {
465
669
  output?: string;
466
670
  }
467
671
 
672
+ export interface AssemblePagesOptions {
673
+ /** Write to this file path instead of returning a Buffer. */
674
+ output?: string;
675
+ /** Password for the source PDF (if encrypted). */
676
+ password?: string;
677
+ }
678
+
679
+ /**
680
+ * A file to embed with {@link addAttachments}.
681
+ *
682
+ * There is deliberately no `mimeType` or `description` field. The MIME type is
683
+ * `/Subtype` on the embedded-file stream and the description is `/Desc` on the
684
+ * filespec dictionary; PDFium's write API reaches neither in the bundled build,
685
+ * and accepting those values only to drop them silently would be worse than not
686
+ * offering them. Note this is an asymmetry with reading: `Attachment.mimeType`
687
+ * is reported when a PDF already carries it.
688
+ */
689
+ export interface AttachmentInput {
690
+ /**
691
+ * File name to store in the /EmbeddedFiles name tree, e.g. 'report.xml'.
692
+ * Must be non-empty and not already present in the document.
693
+ */
694
+ name: string;
695
+ /** File contents. */
696
+ data: Buffer;
697
+ /**
698
+ * Creation date as a PDF date string, e.g. 'D:20250101120000Z'. Defaults to
699
+ * the current time, so set it explicitly for reproducible output.
700
+ */
701
+ creationDate?: string;
702
+ /** Modification date as a PDF date string. */
703
+ modDate?: string;
704
+ }
705
+
706
+ export interface AddAttachmentsOptions {
707
+ /** Write to this file path instead of returning a Buffer. */
708
+ output?: string;
709
+ /** Password for the source PDF (if encrypted). */
710
+ password?: string;
711
+ }
712
+
713
+ export interface FlattenDocumentOptions {
714
+ /**
715
+ * Which appearance to bake in: `'display'` (default) or `'print'`. They
716
+ * differ for annotations whose appearance dictionary distinguishes the two —
717
+ * a watermark marked print-only, for instance.
718
+ */
719
+ usage?: 'display' | 'print';
720
+ /**
721
+ * Only flatten these page indices. Defaults to every page when omitted.
722
+ *
723
+ * An explicitly empty array flattens *nothing* and returns an unchanged copy —
724
+ * so a selection computed by a filter that happens to match no pages is a
725
+ * no-op rather than a document stripped of every annotation.
726
+ */
727
+ pages?: number[];
728
+ /** Write to this file path instead of returning a Buffer. */
729
+ output?: string;
730
+ /** Password for the source PDF (if encrypted). */
731
+ password?: string;
732
+ }
733
+
734
+ export interface NUpPagesOptions {
735
+ /** Number of source pages placed side by side across each output page. */
736
+ columns: number;
737
+ /** Number of source pages stacked down each output page. */
738
+ rows: number;
739
+ /** Output page width in points. Defaults to the width of the first source page. */
740
+ width?: number;
741
+ /** Output page height in points. Defaults to the height of the first source page. */
742
+ height?: number;
743
+ /** Write to this file path instead of returning a Buffer. */
744
+ output?: string;
745
+ /** Password for the source PDF (if encrypted). */
746
+ password?: string;
747
+ }
748
+
468
749
  export interface NativeAddon {
469
750
  loadDocument(input: Buffer | string, password?: string): Promise<NativeDocument>;
470
751
  splitDocument(
@@ -476,4 +757,19 @@ export interface NativeAddon {
476
757
  inputs: Array<Buffer | string | { input: Buffer | string; password?: string }>,
477
758
  options?: { output?: string },
478
759
  ): Promise<Buffer | undefined>;
760
+ assemblePages(
761
+ input: Buffer | string,
762
+ pages: number[],
763
+ options?: AssemblePagesOptions,
764
+ ): Promise<Buffer | undefined>;
765
+ nUpPages(input: Buffer | string, options: NUpPagesOptions): Promise<Buffer | undefined>;
766
+ addAttachments(
767
+ input: Buffer | string,
768
+ attachments: AttachmentInput[],
769
+ options?: AddAttachmentsOptions,
770
+ ): Promise<Buffer | undefined>;
771
+ flattenDocument(
772
+ input: Buffer | string,
773
+ options?: FlattenDocumentOptions,
774
+ ): Promise<Buffer | undefined>;
479
775
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdfium-native",
3
- "version": "0.8.0",
3
+ "version": "0.9.1",
4
4
  "description": "Native Node.js bindings for PDFium",
5
5
  "license": "MIT",
6
6
  "type": "module",