@jasy/pdf 1.0.0-alpha.7 → 1.0.0-alpha.9

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.
Files changed (140) hide show
  1. package/README.md +11 -10
  2. package/dist/api/content.d.ts +5 -4
  3. package/dist/api/content.js +13 -7
  4. package/dist/api/descriptor.js +23 -0
  5. package/dist/api/dimension.d.ts +56 -0
  6. package/dist/api/dimension.js +27 -0
  7. package/dist/api/font-url.d.ts +28 -0
  8. package/dist/api/font-url.js +134 -0
  9. package/dist/api/forms.d.ts +347 -0
  10. package/dist/api/forms.js +189 -0
  11. package/dist/api/gradient.d.ts +53 -0
  12. package/dist/api/gradient.js +97 -0
  13. package/dist/api/index.d.ts +3 -0
  14. package/dist/api/index.js +3 -0
  15. package/dist/api/insets.d.ts +19 -12
  16. package/dist/api/insets.js +18 -7
  17. package/dist/api/layout.d.ts +20 -6
  18. package/dist/api/layout.js +31 -6
  19. package/dist/api/page-builder.d.ts +8 -0
  20. package/dist/api/page-builder.js +12 -0
  21. package/dist/api/structure.d.ts +11 -0
  22. package/dist/api/structure.js +17 -4
  23. package/dist/api/text.d.ts +42 -6
  24. package/dist/api/text.js +29 -5
  25. package/dist/crypto/legacy-handler.d.ts +43 -0
  26. package/dist/crypto/legacy-handler.js +145 -0
  27. package/dist/crypto/security-handler.d.ts +16 -0
  28. package/dist/crypto/security-handler.js +26 -0
  29. package/dist/edit/acroform-reader.d.ts +70 -0
  30. package/dist/edit/acroform-reader.js +126 -0
  31. package/dist/edit/appearance.d.ts +47 -0
  32. package/dist/edit/appearance.js +171 -0
  33. package/dist/edit/document.d.ts +145 -0
  34. package/dist/edit/document.js +642 -0
  35. package/dist/edit/fill.d.ts +65 -0
  36. package/dist/edit/fill.js +424 -0
  37. package/dist/edit/flatten.d.ts +54 -0
  38. package/dist/edit/flatten.js +462 -0
  39. package/dist/edit/index.d.ts +20 -0
  40. package/dist/edit/index.js +18 -0
  41. package/dist/edit/lexer.d.ts +30 -0
  42. package/dist/edit/lexer.js +285 -0
  43. package/dist/edit/objects.d.ts +68 -0
  44. package/dist/edit/objects.js +83 -0
  45. package/dist/edit/writer.d.ts +60 -0
  46. package/dist/edit/writer.js +283 -0
  47. package/dist/elements/container-element.d.ts +21 -1
  48. package/dist/elements/container-element.js +25 -10
  49. package/dist/elements/forms/checkbox-element.d.ts +51 -0
  50. package/dist/elements/forms/checkbox-element.js +48 -0
  51. package/dist/elements/forms/choice-element.d.ts +53 -0
  52. package/dist/elements/forms/choice-element.js +59 -0
  53. package/dist/elements/forms/push-button-element.d.ts +56 -0
  54. package/dist/elements/forms/push-button-element.js +49 -0
  55. package/dist/elements/forms/radio-element.d.ts +52 -0
  56. package/dist/elements/forms/radio-element.js +49 -0
  57. package/dist/elements/forms/signature-element.d.ts +54 -0
  58. package/dist/elements/forms/signature-element.js +47 -0
  59. package/dist/elements/forms/text-field-element.d.ts +61 -0
  60. package/dist/elements/forms/text-field-element.js +54 -0
  61. package/dist/elements/image-element.d.ts +18 -4
  62. package/dist/elements/image-element.js +27 -12
  63. package/dist/elements/index.d.ts +6 -0
  64. package/dist/elements/index.js +6 -0
  65. package/dist/elements/layout/expanded-element.d.ts +3 -1
  66. package/dist/elements/layout/expanded-element.js +12 -3
  67. package/dist/elements/layout/padding-element.d.ts +4 -2
  68. package/dist/elements/layout/padding-element.js +4 -2
  69. package/dist/elements/layout/page-builder-element.js +2 -0
  70. package/dist/elements/pdf-element.d.ts +59 -1
  71. package/dist/elements/pdf-element.js +62 -1
  72. package/dist/elements/rectangle-element.d.ts +20 -6
  73. package/dist/elements/rectangle-element.js +25 -12
  74. package/dist/elements/row-element.d.ts +24 -1
  75. package/dist/elements/row-element.js +25 -10
  76. package/dist/elements/text-element.d.ts +60 -1
  77. package/dist/elements/text-element.js +132 -33
  78. package/dist/forms/acroform.d.ts +63 -0
  79. package/dist/forms/acroform.js +427 -0
  80. package/dist/forms/appearance.d.ts +58 -0
  81. package/dist/forms/appearance.js +204 -0
  82. package/dist/forms/field.d.ts +164 -0
  83. package/dist/forms/field.js +1 -0
  84. package/dist/forms/pdf.d.ts +19 -0
  85. package/dist/forms/pdf.js +28 -0
  86. package/dist/ir/display-list.d.ts +40 -7
  87. package/dist/ir/display-list.js +8 -1
  88. package/dist/layout/alignment.d.ts +10 -0
  89. package/dist/layout/alignment.js +6 -0
  90. package/dist/layout/box-constraints.d.ts +53 -0
  91. package/dist/layout/box-constraints.js +72 -0
  92. package/dist/layout/insets.d.ts +28 -0
  93. package/dist/layout/insets.js +20 -0
  94. package/dist/renderer/form-field-renderer.d.ts +20 -0
  95. package/dist/renderer/form-field-renderer.js +12 -0
  96. package/dist/renderer/image-renderer.js +2 -1
  97. package/dist/renderer/page-renderer.js +8 -2
  98. package/dist/renderer/pdf-backend.d.ts +6 -0
  99. package/dist/renderer/pdf-backend.js +86 -31
  100. package/dist/renderer/pdf-document-renderer.js +8 -1
  101. package/dist/renderer/pdf-renderer.js +19 -1
  102. package/dist/renderer/rectangle-renderer.js +8 -2
  103. package/dist/renderer/text-renderer.d.ts +2 -2
  104. package/dist/renderer/text-renderer.js +150 -42
  105. package/dist/text/advance.js +5 -2
  106. package/dist/text/arabic.d.ts +20 -0
  107. package/dist/text/arabic.js +101 -0
  108. package/dist/text/bidi.d.ts +33 -0
  109. package/dist/text/bidi.js +94 -0
  110. package/dist/text/font-fallback.d.ts +25 -0
  111. package/dist/text/font-fallback.js +38 -0
  112. package/dist/text/line-breaker.d.ts +35 -1
  113. package/dist/text/line-breaker.js +76 -16
  114. package/dist/text/orphans-widows.d.ts +27 -0
  115. package/dist/text/orphans-widows.js +53 -0
  116. package/dist/text/shape.d.ts +29 -0
  117. package/dist/text/shape.js +67 -0
  118. package/dist/text/text-style.d.ts +25 -0
  119. package/dist/text/text-style.js +44 -1
  120. package/dist/utils/dest-registry.d.ts +5 -2
  121. package/dist/utils/dest-registry.js +5 -5
  122. package/dist/utils/flex-layout.d.ts +24 -4
  123. package/dist/utils/flex-layout.js +169 -7
  124. package/dist/utils/font-metrics.d.ts +5 -0
  125. package/dist/utils/gsub.d.ts +36 -0
  126. package/dist/utils/gsub.js +178 -0
  127. package/dist/utils/inflate.d.ts +16 -0
  128. package/dist/utils/inflate.js +52 -0
  129. package/dist/utils/outline.js +1 -2
  130. package/dist/utils/pdf-object-manager.d.ts +39 -0
  131. package/dist/utils/pdf-object-manager.js +166 -15
  132. package/dist/utils/struct-tree.js +2 -3
  133. package/dist/utils/ttf-parser.d.ts +6 -0
  134. package/dist/utils/ttf-parser.js +19 -0
  135. package/dist/utils/utf8-to-windows1252-encoder.d.ts +6 -0
  136. package/dist/utils/utf8-to-windows1252-encoder.js +10 -0
  137. package/dist/utils/woff.d.ts +10 -0
  138. package/dist/utils/woff.js +116 -0
  139. package/dist/validators/element-validator.js +14 -3
  140. package/package.json +10 -2
@@ -0,0 +1,145 @@
1
+ import { type PdfDict, type PdfObject, type PdfStream } from "./objects.js";
2
+ declare const EMPTY: Uint8Array<ArrayBuffer>;
3
+ /** What `PdfDocument.load` accepts. */
4
+ export interface LoadOptions {
5
+ /** Ceiling on what ONE stream may inflate to, in bytes (default 64 MB). Raise it for a document that
6
+ * is genuinely enormous; a stream past it throws `PdfStreamTooLargeError` rather than being read. */
7
+ maxStreamSize?: number;
8
+ }
9
+ /** What `PdfDocument.open` accepts - everything `load` takes, plus the password. */
10
+ export interface OpenOptions extends LoadOptions {
11
+ password?: string;
12
+ }
13
+ /** Thrown when a file cannot be opened because of its encryption - always saying which of the reasons it
14
+ * is: no password, wrong password, or a revision we do not implement. */
15
+ export declare class PdfEncryptedError extends Error {
16
+ constructor(message: string);
17
+ }
18
+ export declare class PdfDocument {
19
+ readonly bytes: Uint8Array;
20
+ private readonly xref;
21
+ private readonly cache;
22
+ /** Object streams already unpacked, by their object number. */
23
+ private readonly objStmCache;
24
+ /** Set once an encrypted file has been opened with its password. */
25
+ private security?;
26
+ /** Decrypted stream payloads, keyed by the stream's data offset (unique per stream). */
27
+ private readonly plainStreams;
28
+ /** True when the cross-reference index was unusable and had to be rebuilt by scanning. Surfaced so
29
+ * a caller can report it - a silently repaired file is exactly the kind of guess we avoid. */
30
+ recovered: boolean;
31
+ trailer: PdfDict;
32
+ /** Whether this file indexes itself with an xref STREAM rather than a classic table. An incremental
33
+ * update has to follow suit: mixing the two leaves readers that understand only one kind unable to
34
+ * walk the chain back. */
35
+ usesXrefStream: boolean;
36
+ /** Ceiling on what one stream may inflate to; see `inflate.ts`. */
37
+ private maxStreamSize;
38
+ private constructor();
39
+ static load(bytes: Uint8Array, opts?: LoadOptions): PdfDocument;
40
+ /**
41
+ * Open a document, decrypting it when it is password-protected.
42
+ *
43
+ * Async because key derivation and AES are (WebCrypto), and because decryption happens ONCE here,
44
+ * eagerly, rather than at every read: that keeps `getObject`, `streamData` and the whole form layer
45
+ * above them synchronous, which is the same trade the writer makes (`structure.ts` builds the handler
46
+ * before the synchronous constructor).
47
+ */
48
+ static open(bytes: Uint8Array, opts?: OpenOptions): Promise<PdfDocument>;
49
+ /**
50
+ * Build the handler for this file's `/Encrypt` dictionary, or say precisely why we cannot.
51
+ *
52
+ * Every refusal names its reason: no password given, wrong password, or an encryption revision we do
53
+ * not implement. "Cannot open this file" would be the unhelpful answer, and guessing would be worse.
54
+ */
55
+ private securityHandlerFor;
56
+ /**
57
+ * Which cipher and key length actually apply to the streams.
58
+ *
59
+ * For V4 the answer is NOT the top-level `/Length`: the document names a crypt filter in `/StmF` and
60
+ * that filter carries its own `/CFM` and `/Length` (in BYTES, where the top-level one is in bits).
61
+ * `/Identity` means the streams are not enciphered at all - decrypting them anyway would destroy them.
62
+ */
63
+ private streamCipher;
64
+ /**
65
+ * Decrypt every string and stream, once, in place.
66
+ *
67
+ * Three things are deliberately NOT decrypted, because they were never encrypted (ISO 32000-1 7.6.2):
68
+ * the `/Encrypt` dictionary's own strings, a cross-reference stream, and strings of objects that live
69
+ * inside an object stream - the object stream was enciphered as a whole, so its members come out in
70
+ * the clear already.
71
+ */
72
+ private decryptEverything;
73
+ /** The byte offset in the `startxref` trailer at the very end of the file. */
74
+ private findStartXref;
75
+ /** Read the index at `startxref`, then walk `/Prev` back through earlier revisions. Entries already
76
+ * known win: the NEWEST revision is read first, and an older one must not overwrite it. */
77
+ private readXrefChain;
78
+ /** One index section, either format. Returns its trailer dictionary. */
79
+ private readXrefSection;
80
+ /** The classic table: `xref`, then `start count` subsections of 20-byte entries, then `trailer`. */
81
+ private readXrefTable;
82
+ /**
83
+ * An xref STREAM: fixed-width binary rows, widths in `/W`, object numbers from `/Index`. Row type 1
84
+ * is a plain byte offset, type 2 points into an object stream, type 0 is a free slot.
85
+ */
86
+ private readXrefStream;
87
+ /** Record an entry unless a NEWER revision already claimed that object number. */
88
+ private note;
89
+ /**
90
+ * Last resort: scan the whole file for `N G obj` headers and believe those. Used when the index is
91
+ * missing or does not lead anywhere - and flagged via `recovered`, never silently.
92
+ */
93
+ private rebuildByScanning;
94
+ /** The object with that number, or `undefined` when the file does not have it. */
95
+ getObject(num: number): PdfObject | undefined;
96
+ /** Parse `N G obj … endobj` at a byte offset, checking that it really is object `num`. */
97
+ private parseObjectAt;
98
+ /** Unpack an object stream once, then serve its members from the cache. */
99
+ private objectFromStream;
100
+ /** Follow indirect references until a direct object is reached. */
101
+ resolve(o: PdfObject | undefined): PdfObject | undefined;
102
+ /** A dictionary entry, with the reference already followed. */
103
+ lookup(o: PdfObject | undefined, key: string): PdfObject | undefined;
104
+ /**
105
+ * The file is password-protected. Every string and stream in it is enciphered, so anything read out
106
+ * of it is ciphertext until a decryption path exists - which is why the form layer refuses rather
107
+ * than handing back nonsense field names.
108
+ */
109
+ get isEncrypted(): boolean;
110
+ /** True once the password has been accepted and the contents decrypted, so what is read is plaintext.
111
+ * Always true for a document that was never encrypted. */
112
+ get isReadable(): boolean;
113
+ /**
114
+ * Encipher a value that is about to be written back INTO this document, or `undefined` when the
115
+ * document is not encrypted and the value goes in as it is.
116
+ *
117
+ * Needed because we decrypt everything on open: a string we then hand back to the writer is plaintext
118
+ * in memory, and writing it into an encrypted file unchanged would both leak it and produce a document
119
+ * that contradicts its own /Encrypt declaration.
120
+ */
121
+ encryptForWrite(data: Uint8Array): Promise<Uint8Array | undefined>;
122
+ /**
123
+ * Whether values written back into this document can be enciphered again.
124
+ *
125
+ * False for a legacy-encrypted file: we can READ RC4 and AES-128, but writing one would downgrade the
126
+ * user's document to a broken cipher. Asked BEFORE any work is done - otherwise the refusal depends on
127
+ * a string happening to be present in whatever we rewrite, which is a guarantee by accident.
128
+ */
129
+ get canReEncrypt(): boolean;
130
+ /** The generation of an object as the file records it. Members of an object stream are always 0. */
131
+ generationOf(num: number): number;
132
+ get catalog(): PdfObject | undefined;
133
+ /** A stream's bytes as they sit in the file: sliced by `/Length`, still filtered and still encrypted. */
134
+ private rawStream;
135
+ /** A stream's DECODED bytes: deciphered if the document is encrypted, then run through its filters. */
136
+ streamData(stream: PdfStream): Uint8Array;
137
+ /**
138
+ * Undo a PNG predictor (`/Predictor` 10-15), which xref streams almost always use - the IRS form in
139
+ * our fixtures does. Each row is prefixed with its filter type and is decoded against the row above.
140
+ */
141
+ private undoPredictor;
142
+ /** Every object number the index knows, ascending - for walking a whole document. */
143
+ objectNumbers(): number[];
144
+ }
145
+ export { EMPTY };