@inkvero/pdf-tools 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/NOTICE +10 -0
- package/README.md +375 -2
- package/dist/index.d.mts +237 -0
- package/dist/index.mjs +6246 -0
- package/package.json +65 -4
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounds on the work and memory of one `open` or `inspect`. Each is a whole number of at least 0 (`timeoutMs`
|
|
3
|
+
* at least 1). Past one, the call rejects with `PdfToolsError('LIMIT_EXCEEDED')` (`TIMEOUT` for the time),
|
|
4
|
+
* `details.limit` naming the field and `details.value` the limit in force. The defaults suit untrusted
|
|
5
|
+
* uploads of up to 20 MB.
|
|
6
|
+
*/
|
|
7
|
+
interface Limits {
|
|
8
|
+
/** Size of the input in bytes. Default 20 000 000. Checked before anything is parsed. */
|
|
9
|
+
maxBytes: number;
|
|
10
|
+
/** Pages the document may have. Default 2 000. */
|
|
11
|
+
maxPages: number;
|
|
12
|
+
/** Object definitions read (in every revision and inside object streams), and, separately, cross-reference entries in all sections together. Default 250 000. */
|
|
13
|
+
maxObjects: number;
|
|
14
|
+
/**
|
|
15
|
+
* Values parsed (numbers, names, strings, arrays, dictionaries...), in every object together. Default
|
|
16
|
+
* 2 000 000. This bounds the memory a file of many small values can take.
|
|
17
|
+
*/
|
|
18
|
+
maxValues: number;
|
|
19
|
+
/**
|
|
20
|
+
* Values one object may have (an object stream's objects each count on their own). Default 100 000. This
|
|
21
|
+
* bounds the time one object takes to parse, during which nothing else runs.
|
|
22
|
+
*/
|
|
23
|
+
maxObjectValues: number;
|
|
24
|
+
/**
|
|
25
|
+
* Cross-reference sections (tables and streams, also those found only by their offsets) the file may have:
|
|
26
|
+
* one per revision in an ordinary file. Default 10 000.
|
|
27
|
+
*/
|
|
28
|
+
maxSections: number;
|
|
29
|
+
/** Bytes produced by decompression, all streams together. Default 100 000 000. */
|
|
30
|
+
maxInflateBytes: number;
|
|
31
|
+
/** Bytes one decompressed stream may have. Default 20 000 000. */
|
|
32
|
+
maxStreamBytes: number;
|
|
33
|
+
/** How deep arrays and dictionaries may nest, and how long a chain of indirect references may be. Default 64. */
|
|
34
|
+
maxDepth: number;
|
|
35
|
+
/** Time for the whole call, in milliseconds. Default 10 000. */
|
|
36
|
+
timeoutMs: number;
|
|
37
|
+
}
|
|
38
|
+
interface OpenOptions {
|
|
39
|
+
/** Changes to the default limits. */
|
|
40
|
+
limits?: Partial<Limits>;
|
|
41
|
+
/** Stops the work: the call rejects with `ABORTED` at its next checkpoint. */
|
|
42
|
+
signal?: AbortSignal;
|
|
43
|
+
/**
|
|
44
|
+
* The password of an encrypted file (the user or the owner password). Without one, only the empty
|
|
45
|
+
* password is tried, which opens files that are protected by an owner password only.
|
|
46
|
+
*/
|
|
47
|
+
password?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The kinds of active content that are counted. Each count is the number of dictionaries of that kind found
|
|
51
|
+
* in the file (see the README for exactly what is counted).
|
|
52
|
+
*/
|
|
53
|
+
type ActiveKind = "javascript" | "launch" | "uri" | "submitForm" | "importData" | "embeddedFile" | "xfa" | "richMedia" | "openAction" | "additionalActions" | "goToRemote" | "goToEmbedded" | "media" | "threeD" | "unresolved";
|
|
54
|
+
type ActiveCounts = Record<ActiveKind, number>;
|
|
55
|
+
interface PageSize {
|
|
56
|
+
/** Width of the media box in points (1/72 inch), before rotation. */
|
|
57
|
+
w: number;
|
|
58
|
+
/** Height of the media box in points, before rotation. */
|
|
59
|
+
h: number;
|
|
60
|
+
/** Clockwise rotation when shown: 0, 90, 180 or 270. */
|
|
61
|
+
rotate: 0 | 90 | 180 | 270;
|
|
62
|
+
}
|
|
63
|
+
/** The standard Info dictionary entries. */
|
|
64
|
+
type InfoField = "Title" | "Author" | "Subject" | "Keywords" | "Creator" | "Producer" | "CreationDate" | "ModDate" | "Trapped";
|
|
65
|
+
interface Inspection {
|
|
66
|
+
/** The PDF version: the header's, or the catalog's `/Version` when that is later. */
|
|
67
|
+
version: string;
|
|
68
|
+
/** Number of pages, counted in the page tree. */
|
|
69
|
+
pages: number;
|
|
70
|
+
pageSizes: PageSize[];
|
|
71
|
+
/** `null` for a file that is not encrypted. */
|
|
72
|
+
encrypted: null | {
|
|
73
|
+
/** The standard security handler revision (2 to 6). */
|
|
74
|
+
revision: number;
|
|
75
|
+
/** Whether a password is needed to open the file (`false`: only an owner password is set). */
|
|
76
|
+
userPassword: boolean;
|
|
77
|
+
};
|
|
78
|
+
/** The PDF/A part and conformance the file's XMP metadata declares. Declared, not validated. */
|
|
79
|
+
pdfa: null | {
|
|
80
|
+
part: 1 | 2 | 3 | 4;
|
|
81
|
+
conformance: "A" | "B" | "U" | "F" | "E" | null;
|
|
82
|
+
};
|
|
83
|
+
structure: {
|
|
84
|
+
/** Revisions: the original file plus each incremental update. */
|
|
85
|
+
revisions: number;
|
|
86
|
+
/** Whether the file has cross-reference streams. */
|
|
87
|
+
xrefStreams: boolean;
|
|
88
|
+
/** Whether the file has object streams. */
|
|
89
|
+
objectStreams: boolean;
|
|
90
|
+
/** Whether the file says it is linearised ("fast web view"). */
|
|
91
|
+
linearized: boolean;
|
|
92
|
+
/** Object definitions that a later definition of the same object replaces (for example in an incremental update). */
|
|
93
|
+
shadowedObjects: number;
|
|
94
|
+
/**
|
|
95
|
+
* Object definitions found only inside other objects' data or at a cross-reference offset, not in the
|
|
96
|
+
* file's normal sequence of objects. A sign of an unusual or crafted file.
|
|
97
|
+
*/
|
|
98
|
+
hiddenObjects: number;
|
|
99
|
+
/** Whether the file had to be repaired to be read: junk between objects, wrong stream lengths, a cross-reference chain that loops or is missing. */
|
|
100
|
+
repaired: boolean;
|
|
101
|
+
};
|
|
102
|
+
metadata: {
|
|
103
|
+
/** The standard Info entries that are present (their values are returned by `readMetadata`, never here). */
|
|
104
|
+
infoFields: InfoField[];
|
|
105
|
+
/** How many other Info entries there are. */
|
|
106
|
+
customInfoFields: number;
|
|
107
|
+
/** Whether the document has an XMP metadata stream. */
|
|
108
|
+
xmp: boolean;
|
|
109
|
+
};
|
|
110
|
+
/** Active content, counted in every object of every revision, including replaced ones and those in object streams. */
|
|
111
|
+
active: ActiveCounts;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* The e-invoice (Factur-X, ZUGFeRD, XRechnung) the XMP metadata declares in the properties of an invoice
|
|
115
|
+
* extension schema. Declared, not validated. Text from the file: treat it as untrusted input.
|
|
116
|
+
*/
|
|
117
|
+
interface EInvoiceClaim {
|
|
118
|
+
/**
|
|
119
|
+
* The namespace the properties are in: `'factur-x'` (`urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#`,
|
|
120
|
+
* Factur-X 1.0 and ZUGFeRD 2.1 and later, XRechnung included), `'zugferd-2.0'`
|
|
121
|
+
* (`urn:zugferd:pdfa:CrossIndustryDocument:invoice:2p0#`) or `'zugferd-1.0'`
|
|
122
|
+
* (`urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#`).
|
|
123
|
+
*/
|
|
124
|
+
schema: "factur-x" | "zugferd-2.0" | "zugferd-1.0";
|
|
125
|
+
/** `DocumentType`, for example `'INVOICE'`, or `null` when not given. */
|
|
126
|
+
documentType: string | null;
|
|
127
|
+
/** `DocumentFileName`: the name of the embedded invoice XML, for example `'factur-x.xml'`. */
|
|
128
|
+
documentFileName: string | null;
|
|
129
|
+
/** `Version`, for example `'1.0'`. */
|
|
130
|
+
version: string | null;
|
|
131
|
+
/** `ConformanceLevel`: the profile, for example `'EN 16931'`, `'BASIC'` or `'XRECHNUNG'`. */
|
|
132
|
+
conformanceLevel: string | null;
|
|
133
|
+
}
|
|
134
|
+
/** Text taken from the file. Treat it as untrusted input. */
|
|
135
|
+
interface Metadata {
|
|
136
|
+
info: Partial<Record<InfoField, string>>;
|
|
137
|
+
/** The XMP packet as text, or `null` when there is none. */
|
|
138
|
+
xmp: string | null;
|
|
139
|
+
/**
|
|
140
|
+
* The e-invoice the XMP packet declares, or `null`: when there is none, and when the packet does not say
|
|
141
|
+
* one thing for sure (properties in two of the namespaces, a property given twice with different values).
|
|
142
|
+
*/
|
|
143
|
+
eInvoice: EInvoiceClaim | null;
|
|
144
|
+
}
|
|
145
|
+
/** One embedded file: text and numbers taken from the file. Treat the text as untrusted input. */
|
|
146
|
+
interface EmbeddedFile {
|
|
147
|
+
/** The file name the file specification gives: `/UF`, else `/F`. `null` when it gives none (or not as a string). */
|
|
148
|
+
name: string | null;
|
|
149
|
+
/** The media type the embedded file stream declares (its `/Subtype`, for example `'text/xml'`), or `null`. */
|
|
150
|
+
mediaType: string | null;
|
|
151
|
+
/** The file specification's `/AFRelationship` (for example `'Data'`, `'Source'`, `'Alternative'`), or `null`. */
|
|
152
|
+
relationship: string | null;
|
|
153
|
+
/** Bytes of the file after decoding. `null` when the data cannot be decoded within the limits, or is damaged. */
|
|
154
|
+
size: number | null;
|
|
155
|
+
/** Whether the document's `/EmbeddedFiles` name tree lists the file specification. */
|
|
156
|
+
inNameTree: boolean;
|
|
157
|
+
/** Whether the catalog's `/AF` array lists the file specification (a file associated with the whole document). */
|
|
158
|
+
inAssociatedFiles: boolean;
|
|
159
|
+
/**
|
|
160
|
+
* For a file whose declared media type is XML (`text/xml`, `application/xml` or one ending in `+xml`) and
|
|
161
|
+
* whose data was decoded: the result of a strict check. `null` otherwise.
|
|
162
|
+
*/
|
|
163
|
+
xml: XmlCheck | null;
|
|
164
|
+
}
|
|
165
|
+
/** The well-formedness of an embedded XML file. */
|
|
166
|
+
interface XmlCheck {
|
|
167
|
+
/** Whether the data is a well-formed XML 1.0 document with no DOCTYPE. */
|
|
168
|
+
wellFormed: boolean;
|
|
169
|
+
/**
|
|
170
|
+
* Whether the data has a DOCTYPE declaration (a document with one is never reported as well-formed: entities
|
|
171
|
+
* and external subsets are not followed).
|
|
172
|
+
*/
|
|
173
|
+
doctype: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* The root element, when the document is well-formed: its local name and namespace URI (`null` for no
|
|
176
|
+
* namespace). Text from the file.
|
|
177
|
+
*/
|
|
178
|
+
root: {
|
|
179
|
+
name: string;
|
|
180
|
+
namespace: string | null;
|
|
181
|
+
} | null;
|
|
182
|
+
}
|
|
183
|
+
interface EmbeddedFiles {
|
|
184
|
+
/** The embedded files of the document in force, ordered by the object number of their streams. */
|
|
185
|
+
files: EmbeddedFile[];
|
|
186
|
+
/**
|
|
187
|
+
* Whether `files` is the whole story: it accounts for every embedded file `inspect().active.embeddedFile`
|
|
188
|
+
* counts, nothing is `unresolved`, and every object and entry read on the way has one reading only. When
|
|
189
|
+
* `false`, another reader may see other files or other values: do not rely on `files`.
|
|
190
|
+
*/
|
|
191
|
+
complete: boolean;
|
|
192
|
+
}
|
|
193
|
+
/** An opened PDF. */
|
|
194
|
+
interface PdfDoc {
|
|
195
|
+
readonly pageCount: number;
|
|
196
|
+
/** What the file contains: counts and flags only, never text from the file. */
|
|
197
|
+
inspect(): Inspection;
|
|
198
|
+
/** The document's metadata: text from the file. */
|
|
199
|
+
readMetadata(): Metadata;
|
|
200
|
+
/** The embedded files of the document: text from the file. */
|
|
201
|
+
readEmbeddedFiles(): EmbeddedFiles;
|
|
202
|
+
}
|
|
203
|
+
type PdfToolsErrorCode = "NOT_PDF" | "DAMAGED" | "ENCRYPTED" | "WRONG_PASSWORD" | "UNSUPPORTED_SECURITY" | "LIMIT_EXCEEDED" | "TIMEOUT" | "ABORTED" | "INVALID_ARGUMENT" | "INTERNAL";
|
|
204
|
+
/**
|
|
205
|
+
* Extra context of an error. Never holds text taken from the file: only numbers and the fixed names below.
|
|
206
|
+
*/
|
|
207
|
+
interface PdfToolsErrorDetails {
|
|
208
|
+
/** The limit that was reached (`LIMIT_EXCEEDED`). */
|
|
209
|
+
limit?: keyof Limits;
|
|
210
|
+
/** The limit in force (`LIMIT_EXCEEDED`, `TIMEOUT`). */
|
|
211
|
+
value?: number;
|
|
212
|
+
/** A byte offset in the file, where one helps (`DAMAGED`). */
|
|
213
|
+
offset?: number;
|
|
214
|
+
/** What was broken or not supported (`DAMAGED`, `UNSUPPORTED_SECURITY`), from a fixed list of words. */
|
|
215
|
+
reason?: string;
|
|
216
|
+
/** The security handler revision (`ENCRYPTED`, `WRONG_PASSWORD`, `UNSUPPORTED_SECURITY`). */
|
|
217
|
+
revision?: number;
|
|
218
|
+
/** The option at fault (`INVALID_ARGUMENT`). */
|
|
219
|
+
path?: string;
|
|
220
|
+
}
|
|
221
|
+
/** The error thrown (as a rejection) for every problem this library reports. */
|
|
222
|
+
export declare class PdfToolsError extends Error {
|
|
223
|
+
readonly code: PdfToolsErrorCode;
|
|
224
|
+
readonly details: PdfToolsErrorDetails;
|
|
225
|
+
constructor(code: PdfToolsErrorCode, message: string, details?: PdfToolsErrorDetails);
|
|
226
|
+
}
|
|
227
|
+
/** The default limits, for untrusted uploads of up to 20 MB. */
|
|
228
|
+
export declare const defaultLimits: Readonly<Limits>;
|
|
229
|
+
/**
|
|
230
|
+
* Opens a PDF under the limits and reads everything `inspect`, `readMetadata` and `readEmbeddedFiles` report. Rejects with
|
|
231
|
+
* `PdfToolsError` for every problem; anything that cannot be read within the limits is an error, never an
|
|
232
|
+
* empty report.
|
|
233
|
+
*/
|
|
234
|
+
export declare function open(bytes: Uint8Array, options?: OpenOptions): Promise<PdfDoc>;
|
|
235
|
+
/** `open(bytes, options)`, then `inspect()`. */
|
|
236
|
+
export declare function inspect(bytes: Uint8Array, options?: OpenOptions): Promise<Inspection>;
|
|
237
|
+
export type { ActiveCounts, ActiveKind, EInvoiceClaim, EmbeddedFile, EmbeddedFiles, InfoField, Inspection, Limits, Metadata, OpenOptions, PageSize, PdfDoc, PdfToolsErrorCode, PdfToolsErrorDetails, XmlCheck };
|