-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathpdf-writer.ts
More file actions
665 lines (559 loc) · 17.6 KB
/
pdf-writer.ts
File metadata and controls
665 lines (559 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/**
* PDF file writer.
*
* Supports both full save (rewrite everything) and incremental save
* (append only changed objects).
*
* Uses a single ByteWriter for the entire PDF to minimize allocations.
*/
import { clearAllDirtyFlags, collectChanges } from "#src/document/change-collector";
import type { ObjectRegistry } from "#src/document/object-registry";
import { FilterPipeline } from "#src/filters/filter-pipeline";
import { CR, LF } from "#src/helpers/chars";
import { ByteWriter } from "#src/io/byte-writer";
import { PdfArray } from "#src/objects/pdf-array";
import { PdfDict } from "#src/objects/pdf-dict";
import { PdfName } from "#src/objects/pdf-name";
import type { PdfObject } from "#src/objects/pdf-object";
import { PdfRef } from "#src/objects/pdf-ref";
import { PdfStream } from "#src/objects/pdf-stream";
import { PdfString } from "#src/objects/pdf-string";
import type { StandardSecurityHandler } from "#src/security/standard-handler";
import { writeXRefStream, writeXRefTable, type XRefWriteEntry } from "./xref-writer";
/**
* Options for PDF writing.
*/
export interface WriteOptions {
/** PDF version string (default: "1.7") */
version?: string;
/** Root catalog reference */
root: PdfRef;
/** Info dictionary reference (optional) */
info?: PdfRef;
/** Encrypt dictionary reference (optional) */
encrypt?: PdfRef;
/** Document ID (optional, two 16-byte arrays) */
id?: [Uint8Array, Uint8Array];
/** Use XRef stream instead of table (PDF 1.5+) */
useXRefStream?: boolean;
/**
* Compress uncompressed streams with FlateDecode (default: true).
*
* When enabled, streams without a /Filter entry will be compressed
* before writing. Streams that already have filters (including image
* formats like DCTDecode/JPXDecode) are left unchanged.
*/
compressStreams?: boolean;
/**
* Minimum stream size in bytes to attempt compression (default: 512).
*
* Streams smaller than this threshold are written uncompressed.
* Deflate initialization has a fixed cost (~0.023ms for pako's 64KB
* hash table) that dominates for small payloads, and tiny streams
* rarely achieve meaningful compression.
*
* Set to 0 to compress all streams regardless of size.
*/
compressionThreshold?: number;
/**
* Security handler for encrypting content.
*
* When provided, strings and streams will be encrypted before writing.
* The encrypt dictionary reference must also be provided.
*/
securityHandler?: StandardSecurityHandler;
/**
* Hint for the final PDF size in bytes.
*
* When provided, the ByteWriter will pre-allocate a buffer of this size,
* reducing the need for reallocations during writing.
*/
sizeHint?: number;
}
/**
* Options for incremental save.
*/
export interface IncrementalWriteOptions extends WriteOptions {
/** Original PDF bytes */
originalBytes: Uint8Array;
/** Offset of the original xref */
originalXRefOffset: number;
}
/**
* Result of a write operation.
*/
export interface WriteResult {
/** The written PDF bytes */
bytes: Uint8Array;
/** Byte offset where the xref section starts */
xrefOffset: number;
}
/**
* Write an indirect object to the ByteWriter.
*
* Format: "N G obj\n[object]\nendobj\n"
*/
function writeIndirectObject(writer: ByteWriter, ref: PdfRef, obj: PdfObject): void {
writer.writeAscii(`${ref.objectNumber} ${ref.generation} obj\n`);
obj.toBytes(writer);
writer.writeAscii("\nendobj\n");
}
/**
* Prepare an object for writing, applying compression if needed.
*
* For PdfStream objects without a /Filter entry, compresses the data
* with FlateDecode and returns a new stream with the compressed data.
* The original stream is not modified.
*
* Streams that already have filters are returned unchanged - this includes
* image formats (DCTDecode, JPXDecode, etc.) that are already compressed.
*/
const DEFAULT_COMPRESSION_THRESHOLD = 512;
function prepareObjectForWrite(
obj: PdfObject,
compress: boolean,
compressionThreshold: number,
): PdfObject {
// Only process streams
if (!(obj instanceof PdfStream)) {
return obj;
}
// Already has a filter - leave it alone
if (obj.has("Filter")) {
return obj;
}
// Compression disabled
if (!compress) {
return obj;
}
// Pako's deflate initialization zeros a 64KB hash table on every call
// (~0.023ms). For streams below the threshold the compression savings
// are negligible relative to the init cost, especially when writing
// many PDFs (e.g. splitting 2000 pages).
if (obj.data.length < compressionThreshold) {
return obj;
}
// Compress with FlateDecode
const compressed = FilterPipeline.encode(obj.data, { name: "FlateDecode" });
// Only use compression if it actually reduces size
if (compressed.length >= obj.data.length) {
return obj;
}
// Create a new stream with compressed data
// Copy all existing entries from the original stream
const compressedStream = new PdfStream(obj, compressed);
compressedStream.set("Filter", PdfName.of("FlateDecode"));
return compressedStream;
}
/**
* Encryption context for writing.
*/
interface EncryptionContext {
handler: StandardSecurityHandler;
objectNumber: number;
generation: number;
}
/**
* Recursively encrypt an object for writing.
*
* Creates a new object tree with all strings and stream data encrypted.
* References, names, numbers, and booleans are returned unchanged.
*
* @param obj - The object to encrypt
* @param ctx - Encryption context with handler and object reference
* @returns Encrypted copy of the object
*/
function encryptObject(obj: PdfObject, ctx: EncryptionContext): PdfObject {
if (obj instanceof PdfString) {
// Encrypt string bytes
const encrypted = ctx.handler.encryptString(obj.bytes, ctx.objectNumber, ctx.generation);
// Always use hex format for encrypted strings (cleaner, no escaping issues)
return new PdfString(encrypted, "hex");
}
if (obj instanceof PdfStream) {
// Check if this stream type should be encrypted
const streamType = obj.getName("Type")?.value;
if (!ctx.handler.shouldEncryptStream(streamType)) {
// Recursively encrypt dictionary entries but not stream data
return encryptStreamDict(obj, ctx);
}
// Encrypt stream data
const encryptedData = ctx.handler.encryptStream(obj.data, ctx.objectNumber, ctx.generation);
// Create new stream with encrypted data and encrypted dict entries
const encryptedStream = new PdfStream([], encryptedData);
// Copy and encrypt dictionary entries
for (const [key, value] of obj) {
if (key.value === "Length") {
continue; // Skip Length, will be recalculated
}
encryptedStream.set(key.value, encryptObject(value, ctx));
}
return encryptedStream;
}
if (obj instanceof PdfDict) {
// Recursively encrypt dictionary values
const encrypted = new PdfDict();
for (const [key, value] of obj) {
encrypted.set(key.value, encryptObject(value, ctx));
}
return encrypted;
}
if (obj instanceof PdfArray) {
// Recursively encrypt array elements
const encrypted = new PdfArray();
for (const item of obj) {
encrypted.push(encryptObject(item, ctx));
}
return encrypted;
}
// Other types (PdfRef, PdfName, PdfNumber, PdfBool, PdfNull) pass through unchanged
return obj;
}
/**
* Encrypt dictionary entries of a stream without encrypting stream data.
* Used for streams that should not be encrypted (XRef, unencrypted metadata).
*/
function encryptStreamDict(stream: PdfStream, ctx: EncryptionContext): PdfStream {
const encrypted = new PdfStream([], stream.data);
for (const [key, value] of stream) {
if (key.value === "Length") {
continue;
}
encrypted.set(key.value, encryptObject(value, ctx));
}
return encrypted;
}
/**
* Collect all refs reachable from the document root and trailer entries.
*
* Walks the object graph starting from Root, Info, and Encrypt (if present),
* returning the set of all object keys (as "objNum gen" strings) that are reachable.
* This is used for garbage collection during full saves.
*/
function collectReachableRefs(
registry: ObjectRegistry,
root: PdfRef,
info?: PdfRef,
encrypt?: PdfRef,
): Set<string> {
const visited = new Set<string>();
const stack: PdfObject[] = [root];
if (info) {
stack.push(info);
}
if (encrypt) {
stack.push(encrypt);
}
while (stack.length > 0) {
const obj = stack.pop()!;
if (obj instanceof PdfRef) {
const key = `${obj.objectNumber} ${obj.generation}`;
if (visited.has(key)) {
continue;
}
visited.add(key);
const resolved = registry.resolve(obj);
if (resolved !== null) {
stack.push(resolved);
}
} else if (obj instanceof PdfDict) {
// PdfStream extends PdfDict, so this handles both
for (const [, value] of obj) {
if (value != null) {
stack.push(value);
}
}
} else if (obj instanceof PdfArray) {
for (const item of obj) {
stack.push(item);
}
}
}
return visited;
}
/**
* Write a complete PDF from scratch.
*
* Structure:
* ```
* %PDF-X.Y
* %[binary comment]
* 1 0 obj
* ...
* endobj
* 2 0 obj
* ...
* xref
* ...
* trailer
* ...
* startxref
* ...
* %%EOF
* ```
*/
export function writeComplete(registry: ObjectRegistry, options: WriteOptions): WriteResult {
const writer = new ByteWriter(undefined, {
initialSize: options.sizeHint,
});
const compress = options.compressStreams ?? true;
const threshold = options.compressionThreshold ?? DEFAULT_COMPRESSION_THRESHOLD;
// Version
const version = options.version ?? "1.7";
writer.writeAscii(`%PDF-${version}\n`);
// Binary comment (signals binary file to text tools)
// Use high-byte characters as recommended by PDF spec
writer.writeBytes(new Uint8Array([0x25, 0xe2, 0xe3, 0xcf, 0xd3, 0x0a])); // %âãÏÓ\n
// Track offsets for xref
const offsets = new Map<number, { offset: number; generation: number }>();
// Collect only reachable objects (garbage collection)
// This ensures orphan objects are not written to the output
const reachableKeys = collectReachableRefs(registry, options.root, options.info, options.encrypt);
// Write only reachable objects and record offsets
for (const [ref, obj] of registry.entries()) {
const key = `${ref.objectNumber} ${ref.generation}`;
if (!reachableKeys.has(key)) {
continue; // Skip orphan objects
}
// Prepare object (compress streams if needed)
let prepared = prepareObjectForWrite(obj, compress, threshold);
// Apply encryption if security handler is provided
// Skip encrypting the /Encrypt dictionary itself
if (options.securityHandler && options.encrypt && ref !== options.encrypt) {
prepared = encryptObject(prepared, {
handler: options.securityHandler,
objectNumber: ref.objectNumber,
generation: ref.generation,
});
}
offsets.set(ref.objectNumber, {
offset: writer.position,
generation: ref.generation,
});
writeIndirectObject(writer, ref, prepared);
}
// Record xref offset before writing it
const xrefOffset = writer.position;
// Build xref entries
const entries: XRefWriteEntry[] = [
// Object 0 is always the free list head
{ objectNumber: 0, generation: 65535, type: "free", offset: 0 },
];
for (const [objNum, info] of offsets) {
entries.push({
objectNumber: objNum,
generation: info.generation,
type: "inuse",
offset: info.offset,
});
}
// Write xref section
if (options.useXRefStream) {
const xrefObjNum = registry.nextObjectNumber;
// Add entry for the xref stream itself
entries.push({
objectNumber: xrefObjNum,
generation: 0,
type: "inuse",
offset: xrefOffset,
});
// Size is max object number + 1
const size = Math.max(0, ...entries.map(e => e.objectNumber)) + 1;
writeXRefStream(writer, {
entries,
size,
xrefOffset,
root: options.root,
info: options.info,
encrypt: options.encrypt,
id: options.id,
streamObjectNumber: xrefObjNum,
});
} else {
// Size is max object number + 1
const size = Math.max(0, ...entries.map(e => e.objectNumber)) + 1;
writeXRefTable(writer, {
entries,
size,
xrefOffset,
root: options.root,
info: options.info,
encrypt: options.encrypt,
id: options.id,
});
}
return {
bytes: writer.toBytes(),
xrefOffset,
};
}
/**
* Write an incremental update to a PDF.
*
* Appends only the changed/new objects to the original PDF bytes,
* preserving the original content exactly.
*
* Structure:
* ```
* [original PDF bytes]
* [modified object 1]
* [modified object 2]
* ...
* xref
* ...
* trailer
* << ... /Prev [originalXRefOffset] >>
* startxref
* ...
* %%EOF
* ```
*/
export function writeIncremental(
registry: ObjectRegistry,
options: IncrementalWriteOptions,
): WriteResult {
// Collect changes
const changes = collectChanges(registry);
// If no changes, return original (should caller handle this?)
if (changes.modified.size === 0 && changes.created.size === 0) {
return {
bytes: options.originalBytes,
xrefOffset: options.originalXRefOffset,
};
}
const compress = options.compressStreams ?? true;
const threshold = options.compressionThreshold ?? DEFAULT_COMPRESSION_THRESHOLD;
// Initialize ByteWriter with original bytes
const writer = new ByteWriter(options.originalBytes);
// Ensure there's a newline before appended content
const lastByte = options.originalBytes[options.originalBytes.length - 1];
if (lastByte !== LF && lastByte !== CR) {
writer.writeByte(0x0a); // newline
}
// Track offsets for new xref
const offsets = new Map<number, { offset: number; generation: number }>();
// Write modified objects
for (const [ref, obj] of changes.modified) {
let prepared = prepareObjectForWrite(obj, compress, threshold);
// Apply encryption if security handler is provided
// Skip encrypting the /Encrypt dictionary itself
if (options.securityHandler && options.encrypt && ref !== options.encrypt) {
prepared = encryptObject(prepared, {
handler: options.securityHandler,
objectNumber: ref.objectNumber,
generation: ref.generation,
});
}
offsets.set(ref.objectNumber, {
offset: writer.position,
generation: ref.generation,
});
writeIndirectObject(writer, ref, prepared);
}
// Write new objects
for (const [ref, obj] of changes.created) {
let prepared = prepareObjectForWrite(obj, compress, threshold);
// Apply encryption if security handler is provided
// Skip encrypting the /Encrypt dictionary itself
if (options.securityHandler && options.encrypt && ref !== options.encrypt) {
prepared = encryptObject(prepared, {
handler: options.securityHandler,
objectNumber: ref.objectNumber,
generation: ref.generation,
});
}
offsets.set(ref.objectNumber, {
offset: writer.position,
generation: ref.generation,
});
writeIndirectObject(writer, ref, prepared);
}
// Record xref offset
const xrefOffset = writer.position;
// Build xref entries (only for changed objects)
const entries: XRefWriteEntry[] = [];
for (const [objNum, info] of offsets) {
entries.push({
objectNumber: objNum,
generation: info.generation,
type: "inuse",
offset: info.offset,
});
}
// Write xref section with /Prev pointer
if (options.useXRefStream) {
const xrefObjNum = registry.nextObjectNumber;
// Add entry for the xref stream itself
entries.push({
objectNumber: xrefObjNum,
generation: 0,
type: "inuse",
offset: xrefOffset,
});
// Size must cover all objects (original + new), including xref stream
const size = Math.max(changes.maxObjectNumber + 1, xrefObjNum + 1);
writeXRefStream(writer, {
entries,
size,
xrefOffset,
prev: options.originalXRefOffset,
root: options.root,
info: options.info,
encrypt: options.encrypt,
id: options.id,
streamObjectNumber: xrefObjNum,
});
} else {
// Size must cover all objects (original + new)
const size = Math.max(changes.maxObjectNumber + 1, registry.nextObjectNumber);
writeXRefTable(writer, {
entries,
size,
xrefOffset,
prev: options.originalXRefOffset,
root: options.root,
info: options.info,
encrypt: options.encrypt,
id: options.id,
});
}
// Clear dirty flags and commit new objects
clearAllDirtyFlags(registry);
registry.commitNewObjects();
return {
bytes: writer.toBytes(),
xrefOffset,
};
}
/**
* Utility to check if incremental save produced valid output.
*
* Verifies that:
* 1. Original bytes are preserved exactly
* 2. New content starts after original
* 3. Basic structure is valid
*/
export function verifyIncrementalSave(
original: Uint8Array,
result: Uint8Array,
): { valid: boolean; error?: string } {
// Result must be at least as long as original
if (result.length < original.length) {
return { valid: false, error: "Result shorter than original" };
}
// Original bytes must be preserved exactly
for (let i = 0; i < original.length; i++) {
if (result[i] !== original[i]) {
return {
valid: false,
error: `Byte mismatch at offset ${i}: expected ${original[i]}, got ${result[i]}`,
};
}
}
// Check for %%EOF at end
const tail = new TextDecoder().decode(result.slice(-10));
if (!tail.includes("%%EOF")) {
return { valid: false, error: "Missing %%EOF at end" };
}
return { valid: true };
}