pdf-parser.js 3.0.7 → 4.5.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/README.md +105 -54
- package/dist/{afm-widths-Dxucrg7D.js → afm-widths-BTu0IDp2.cjs} +1517 -4
- package/dist/{afm-widths-DyDq56Ph.d.cts → afm-widths-CneRyu5l.d.cts} +1 -1
- package/dist/{afm-widths-DyDq56Ph.d.ts → afm-widths-CneRyu5l.d.ts} +1 -1
- package/dist/{afm-widths-BoeTOK2r.cjs → afm-widths-De_QMWgC.js} +1416 -51
- package/dist/afm-widths.cjs +1 -1
- package/dist/afm-widths.d.cts +1 -1
- package/dist/afm-widths.d.ts +1 -1
- package/dist/afm-widths.js +1 -1
- package/dist/annotations.cjs +104 -0
- package/dist/annotations.d.cts +9 -0
- package/dist/annotations.d.ts +9 -0
- package/dist/annotations.js +103 -0
- package/dist/attachments.cjs +65 -0
- package/dist/attachments.d.cts +8 -0
- package/dist/attachments.d.ts +8 -0
- package/dist/attachments.js +64 -0
- package/dist/builtin-encoding.cjs +221 -0
- package/dist/builtin-encoding.d.cts +8 -0
- package/dist/builtin-encoding.d.ts +8 -0
- package/dist/builtin-encoding.js +220 -0
- package/dist/bytes/flate.cjs +1 -1
- package/dist/bytes/flate.js +1 -1
- package/dist/cff.cjs +14 -0
- package/dist/cff.d.cts +4 -1
- package/dist/cff.d.ts +4 -1
- package/dist/cff.js +12 -1
- package/dist/cmap-table.cjs +76 -13
- package/dist/cmap-table.d.cts +9 -1
- package/dist/cmap-table.d.ts +9 -1
- package/dist/cmap-table.js +77 -15
- package/dist/codec.cjs +1 -1
- package/dist/codec.d.cts +134 -0
- package/dist/codec.d.ts +134 -0
- package/dist/codec.js +1 -1
- package/dist/content-read.cjs +1 -1
- package/dist/content-read.d.cts +3 -3
- package/dist/content-read.d.ts +3 -3
- package/dist/content-read.js +1 -1
- package/dist/content-write.cjs +32 -10
- package/dist/content-write.d.cts +12 -5
- package/dist/content-write.d.ts +12 -5
- package/dist/content-write.js +32 -10
- package/dist/crypto/random.cjs +9 -0
- package/dist/crypto/random.d.cts +4 -0
- package/dist/crypto/random.d.ts +4 -0
- package/dist/crypto/random.js +8 -0
- package/dist/diagnostics.d.cts +1 -1
- package/dist/diagnostics.d.ts +1 -1
- package/dist/document.cjs +12 -3
- package/dist/document.d.cts +3 -1
- package/dist/document.d.ts +3 -1
- package/dist/document.js +12 -3
- package/dist/{embedded-font-fREdbxnr.d.ts → embedded-font-B4p9a3O3.d.ts} +3 -1
- package/dist/{embedded-font-BU6Jgcof.d.cts → embedded-font-oc65VGlr.d.cts} +3 -1
- package/dist/embedded-font-write.cjs +1 -1
- package/dist/embedded-font-write.d.cts +3 -3
- package/dist/embedded-font-write.d.ts +3 -3
- package/dist/embedded-font-write.js +1 -1
- package/dist/embedded-font.cjs +46 -15
- package/dist/embedded-font.d.cts +1 -1
- package/dist/embedded-font.d.ts +1 -1
- package/dist/embedded-font.js +46 -15
- package/dist/encoding.cjs +10 -1
- package/dist/encoding.d.cts +10 -1
- package/dist/encoding.d.ts +10 -1
- package/dist/encoding.js +2 -2
- package/dist/encrypt-write.cjs +234 -0
- package/dist/encrypt-write.d.cts +31 -0
- package/dist/encrypt-write.d.ts +31 -0
- package/dist/encrypt-write.js +231 -0
- package/dist/encrypt.cjs +51 -21
- package/dist/encrypt.d.cts +19 -3
- package/dist/encrypt.d.ts +19 -3
- package/dist/encrypt.js +36 -22
- package/dist/filters.cjs +18 -14
- package/dist/filters.d.cts +1 -1
- package/dist/filters.d.ts +1 -1
- package/dist/filters.js +18 -14
- package/dist/font-read.cjs +68 -13
- package/dist/font-read.d.cts +1 -1
- package/dist/font-read.d.ts +1 -1
- package/dist/font-read.js +69 -14
- package/dist/font-registry.cjs +1 -1
- package/dist/font-registry.d.cts +4 -4
- package/dist/font-registry.d.ts +4 -4
- package/dist/font-registry.js +1 -1
- package/dist/font-substitutes.d.cts +1 -1
- package/dist/font-substitutes.d.ts +1 -1
- package/dist/font-tables.cjs +31 -0
- package/dist/font-tables.d.cts +3 -1
- package/dist/font-tables.d.ts +3 -1
- package/dist/font-tables.js +31 -2
- package/dist/fonts.d.cts +2 -2
- package/dist/fonts.d.ts +2 -2
- package/dist/form.cjs +138 -0
- package/dist/form.d.cts +9 -0
- package/dist/form.d.ts +9 -0
- package/dist/form.js +137 -0
- package/dist/gsub-table-BRTQ8EUW.d.ts +10 -0
- package/dist/gsub-table-URczLd85.d.cts +10 -0
- package/dist/gsub-table.cjs +225 -0
- package/dist/gsub-table.d.cts +2 -0
- package/dist/gsub-table.d.ts +2 -0
- package/dist/gsub-table.js +224 -0
- package/dist/image/ccitt-encode.cjs +119 -0
- package/dist/image/ccitt-encode.d.cts +9 -0
- package/dist/image/ccitt-encode.d.ts +9 -0
- package/dist/image/ccitt-encode.js +118 -0
- package/dist/image/ccitt.cjs +3 -0
- package/dist/image/ccitt.d.cts +4 -1
- package/dist/image/ccitt.d.ts +4 -1
- package/dist/image/ccitt.js +1 -1
- package/dist/image/jbig2-bitmap.d.cts +1 -1
- package/dist/image/jbig2-bitmap.d.ts +1 -1
- package/dist/image/jbig2.cjs +3 -1
- package/dist/image/jbig2.js +3 -1
- package/dist/image/jp2-boxes.d.cts +1 -1
- package/dist/image/jp2-boxes.d.ts +1 -1
- package/dist/image/jpeg2000-codestream.d.cts +3 -3
- package/dist/image/jpeg2000-codestream.d.ts +3 -3
- package/dist/image/jpeg2000-t1.d.cts +1 -1
- package/dist/image/jpeg2000-t1.d.ts +1 -1
- package/dist/image/jpeg2000.cjs +1 -0
- package/dist/image/jpeg2000.d.cts +3 -0
- package/dist/image/jpeg2000.d.ts +3 -0
- package/dist/image/jpeg2000.js +1 -0
- package/dist/image/png-decode.cjs +1 -1
- package/dist/image/png-decode.js +1 -1
- package/dist/image/png-filter.d.cts +1 -1
- package/dist/image/png-filter.d.ts +1 -1
- package/dist/images-read.cjs +18 -12
- package/dist/images-read.d.cts +2 -2
- package/dist/images-read.d.ts +2 -2
- package/dist/images-read.js +15 -9
- package/dist/index.cjs +17 -4
- package/dist/index.d.cts +7 -6
- package/dist/index.d.ts +7 -6
- package/dist/index.js +6 -5
- package/dist/interpret.cjs +178 -56
- package/dist/interpret.d.cts +27 -12
- package/dist/interpret.d.ts +27 -12
- package/dist/interpret.js +178 -56
- package/dist/layout.cjs +149 -2
- package/dist/layout.d.cts +394 -1
- package/dist/layout.d.ts +394 -1
- package/dist/layout.js +140 -4
- package/dist/lexer.d.cts +9 -9
- package/dist/lexer.d.ts +9 -9
- package/dist/math-content-write.cjs +1 -1
- package/dist/math-content-write.js +1 -1
- package/dist/math-font-write.cjs +1 -1
- package/dist/math-font-write.d.cts +1 -1
- package/dist/math-font-write.d.ts +1 -1
- package/dist/math-font-write.js +1 -1
- package/dist/math-font.cjs +2 -2
- package/dist/math-font.d.cts +1 -1
- package/dist/math-font.d.ts +1 -1
- package/dist/math-font.js +2 -2
- package/dist/{math-stretch-a_rHNRvc.d.ts → math-stretch-BbC2o4Br.d.ts} +1 -1
- package/dist/{math-stretch-E26z6byT.d.cts → math-stretch-C2JanDAz.d.cts} +1 -1
- package/dist/math-stretch.d.cts +1 -1
- package/dist/math-stretch.d.ts +1 -1
- package/dist/measure.cjs +1 -1
- package/dist/measure.d.cts +1 -1
- package/dist/measure.d.ts +1 -1
- package/dist/measure.js +1 -1
- package/dist/names.cjs +41 -0
- package/dist/names.d.cts +11 -0
- package/dist/names.d.ts +11 -0
- package/dist/names.js +40 -0
- package/dist/navigation.cjs +210 -0
- package/dist/navigation.d.cts +18 -0
- package/dist/navigation.d.ts +18 -0
- package/dist/navigation.js +207 -0
- package/dist/notes-annotation-author.cjs +5 -0
- package/dist/notes-annotation-author.d.cts +4 -0
- package/dist/notes-annotation-author.d.ts +4 -0
- package/dist/notes-annotation-author.js +4 -0
- package/dist/objects-B7HdMeWS.d.cts +59 -0
- package/dist/objects-B7HdMeWS.d.ts +59 -0
- package/dist/objects.d.cts +1 -58
- package/dist/objects.d.ts +1 -58
- package/dist/optional-content.cjs +63 -0
- package/dist/optional-content.d.cts +12 -0
- package/dist/optional-content.d.ts +12 -0
- package/dist/optional-content.js +62 -0
- package/dist/parse.cjs +1 -1
- package/dist/parse.d.cts +2 -2
- package/dist/parse.d.ts +2 -2
- package/dist/parse.js +1 -1
- package/dist/pdf-text.cjs +21 -0
- package/dist/pdf-text.d.cts +5 -0
- package/dist/pdf-text.d.ts +5 -0
- package/dist/pdf-text.js +19 -0
- package/dist/predictors.d.cts +1 -1
- package/dist/predictors.d.ts +1 -1
- package/dist/read.cjs +287 -72
- package/dist/read.d.cts +2 -4
- package/dist/read.d.ts +2 -4
- package/dist/read.js +284 -67
- package/dist/serialize.cjs +5 -1
- package/dist/serialize.d.cts +3 -2
- package/dist/serialize.d.ts +3 -2
- package/dist/serialize.js +5 -2
- package/dist/sfnt-subset.cjs +10 -3
- package/dist/sfnt-subset.d.cts +1 -1
- package/dist/sfnt-subset.d.ts +1 -1
- package/dist/sfnt-subset.js +10 -3
- package/dist/structure.cjs +184 -0
- package/dist/structure.d.cts +12 -0
- package/dist/structure.d.ts +12 -0
- package/dist/structure.js +183 -0
- package/dist/tounicode.cjs +8 -3
- package/dist/tounicode.d.cts +2 -2
- package/dist/tounicode.d.ts +2 -2
- package/dist/tounicode.js +8 -3
- package/dist/util/base64.cjs +4 -4
- package/dist/util/base64.js +4 -4
- package/dist/winansi.cjs +1 -1
- package/dist/winansi.d.cts +1 -1
- package/dist/winansi.d.ts +1 -1
- package/dist/winansi.js +1 -1
- package/dist/write.cjs +464 -21
- package/dist/write.d.cts +4 -3
- package/dist/write.d.ts +4 -3
- package/dist/write.js +464 -20
- package/dist/xmp.cjs +46 -0
- package/dist/xmp.d.cts +14 -0
- package/dist/xmp.d.ts +14 -0
- package/dist/xmp.js +45 -0
- package/dist/xref.cjs +2 -2
- package/dist/xref.d.cts +3 -3
- package/dist/xref.d.ts +3 -3
- package/dist/xref.js +2 -2
- package/package.json +46 -31
- package/dist/image/png-encode.cjs +0 -64
- package/dist/image/png-encode.d.cts +0 -8
- package/dist/image/png-encode.d.ts +0 -8
- package/dist/image/png-encode.js +0 -63
package/dist/write.cjs
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_afm_widths = require("./afm-widths-
|
|
3
|
-
const
|
|
4
|
-
const require_bytes_writer = require("./bytes/writer.cjs");
|
|
2
|
+
const require_afm_widths = require("./afm-widths-BTu0IDp2.cjs");
|
|
3
|
+
const require_notes_annotation_author = require("./notes-annotation-author.cjs");
|
|
5
4
|
const require_objects = require("./objects.cjs");
|
|
5
|
+
const require_bytes_writer = require("./bytes/writer.cjs");
|
|
6
|
+
const require_serialize = require("./serialize.cjs");
|
|
7
|
+
const require_bytes_reader = require("./bytes/reader.cjs");
|
|
6
8
|
const require_bytes_flate = require("./bytes/flate.cjs");
|
|
9
|
+
const require_util_base64 = require("./util/base64.cjs");
|
|
10
|
+
const require_parse = require("./parse.cjs");
|
|
7
11
|
const require_util_abort = require("./util/abort.cjs");
|
|
12
|
+
const require_crypto_random = require("./crypto/random.cjs");
|
|
13
|
+
const require_encrypt_write = require("./encrypt-write.cjs");
|
|
8
14
|
const require_image_jpeg_info = require("./image/jpeg-info.cjs");
|
|
15
|
+
const require_image_ccitt_encode = require("./image/ccitt-encode.cjs");
|
|
9
16
|
const require_image_png_decode = require("./image/png-decode.cjs");
|
|
10
17
|
const require_embedded_font = require("./embedded-font.cjs");
|
|
11
|
-
const require_serialize = require("./serialize.cjs");
|
|
12
18
|
const require_content_write = require("./content-write.cjs");
|
|
13
19
|
const require_embedded_font_write = require("./embedded-font-write.cjs");
|
|
14
20
|
const require_font_registry = require("./font-registry.cjs");
|
|
@@ -29,6 +35,7 @@ const FLAG_SERIF = 2;
|
|
|
29
35
|
const FLAG_NONSYMBOLIC = 32;
|
|
30
36
|
const FLAG_ITALIC = 64;
|
|
31
37
|
const FLAG_FORCE_BOLD = 262144;
|
|
38
|
+
const FILE_ID_BYTES = 16;
|
|
32
39
|
function textToPdfString(text) {
|
|
33
40
|
const bytes = new Uint8Array(2 + text.length * 2);
|
|
34
41
|
bytes[0] = 254;
|
|
@@ -139,9 +146,57 @@ function pngImageDict(width, height, colorSpace, compress) {
|
|
|
139
146
|
if (compress) entries.set("Filter", require_objects.pdfName("FlateDecode"));
|
|
140
147
|
return require_objects.pdfDict(entries);
|
|
141
148
|
}
|
|
149
|
+
function packBilevel(raw) {
|
|
150
|
+
const rowBytes = raw.width + 7 >> 3;
|
|
151
|
+
const packed = new Uint8Array(rowBytes * raw.height);
|
|
152
|
+
for (let y = 0; y < raw.height; y++) for (let x = 0; x < raw.width; x++) {
|
|
153
|
+
const sample = raw.data[y * raw.width + x] ?? 0;
|
|
154
|
+
if (sample !== 0 && sample !== 255) return;
|
|
155
|
+
if (sample === 255) {
|
|
156
|
+
const index = y * rowBytes + (x >> 3);
|
|
157
|
+
packed[index] = (packed[index] ?? 0) | 128 >> (x & 7);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return packed;
|
|
161
|
+
}
|
|
142
162
|
function preparePngImage(bytes, compress) {
|
|
143
163
|
const raw = require_image_png_decode.decodePng(bytes);
|
|
144
164
|
const colorSpace = raw.channels === 1 ? "DeviceGray" : "DeviceRGB";
|
|
165
|
+
if (compress && raw.channels === 1 && raw.alpha === void 0) {
|
|
166
|
+
const bilevel = packBilevel(raw);
|
|
167
|
+
if (bilevel !== void 0) {
|
|
168
|
+
const flate = require_bytes_flate.deflate(raw.data);
|
|
169
|
+
const g4 = require_image_ccitt_encode.encodeCcittFax(bilevel, {
|
|
170
|
+
columns: raw.width,
|
|
171
|
+
rows: raw.height,
|
|
172
|
+
maxBytes: flate.length
|
|
173
|
+
});
|
|
174
|
+
if (g4 !== void 0) return {
|
|
175
|
+
dict: require_objects.pdfDict(/* @__PURE__ */ new Map([
|
|
176
|
+
["Type", require_objects.pdfName("XObject")],
|
|
177
|
+
["Subtype", require_objects.pdfName("Image")],
|
|
178
|
+
["Width", require_objects.pdfNum(raw.width)],
|
|
179
|
+
["Height", require_objects.pdfNum(raw.height)],
|
|
180
|
+
["ColorSpace", require_objects.pdfName("DeviceGray")],
|
|
181
|
+
["BitsPerComponent", require_objects.pdfNum(1)],
|
|
182
|
+
["Filter", require_objects.pdfName("CCITTFaxDecode")],
|
|
183
|
+
["DecodeParms", require_objects.pdfDict(/* @__PURE__ */ new Map([
|
|
184
|
+
["K", require_objects.pdfNum(-1)],
|
|
185
|
+
["Columns", require_objects.pdfNum(raw.width)],
|
|
186
|
+
["Rows", require_objects.pdfNum(raw.height)],
|
|
187
|
+
["BlackIs1", require_objects.pdfBool(false)]
|
|
188
|
+
]))]
|
|
189
|
+
])),
|
|
190
|
+
raw: g4,
|
|
191
|
+
alpha: void 0
|
|
192
|
+
};
|
|
193
|
+
return {
|
|
194
|
+
dict: pngImageDict(raw.width, raw.height, colorSpace, true),
|
|
195
|
+
raw: flate,
|
|
196
|
+
alpha: void 0
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
}
|
|
145
200
|
return {
|
|
146
201
|
dict: pngImageDict(raw.width, raw.height, colorSpace, compress),
|
|
147
202
|
raw: compress ? require_bytes_flate.deflate(raw.data) : raw.data,
|
|
@@ -177,11 +232,59 @@ function buildLinkAnnotDict(link) {
|
|
|
177
232
|
})
|
|
178
233
|
});
|
|
179
234
|
}
|
|
235
|
+
function destinationViewArray(target) {
|
|
236
|
+
const n = (value) => value === void 0 ? require_objects.pdfNull() : require_objects.pdfNum(value);
|
|
237
|
+
if (target.kind === "xyz") return [
|
|
238
|
+
require_objects.pdfName("XYZ"),
|
|
239
|
+
n(target.leftPt),
|
|
240
|
+
n(target.topPt),
|
|
241
|
+
n(target.zoom)
|
|
242
|
+
];
|
|
243
|
+
if (target.kind === "fitH") return [require_objects.pdfName("FitH"), n(target.topPt)];
|
|
244
|
+
if (target.kind === "fitV") return [require_objects.pdfName("FitV"), n(target.leftPt)];
|
|
245
|
+
if (target.kind === "fitR") return [
|
|
246
|
+
require_objects.pdfName("FitR"),
|
|
247
|
+
n(target.leftPt),
|
|
248
|
+
n(target.bottomPt),
|
|
249
|
+
n(target.rightPt),
|
|
250
|
+
n(target.topPt)
|
|
251
|
+
];
|
|
252
|
+
if (target.kind === "fitBH") return [require_objects.pdfName("FitBH"), n(target.topPt)];
|
|
253
|
+
if (target.kind === "fitBV") return [require_objects.pdfName("FitBV"), n(target.leftPt)];
|
|
254
|
+
return [require_objects.pdfName(target.kind === "fitB" ? "FitB" : "Fit")];
|
|
255
|
+
}
|
|
256
|
+
function resolveDestinationArray(doc, pageAllocs, name, what) {
|
|
257
|
+
const destination = doc.destinations?.find((d) => d.name === name);
|
|
258
|
+
if (destination === void 0) throw new Error(`${what} names destination "${name}", which the document's destinations table does not carry -- this is a caller-invariant violation`);
|
|
259
|
+
const targetPage = pageAllocs[destination.pageIndex];
|
|
260
|
+
if (targetPage === void 0) throw new Error(`destination "${destination.name}" names page index ${destination.pageIndex}, which is beyond the document's own pages -- this is a caller-invariant violation`);
|
|
261
|
+
return [require_objects.pdfRef(targetPage.pageNum, 0), ...destinationViewArray(destination.target)];
|
|
262
|
+
}
|
|
263
|
+
function buildInternalLinkAnnotDict(link, doc, pageAllocs) {
|
|
264
|
+
return require_objects.pdfDict({
|
|
265
|
+
Type: require_objects.pdfName("Annot"),
|
|
266
|
+
Subtype: require_objects.pdfName("Link"),
|
|
267
|
+
Rect: require_objects.pdfArray([
|
|
268
|
+
link.xPt,
|
|
269
|
+
link.yPt,
|
|
270
|
+
link.xPt + link.widthPt,
|
|
271
|
+
link.yPt + link.heightPt
|
|
272
|
+
].map((v) => require_objects.pdfNum(v))),
|
|
273
|
+
Border: require_objects.pdfArray([
|
|
274
|
+
0,
|
|
275
|
+
0,
|
|
276
|
+
0
|
|
277
|
+
].map((v) => require_objects.pdfNum(v))),
|
|
278
|
+
Dest: require_objects.pdfArray(resolveDestinationArray(doc, pageAllocs, link.destination, "internal link"))
|
|
279
|
+
});
|
|
280
|
+
}
|
|
180
281
|
function isLinkItem(item) {
|
|
181
282
|
return item.kind === "link";
|
|
182
283
|
}
|
|
284
|
+
function isInternalLinkItem(item) {
|
|
285
|
+
return item.kind === "internalLink";
|
|
286
|
+
}
|
|
183
287
|
const NOTES_ANNOTATION_HIDDEN_FLAG = 2;
|
|
184
|
-
const NOTES_ANNOTATION_AUTHOR = "documents.js:notes";
|
|
185
288
|
function buildNotesAnnotDict(notes) {
|
|
186
289
|
return require_objects.pdfDict({
|
|
187
290
|
Type: require_objects.pdfName("Annot"),
|
|
@@ -193,13 +296,31 @@ function buildNotesAnnotDict(notes) {
|
|
|
193
296
|
0
|
|
194
297
|
].map((n) => require_objects.pdfNum(n))),
|
|
195
298
|
Contents: textToPdfString(notes),
|
|
196
|
-
T: textToPdfString(NOTES_ANNOTATION_AUTHOR),
|
|
299
|
+
T: textToPdfString(require_notes_annotation_author.NOTES_ANNOTATION_AUTHOR),
|
|
197
300
|
F: require_objects.pdfNum(NOTES_ANNOTATION_HIDDEN_FLAG)
|
|
198
301
|
});
|
|
199
302
|
}
|
|
200
303
|
function xrefEntry(offset, generation, inUse) {
|
|
201
304
|
return `${offset.toString().padStart(10, "0")} ${generation.toString().padStart(5, "0")} ${inUse ? "n" : "f"} \n`;
|
|
202
305
|
}
|
|
306
|
+
function parseResidueRow(residue) {
|
|
307
|
+
const reader = new require_bytes_reader.ByteReader(new TextEncoder().encode(residue.xml));
|
|
308
|
+
return require_parse.parseValue(reader, () => {});
|
|
309
|
+
}
|
|
310
|
+
function objectContainsReference(obj) {
|
|
311
|
+
if (obj.kind === "ref") return true;
|
|
312
|
+
if (obj.kind === "array") return obj.items.some(objectContainsReference);
|
|
313
|
+
if (obj.kind === "dict") return [...obj.entries.values()].some(objectContainsReference);
|
|
314
|
+
if (obj.kind === "stream") return [...obj.dict.entries.values()].some(objectContainsReference);
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
function restoreResidueRow(source, key) {
|
|
318
|
+
const row = source?.[key];
|
|
319
|
+
if (row === void 0) return;
|
|
320
|
+
const parsed = parseResidueRow(row);
|
|
321
|
+
if (parsed === void 0 || objectContainsReference(parsed)) return;
|
|
322
|
+
return parsed;
|
|
323
|
+
}
|
|
203
324
|
function writePdf(doc, options = {}) {
|
|
204
325
|
const compress = options.compress ?? true;
|
|
205
326
|
const registry = options.fonts;
|
|
@@ -272,13 +393,99 @@ function writePdf(doc, options = {}) {
|
|
|
272
393
|
pageNum: nextObjNum++,
|
|
273
394
|
contentsNum: nextObjNum++
|
|
274
395
|
}));
|
|
396
|
+
const encryptDictNum = options.encryption === void 0 ? void 0 : nextObjNum++;
|
|
397
|
+
const attachmentAllocs = (doc.attachments ?? []).map(() => ({
|
|
398
|
+
fileNum: nextObjNum++,
|
|
399
|
+
specNum: nextObjNum++
|
|
400
|
+
}));
|
|
401
|
+
const attachmentsNamesNum = attachmentAllocs.length > 0 ? nextObjNum++ : void 0;
|
|
402
|
+
const outlineRootNum = (doc.outline ?? []).length > 0 ? nextObjNum++ : void 0;
|
|
403
|
+
const outlineItemNums = [];
|
|
404
|
+
if (outlineRootNum !== void 0) {
|
|
405
|
+
const countItems = (items) => {
|
|
406
|
+
let n = 0;
|
|
407
|
+
for (const item of items) n += 1 + countItems(item.children);
|
|
408
|
+
return n;
|
|
409
|
+
};
|
|
410
|
+
for (let i = 0; i < countItems(doc.outline ?? []); i += 1) outlineItemNums.push(nextObjNum++);
|
|
411
|
+
}
|
|
412
|
+
const layerNumByName = /* @__PURE__ */ new Map();
|
|
413
|
+
for (const layer of doc.layers ?? []) layerNumByName.set(layer.name, nextObjNum++);
|
|
414
|
+
const formObjectNums = [];
|
|
415
|
+
const countFieldObjects = (fields) => {
|
|
416
|
+
let n = 0;
|
|
417
|
+
for (const field of fields) n += 1 + (field.fieldType !== "group" && field.widgets.length > 1 ? field.widgets.length - 1 : 0) + countFieldObjects(field.children);
|
|
418
|
+
return n;
|
|
419
|
+
};
|
|
420
|
+
for (let i = 0; i < countFieldObjects(doc.form ?? []); i += 1) formObjectNums.push(nextObjNum++);
|
|
421
|
+
const formNumByField = /* @__PURE__ */ new Map();
|
|
422
|
+
const formExtraWidgetNums = /* @__PURE__ */ new Map();
|
|
423
|
+
let formNumCursor = 0;
|
|
424
|
+
const claimFormNums = (fields) => {
|
|
425
|
+
for (const field of fields) {
|
|
426
|
+
formNumByField.set(field, formObjectNums[formNumCursor++]);
|
|
427
|
+
if (field.fieldType !== "group" && field.widgets.length > 1) formExtraWidgetNums.set(field, field.widgets.slice(1).map(() => formObjectNums[formNumCursor++]));
|
|
428
|
+
claimFormNums(field.children);
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
claimFormNums(doc.form ?? []);
|
|
432
|
+
const formNumOf = (field) => {
|
|
433
|
+
const num = formNumByField.get(field);
|
|
434
|
+
if (num === void 0) throw new Error("AcroForm field object number was not claimed -- this is a writePdf internal invariant violation");
|
|
435
|
+
return num;
|
|
436
|
+
};
|
|
437
|
+
const structElementNumById = /* @__PURE__ */ new Map();
|
|
438
|
+
const countElements = (elements) => {
|
|
439
|
+
let n = 0;
|
|
440
|
+
for (const element of elements) {
|
|
441
|
+
structElementNumById.set(element.id, nextObjNum++);
|
|
442
|
+
n += 1 + countElements(element.children);
|
|
443
|
+
}
|
|
444
|
+
return n;
|
|
445
|
+
};
|
|
446
|
+
const structElementCount = countElements(doc.structure ?? []);
|
|
447
|
+
const structRootNum = structElementCount > 0 ? nextObjNum++ : void 0;
|
|
448
|
+
const structParentTreeNum = structElementCount > 0 ? nextObjNum++ : void 0;
|
|
449
|
+
const residueXmpNum = doc.source?.xmp !== void 0 ? nextObjNum++ : void 0;
|
|
275
450
|
const objects = [];
|
|
451
|
+
const catalogEntries = [["Type", require_objects.pdfName("Catalog")], ["Pages", require_objects.pdfRef(pagesNum, 0)]];
|
|
452
|
+
if (attachmentsNamesNum !== void 0) catalogEntries.push(["Names", require_objects.pdfRef(attachmentsNamesNum, 0)]);
|
|
453
|
+
if (outlineRootNum !== void 0) catalogEntries.push(["Outlines", require_objects.pdfRef(outlineRootNum, 0)]);
|
|
454
|
+
if (layerNumByName.size > 0) {
|
|
455
|
+
const ocgRefs = [...layerNumByName.values()].map((num) => require_objects.pdfRef(num, 0));
|
|
456
|
+
const visible = [];
|
|
457
|
+
const hidden = [];
|
|
458
|
+
for (const layer of doc.layers ?? []) {
|
|
459
|
+
const num = layerNumByName.get(layer.name);
|
|
460
|
+
(layer.visible ? visible : hidden).push(require_objects.pdfRef(num, 0));
|
|
461
|
+
}
|
|
462
|
+
const defaultConfigEntries = [];
|
|
463
|
+
if (visible.length > 0) defaultConfigEntries.push(["ON", require_objects.pdfArray(visible)]);
|
|
464
|
+
if (hidden.length > 0) defaultConfigEntries.push(["OFF", require_objects.pdfArray(hidden)]);
|
|
465
|
+
catalogEntries.push(["OCProperties", require_objects.pdfDict({
|
|
466
|
+
OCGs: require_objects.pdfArray(ocgRefs),
|
|
467
|
+
D: require_objects.pdfDict(Object.fromEntries(defaultConfigEntries))
|
|
468
|
+
})]);
|
|
469
|
+
}
|
|
470
|
+
if (doc.form !== void 0 && doc.form.length > 0) catalogEntries.push(["AcroForm", require_objects.pdfDict({ Fields: require_objects.pdfArray(doc.form.map((field) => require_objects.pdfRef(formNumOf(field), 0))) })]);
|
|
471
|
+
if (structRootNum !== void 0) catalogEntries.push(["StructTreeRoot", require_objects.pdfRef(structRootNum, 0)]);
|
|
472
|
+
if (residueXmpNum !== void 0) catalogEntries.push(["Metadata", require_objects.pdfRef(residueXmpNum, 0)]);
|
|
473
|
+
const trailerIdRestore = restoreResidueRow(doc.source, "trailer-id");
|
|
474
|
+
for (const [rowKey, catalogKey] of [
|
|
475
|
+
["viewer-preferences", "ViewerPreferences"],
|
|
476
|
+
["page-mode", "PageMode"],
|
|
477
|
+
["page-layout", "PageLayout"],
|
|
478
|
+
["output-intents", "OutputIntents"],
|
|
479
|
+
["piece-info", "PieceInfo"],
|
|
480
|
+
["legal", "Legal"],
|
|
481
|
+
["collection", "Collection"]
|
|
482
|
+
]) {
|
|
483
|
+
const restored = restoreResidueRow(doc.source, rowKey);
|
|
484
|
+
if (restored !== void 0) catalogEntries.push([catalogKey, restored]);
|
|
485
|
+
}
|
|
276
486
|
objects.push({
|
|
277
487
|
num: catalogNum,
|
|
278
|
-
value: require_objects.pdfDict(
|
|
279
|
-
Type: require_objects.pdfName("Catalog"),
|
|
280
|
-
Pages: require_objects.pdfRef(pagesNum, 0)
|
|
281
|
-
})
|
|
488
|
+
value: require_objects.pdfDict(Object.fromEntries(catalogEntries))
|
|
282
489
|
});
|
|
283
490
|
objects.push({
|
|
284
491
|
num: pagesNum,
|
|
@@ -292,6 +499,185 @@ function writePdf(doc, options = {}) {
|
|
|
292
499
|
num: infoNum,
|
|
293
500
|
value: buildInfoDict(doc)
|
|
294
501
|
});
|
|
502
|
+
for (const [index, attachment] of (doc.attachments ?? []).entries()) {
|
|
503
|
+
const alloc = attachmentAllocs[index];
|
|
504
|
+
const fileEntries = [["Type", require_objects.pdfName("EmbeddedFile")]];
|
|
505
|
+
if (attachment.mimeType !== void 0) fileEntries.push(["Subtype", require_objects.pdfName(attachment.mimeType)]);
|
|
506
|
+
objects.push({
|
|
507
|
+
num: alloc.fileNum,
|
|
508
|
+
value: require_objects.pdfStream(require_objects.pdfDict(Object.fromEntries(fileEntries)), require_util_base64.base64ToBytes(attachment.base64))
|
|
509
|
+
});
|
|
510
|
+
const specEntries = [
|
|
511
|
+
["Type", require_objects.pdfName("Filespec")],
|
|
512
|
+
["F", require_objects.pdfLiteralString(new TextEncoder().encode(attachment.name))],
|
|
513
|
+
["EF", require_objects.pdfDict({ F: require_objects.pdfRef(alloc.fileNum, 0) })]
|
|
514
|
+
];
|
|
515
|
+
if (attachment.description !== void 0) specEntries.push(["Desc", require_objects.pdfLiteralString(new TextEncoder().encode(attachment.description))]);
|
|
516
|
+
objects.push({
|
|
517
|
+
num: alloc.specNum,
|
|
518
|
+
value: require_objects.pdfDict(Object.fromEntries(specEntries))
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
if (attachmentsNamesNum !== void 0) {
|
|
522
|
+
const names = [];
|
|
523
|
+
for (const [index, attachment] of (doc.attachments ?? []).entries()) {
|
|
524
|
+
names.push(require_objects.pdfLiteralString(new TextEncoder().encode(attachment.name)));
|
|
525
|
+
names.push(require_objects.pdfRef(attachmentAllocs[index].specNum, 0));
|
|
526
|
+
}
|
|
527
|
+
objects.push({
|
|
528
|
+
num: attachmentsNamesNum,
|
|
529
|
+
value: require_objects.pdfDict({ EmbeddedFiles: require_objects.pdfDict({ Names: require_objects.pdfArray(names) }) })
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
if (outlineRootNum !== void 0 && (doc.outline ?? []).length > 0) {
|
|
533
|
+
let itemCursor = 0;
|
|
534
|
+
const emitItems = (items, parentNum) => {
|
|
535
|
+
const siblingNums = [];
|
|
536
|
+
let prevNum;
|
|
537
|
+
for (const item of items) {
|
|
538
|
+
const ownNum = outlineItemNums[itemCursor];
|
|
539
|
+
itemCursor += 1;
|
|
540
|
+
siblingNums.push(ownNum);
|
|
541
|
+
const childNums = emitItems(item.children, ownNum);
|
|
542
|
+
const entries = [["Title", require_objects.pdfLiteralString(new TextEncoder().encode(item.title))], ["Parent", require_objects.pdfRef(parentNum, 0)]];
|
|
543
|
+
if (item.destination !== void 0) entries.push(["Dest", require_objects.pdfArray(resolveDestinationArray(doc, pageAllocs, item.destination, `outline item "${item.title}"`))]);
|
|
544
|
+
if (prevNum !== void 0) entries.push(["Prev", require_objects.pdfRef(prevNum, 0)]);
|
|
545
|
+
if (siblingNums.length < items.length) entries.push(["Next", require_objects.pdfRef(outlineItemNums[itemCursor], 0)]);
|
|
546
|
+
if (childNums.length > 0) {
|
|
547
|
+
entries.push(["First", require_objects.pdfRef(childNums[0], 0)]);
|
|
548
|
+
entries.push(["Last", require_objects.pdfRef(childNums[childNums.length - 1], 0)]);
|
|
549
|
+
entries.push(["Count", require_objects.pdfNum(childNums.length)]);
|
|
550
|
+
}
|
|
551
|
+
objects.push({
|
|
552
|
+
num: ownNum,
|
|
553
|
+
value: require_objects.pdfDict(Object.fromEntries(entries))
|
|
554
|
+
});
|
|
555
|
+
prevNum = ownNum;
|
|
556
|
+
}
|
|
557
|
+
return siblingNums;
|
|
558
|
+
};
|
|
559
|
+
const topLevelNums = emitItems(doc.outline ?? [], outlineRootNum);
|
|
560
|
+
objects.push({
|
|
561
|
+
num: outlineRootNum,
|
|
562
|
+
value: require_objects.pdfDict({
|
|
563
|
+
Type: require_objects.pdfName("Outlines"),
|
|
564
|
+
First: require_objects.pdfRef(topLevelNums[0], 0),
|
|
565
|
+
Last: require_objects.pdfRef(topLevelNums[topLevelNums.length - 1], 0)
|
|
566
|
+
})
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
for (const layer of doc.layers ?? []) {
|
|
570
|
+
const num = layerNumByName.get(layer.name);
|
|
571
|
+
objects.push({
|
|
572
|
+
num,
|
|
573
|
+
value: require_objects.pdfDict({
|
|
574
|
+
Type: require_objects.pdfName("OCG"),
|
|
575
|
+
Name: require_objects.pdfLiteralString(new TextEncoder().encode(layer.name))
|
|
576
|
+
})
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
const widgetRectArray = (widget) => require_objects.pdfArray([
|
|
580
|
+
widget.xPt,
|
|
581
|
+
widget.yPt,
|
|
582
|
+
widget.xPt + widget.widthPt,
|
|
583
|
+
widget.yPt + widget.heightPt
|
|
584
|
+
].map((n) => require_objects.pdfNum(n)));
|
|
585
|
+
const widgetDict = (widget) => require_objects.pdfDict({
|
|
586
|
+
Subtype: require_objects.pdfName("Widget"),
|
|
587
|
+
Rect: widgetRectArray(widget),
|
|
588
|
+
P: require_objects.pdfRef(pageAllocs[widget.pageIndex].pageNum, 0)
|
|
589
|
+
});
|
|
590
|
+
const FIELD_TYPE_PDF_NAME = {
|
|
591
|
+
text: "Tx",
|
|
592
|
+
checkbox: "Btn",
|
|
593
|
+
radio: "Btn",
|
|
594
|
+
button: "Btn",
|
|
595
|
+
listbox: "Ch",
|
|
596
|
+
combobox: "Ch",
|
|
597
|
+
signature: "Sig"
|
|
598
|
+
};
|
|
599
|
+
const emitFormFieldObjects = (fields, parentName) => {
|
|
600
|
+
for (const field of fields) {
|
|
601
|
+
const ownName = parentName.length > 0 && field.name.startsWith(`${parentName}.`) ? field.name.slice(parentName.length + 1) : field.name;
|
|
602
|
+
const entries = [];
|
|
603
|
+
if (ownName.length > 0) entries.push(["T", require_objects.pdfLiteralString(new TextEncoder().encode(ownName))]);
|
|
604
|
+
if (field.alias !== void 0) entries.push(["TU", require_objects.pdfLiteralString(new TextEncoder().encode(field.alias))]);
|
|
605
|
+
if (field.fieldType === "group") entries.push(["Kids", require_objects.pdfArray(field.children.map((child) => require_objects.pdfRef(formNumOf(child), 0)))]);
|
|
606
|
+
else {
|
|
607
|
+
entries.push(["FT", require_objects.pdfName(FIELD_TYPE_PDF_NAME[field.fieldType])]);
|
|
608
|
+
const FLAG_READ_ONLY = 1;
|
|
609
|
+
const FLAG_PUSHBUTTON = 4;
|
|
610
|
+
const FLAG_RADIO = 32768;
|
|
611
|
+
const FLAG_COMBO = 131072;
|
|
612
|
+
let flags = 0;
|
|
613
|
+
if (field.readOnly === true) flags |= FLAG_READ_ONLY;
|
|
614
|
+
if (field.fieldType === "button") flags |= FLAG_PUSHBUTTON;
|
|
615
|
+
if (field.fieldType === "radio") flags |= FLAG_RADIO;
|
|
616
|
+
if (field.fieldType === "combobox") flags |= FLAG_COMBO;
|
|
617
|
+
if (flags !== 0) entries.push(["Ff", require_objects.pdfNum(flags)]);
|
|
618
|
+
if (field.fieldType === "text" || field.fieldType === "listbox" || field.fieldType === "combobox") {
|
|
619
|
+
if (field.value !== void 0) entries.push(["V", require_objects.pdfLiteralString(new TextEncoder().encode(field.value))]);
|
|
620
|
+
} else if (field.fieldType === "checkbox" || field.fieldType === "radio") entries.push(["V", require_objects.pdfName(field.checked === false || field.value === void 0 ? field.value ?? (field.checked === true ? "Yes" : "Off") : field.value)]);
|
|
621
|
+
if (field.options !== void 0) entries.push(["Opt", require_objects.pdfArray(field.options.map((option) => require_objects.pdfLiteralString(new TextEncoder().encode(option))))]);
|
|
622
|
+
const firstWidget = field.widgets[0];
|
|
623
|
+
if (field.widgets.length === 1 && firstWidget !== void 0) {
|
|
624
|
+
entries.push(["Subtype", require_objects.pdfName("Widget")]);
|
|
625
|
+
entries.push(["Rect", widgetRectArray(firstWidget)]);
|
|
626
|
+
entries.push(["P", require_objects.pdfRef(pageAllocs[firstWidget.pageIndex].pageNum, 0)]);
|
|
627
|
+
} else if (field.widgets.length > 1 && firstWidget !== void 0) {
|
|
628
|
+
const extraNums = formExtraWidgetNums.get(field) ?? [];
|
|
629
|
+
entries.push(["Kids", require_objects.pdfArray([widgetDict(firstWidget), ...extraNums.map((num) => require_objects.pdfRef(num, 0))])]);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
objects.push({
|
|
633
|
+
num: formNumOf(field),
|
|
634
|
+
value: require_objects.pdfDict(Object.fromEntries(entries))
|
|
635
|
+
});
|
|
636
|
+
const extraNums = formExtraWidgetNums.get(field) ?? [];
|
|
637
|
+
for (const [index, num] of extraNums.entries()) {
|
|
638
|
+
const widget = field.widgets[index + 1];
|
|
639
|
+
objects.push({
|
|
640
|
+
num,
|
|
641
|
+
value: widgetDict(widget)
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
emitFormFieldObjects(field.children, field.name);
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
emitFormFieldObjects(doc.form ?? [], "");
|
|
648
|
+
if (structRootNum !== void 0 && structParentTreeNum !== void 0) {
|
|
649
|
+
const emitStructureElement = (element, parentNum) => {
|
|
650
|
+
const entries = [
|
|
651
|
+
["Type", require_objects.pdfName("StructElem")],
|
|
652
|
+
["S", require_objects.pdfName(element.type)],
|
|
653
|
+
["P", require_objects.pdfRef(parentNum, 0)]
|
|
654
|
+
];
|
|
655
|
+
if (element.title !== void 0) entries.push(["T", require_objects.pdfLiteralString(new TextEncoder().encode(element.title))]);
|
|
656
|
+
if (element.language !== void 0) entries.push(["Lang", require_objects.pdfLiteralString(new TextEncoder().encode(element.language))]);
|
|
657
|
+
if (element.alt !== void 0) entries.push(["Alt", require_objects.pdfLiteralString(new TextEncoder().encode(element.alt))]);
|
|
658
|
+
if (element.actualText !== void 0) entries.push(["ActualText", require_objects.pdfLiteralString(new TextEncoder().encode(element.actualText))]);
|
|
659
|
+
if (element.children.length > 0) entries.push(["K", require_objects.pdfArray(element.children.map((child) => {
|
|
660
|
+
const num = structElementNumById.get(child.id);
|
|
661
|
+
if (num === void 0) throw new Error(`structure element "${child.id}" was not allocated -- this is a writePdf internal invariant violation`);
|
|
662
|
+
return require_objects.pdfRef(num, 0);
|
|
663
|
+
}))]);
|
|
664
|
+
const ownNum = structElementNumById.get(element.id);
|
|
665
|
+
if (ownNum === void 0) throw new Error(`structure element "${element.id}" was not allocated -- this is a writePdf internal invariant violation`);
|
|
666
|
+
objects.push({
|
|
667
|
+
num: ownNum,
|
|
668
|
+
value: require_objects.pdfDict(Object.fromEntries(entries))
|
|
669
|
+
});
|
|
670
|
+
for (const child of element.children) emitStructureElement(child, ownNum);
|
|
671
|
+
};
|
|
672
|
+
for (const element of doc.structure ?? []) emitStructureElement(element, structRootNum);
|
|
673
|
+
}
|
|
674
|
+
if (residueXmpNum !== void 0 && doc.source?.xmp !== void 0) objects.push({
|
|
675
|
+
num: residueXmpNum,
|
|
676
|
+
value: require_objects.pdfStream(require_objects.pdfDict({
|
|
677
|
+
Type: require_objects.pdfName("Metadata"),
|
|
678
|
+
Subtype: require_objects.pdfName("XML")
|
|
679
|
+
}), new TextEncoder().encode(doc.source.xmp.xml))
|
|
680
|
+
});
|
|
295
681
|
for (const [standardName, alloc] of fontAllocs) {
|
|
296
682
|
const { font, descriptor } = buildFontObjects(standardName, require_objects.pdfRef(alloc.descNum, 0));
|
|
297
683
|
objects.push({
|
|
@@ -347,9 +733,10 @@ function writePdf(doc, options = {}) {
|
|
|
347
733
|
});
|
|
348
734
|
}
|
|
349
735
|
for (const [face, alloc] of embeddedAllocs) {
|
|
350
|
-
const
|
|
736
|
+
const usedGlyphs = require_embedded_font.collectEmbeddedGlyphs(alloc.texts, face);
|
|
737
|
+
const subset = require_sfnt_subset.subsetSfnt(face.font, [...alloc.codePoints].sort((a, b) => a - b), [...usedGlyphs.keys()].sort((a, b) => a - b));
|
|
351
738
|
if (subset === void 0) throw new Error(`font "${face.postScriptName}" resolved to an embeddable face, but its glyph outlines could not be subsetted -- only TrueType-outline ('glyf') fonts can be embedded, so supply a TrueType face for this family or drop it from the registry`);
|
|
352
|
-
const built = require_embedded_font_write.buildEmbeddedFontObjects(face, subset,
|
|
739
|
+
const built = require_embedded_font_write.buildEmbeddedFontObjects(face, subset, usedGlyphs, {
|
|
353
740
|
cidFontRef: require_objects.pdfRef(alloc.cidFontNum, 0),
|
|
354
741
|
descriptorRef: require_objects.pdfRef(alloc.descriptorNum, 0),
|
|
355
742
|
fontFileRef: require_objects.pdfRef(alloc.fontFileNum, 0),
|
|
@@ -385,6 +772,7 @@ function writePdf(doc, options = {}) {
|
|
|
385
772
|
}
|
|
386
773
|
if (imageAllocs.size > 0) resourceEntries.set("XObject", require_objects.pdfDict(new Map([...imageAllocs.values()].map((alloc) => [alloc.resourceName, require_objects.pdfRef(alloc.imageNum, 0)]))));
|
|
387
774
|
const resourcesDict = require_objects.pdfDict(resourceEntries);
|
|
775
|
+
const markedStructureByPage = /* @__PURE__ */ new Map();
|
|
388
776
|
const formulasByPage = /* @__PURE__ */ new Map();
|
|
389
777
|
for (const formula of formulas) {
|
|
390
778
|
const forPage = formulasByPage.get(formula.pageIndex);
|
|
@@ -421,7 +809,14 @@ function writePdf(doc, options = {}) {
|
|
|
421
809
|
doc.pages.forEach((page, pageIndex) => {
|
|
422
810
|
require_util_abort.throwIfAborted(options.signal);
|
|
423
811
|
const { pageNum, contentsNum } = pageAllocs[pageIndex];
|
|
424
|
-
|
|
812
|
+
let pageMcid = 0;
|
|
813
|
+
const pageContext = {
|
|
814
|
+
...context,
|
|
815
|
+
nextMcid: () => pageMcid++,
|
|
816
|
+
layerObjectNumberOf: (name) => layerNumByName.get(name)
|
|
817
|
+
};
|
|
818
|
+
const { bytes: contentBytes, substitutions, missingGlyphs, markedStructure } = require_content_write.writeContentStream(page.items, pageContext);
|
|
819
|
+
if (markedStructure.length > 0) markedStructureByPage.set(pageIndex, [...markedStructure]);
|
|
425
820
|
for (const substitution of substitutions) options.onSubstitution?.(substitution, { pageIndex });
|
|
426
821
|
for (const missing of missingGlyphs) options.onMissingGlyph?.(missing, { pageIndex });
|
|
427
822
|
const pageFormulas = formulasByPage.get(pageIndex);
|
|
@@ -436,7 +831,7 @@ function writePdf(doc, options = {}) {
|
|
|
436
831
|
num: contentsNum,
|
|
437
832
|
value: require_objects.pdfStream(contentsDict, finalContentBytes)
|
|
438
833
|
});
|
|
439
|
-
const annots = page.items.filter(isLinkItem).map((link) => buildLinkAnnotDict(link));
|
|
834
|
+
const annots = [...page.items.filter(isLinkItem).map((link) => buildLinkAnnotDict(link)), ...page.items.filter(isInternalLinkItem).map((link) => buildInternalLinkAnnotDict(link, doc, pageAllocs))];
|
|
440
835
|
if (page.notes !== void 0 && page.notes.length > 0) annots.push(buildNotesAnnotDict(page.notes));
|
|
441
836
|
const pageEntries = /* @__PURE__ */ new Map([
|
|
442
837
|
["Type", require_objects.pdfName("Page")],
|
|
@@ -451,15 +846,59 @@ function writePdf(doc, options = {}) {
|
|
|
451
846
|
["Contents", require_objects.pdfRef(contentsNum, 0)]
|
|
452
847
|
]);
|
|
453
848
|
if (annots.length > 0) pageEntries.set("Annots", require_objects.pdfArray(annots));
|
|
849
|
+
if ((markedStructureByPage.get(pageIndex) ?? []).length > 0) pageEntries.set("StructParents", require_objects.pdfNum(pageIndex));
|
|
454
850
|
objects.push({
|
|
455
851
|
num: pageNum,
|
|
456
852
|
value: require_objects.pdfDict(pageEntries)
|
|
457
853
|
});
|
|
458
854
|
});
|
|
855
|
+
if (structRootNum !== void 0 && structParentTreeNum !== void 0) {
|
|
856
|
+
const nums = [];
|
|
857
|
+
for (const [pageIndex, marks] of markedStructureByPage) {
|
|
858
|
+
const maxMcid = Math.max(...marks.map((mark) => mark.mcid));
|
|
859
|
+
const byMcid = Array.from({ length: maxMcid + 1 }, () => require_objects.pdfNull());
|
|
860
|
+
for (const mark of marks) {
|
|
861
|
+
const elementNum = structElementNumById.get(mark.structureId);
|
|
862
|
+
if (elementNum !== void 0) byMcid[mark.mcid] = require_objects.pdfRef(elementNum, 0);
|
|
863
|
+
}
|
|
864
|
+
nums.push(require_objects.pdfNum(pageIndex), require_objects.pdfArray(byMcid));
|
|
865
|
+
}
|
|
866
|
+
objects.push({
|
|
867
|
+
num: structParentTreeNum,
|
|
868
|
+
value: require_objects.pdfDict({ Nums: require_objects.pdfArray(nums) })
|
|
869
|
+
});
|
|
870
|
+
const rootEntries = [
|
|
871
|
+
["Type", require_objects.pdfName("StructTreeRoot")],
|
|
872
|
+
["K", require_objects.pdfArray((doc.structure ?? []).map((element) => {
|
|
873
|
+
const num = structElementNumById.get(element.id);
|
|
874
|
+
if (num === void 0) throw new Error(`structure element "${element.id}" was not allocated -- this is a writePdf internal invariant violation`);
|
|
875
|
+
return require_objects.pdfRef(num, 0);
|
|
876
|
+
}))],
|
|
877
|
+
["ParentTree", require_objects.pdfRef(structParentTreeNum, 0)]
|
|
878
|
+
];
|
|
879
|
+
objects.push({
|
|
880
|
+
num: structRootNum,
|
|
881
|
+
value: require_objects.pdfDict(Object.fromEntries(rootEntries))
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
let fileId;
|
|
885
|
+
let encryptedObjects = objects;
|
|
886
|
+
if (options.encryption !== void 0 && encryptDictNum !== void 0) {
|
|
887
|
+
fileId = require_crypto_random.randomBytes(FILE_ID_BYTES);
|
|
888
|
+
const encryptor = require_encrypt_write.createStandardEncryptor(options.encryption, fileId);
|
|
889
|
+
encryptedObjects = objects.map(({ num, value }) => ({
|
|
890
|
+
num,
|
|
891
|
+
value: require_encrypt_write.encryptIndirectObject(value, num, 0, encryptor)
|
|
892
|
+
}));
|
|
893
|
+
encryptedObjects.push({
|
|
894
|
+
num: encryptDictNum,
|
|
895
|
+
value: encryptor.encryptDict
|
|
896
|
+
});
|
|
897
|
+
}
|
|
459
898
|
const writer = new require_bytes_writer.ByteWriter();
|
|
460
899
|
writer.writeAscii("%PDF-1.7\n");
|
|
461
900
|
const offsets = /* @__PURE__ */ new Map();
|
|
462
|
-
for (const { num, value } of
|
|
901
|
+
for (const { num, value } of encryptedObjects) {
|
|
463
902
|
offsets.set(num, writer.length);
|
|
464
903
|
writer.writeAscii(`${num} 0 obj\n`);
|
|
465
904
|
require_serialize.writeObject(writer, value);
|
|
@@ -475,17 +914,21 @@ function writePdf(doc, options = {}) {
|
|
|
475
914
|
if (offset === void 0) throw new Error(`object ${num} was allocated but never written -- this is a writePdf internal invariant violation`);
|
|
476
915
|
writer.writeAscii(xrefEntry(offset, 0, true));
|
|
477
916
|
}
|
|
917
|
+
const trailerEntries = /* @__PURE__ */ new Map([
|
|
918
|
+
["Size", require_objects.pdfNum(maxObjNum + 1)],
|
|
919
|
+
["Root", require_objects.pdfRef(catalogNum, 0)],
|
|
920
|
+
["Info", require_objects.pdfRef(infoNum, 0)]
|
|
921
|
+
]);
|
|
922
|
+
if (fileId !== void 0 && encryptDictNum !== void 0) {
|
|
923
|
+
trailerEntries.set("ID", require_objects.pdfArray([require_objects.pdfHexString(fileId), require_objects.pdfHexString(fileId)]));
|
|
924
|
+
trailerEntries.set("Encrypt", require_objects.pdfRef(encryptDictNum, 0));
|
|
925
|
+
} else if (trailerIdRestore !== void 0) trailerEntries.set("ID", trailerIdRestore);
|
|
478
926
|
writer.writeAscii("trailer\n");
|
|
479
|
-
require_serialize.writeObject(writer, require_objects.pdfDict(
|
|
480
|
-
Size: require_objects.pdfNum(maxObjNum + 1),
|
|
481
|
-
Root: require_objects.pdfRef(catalogNum, 0),
|
|
482
|
-
Info: require_objects.pdfRef(infoNum, 0)
|
|
483
|
-
}));
|
|
927
|
+
require_serialize.writeObject(writer, require_objects.pdfDict(trailerEntries));
|
|
484
928
|
writer.writeAscii("\nstartxref\n");
|
|
485
929
|
writer.writeAscii(`${xrefOffset}\n`);
|
|
486
930
|
writer.writeAscii("%%EOF");
|
|
487
931
|
return writer.toBytes();
|
|
488
932
|
}
|
|
489
933
|
//#endregion
|
|
490
|
-
exports.NOTES_ANNOTATION_AUTHOR = NOTES_ANNOTATION_AUTHOR;
|
|
491
934
|
exports.writePdf = writePdf;
|
package/dist/write.d.cts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { LayoutDocument } from "./layout.cjs";
|
|
2
|
-
import { r as EmbeddedFaceSubstitution } from "./embedded-font-
|
|
2
|
+
import { r as EmbeddedFaceSubstitution } from "./embedded-font-oc65VGlr.cjs";
|
|
3
3
|
import { WinAnsiSubstitution } from "./winansi.cjs";
|
|
4
|
+
import { PdfEncryptionOptions } from "./encrypt-write.cjs";
|
|
4
5
|
import { FontRegistry } from "./font-registry.cjs";
|
|
5
6
|
import { PositionedFormula } from "document-schema.js";
|
|
6
7
|
//#region src/write.d.ts
|
|
@@ -15,8 +16,8 @@ interface WritePdfOptions {
|
|
|
15
16
|
}) => void;
|
|
16
17
|
readonly fonts?: FontRegistry;
|
|
17
18
|
readonly formulas?: readonly PositionedFormula[];
|
|
19
|
+
readonly encryption?: PdfEncryptionOptions;
|
|
18
20
|
}
|
|
19
|
-
declare const NOTES_ANNOTATION_AUTHOR = "documents.js:notes";
|
|
20
21
|
declare function writePdf(doc: LayoutDocument, options?: WritePdfOptions): Uint8Array<ArrayBuffer>;
|
|
21
22
|
//#endregion
|
|
22
|
-
export {
|
|
23
|
+
export { WritePdfOptions, writePdf };
|
package/dist/write.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { LayoutDocument } from "./layout.js";
|
|
2
|
-
import { r as EmbeddedFaceSubstitution } from "./embedded-font-
|
|
2
|
+
import { r as EmbeddedFaceSubstitution } from "./embedded-font-B4p9a3O3.js";
|
|
3
3
|
import { WinAnsiSubstitution } from "./winansi.js";
|
|
4
|
+
import { PdfEncryptionOptions } from "./encrypt-write.js";
|
|
4
5
|
import { FontRegistry } from "./font-registry.js";
|
|
5
6
|
import { PositionedFormula } from "document-schema.js";
|
|
6
7
|
//#region src/write.d.ts
|
|
@@ -15,8 +16,8 @@ interface WritePdfOptions {
|
|
|
15
16
|
}) => void;
|
|
16
17
|
readonly fonts?: FontRegistry;
|
|
17
18
|
readonly formulas?: readonly PositionedFormula[];
|
|
19
|
+
readonly encryption?: PdfEncryptionOptions;
|
|
18
20
|
}
|
|
19
|
-
declare const NOTES_ANNOTATION_AUTHOR = "documents.js:notes";
|
|
20
21
|
declare function writePdf(doc: LayoutDocument, options?: WritePdfOptions): Uint8Array<ArrayBuffer>;
|
|
21
22
|
//#endregion
|
|
22
|
-
export {
|
|
23
|
+
export { WritePdfOptions, writePdf };
|