pdf-codec 4.0.13 → 4.1.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/README.md +6 -5
- package/dist/annotations.d.cts +1 -1
- package/dist/annotations.d.ts +1 -1
- package/dist/attachments.d.cts +1 -1
- package/dist/attachments.d.ts +1 -1
- package/dist/content-read.d.cts +1 -1
- package/dist/content-read.d.ts +1 -1
- 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/document.d.cts +1 -1
- package/dist/document.d.ts +1 -1
- package/dist/embedded-font-write.d.cts +1 -1
- package/dist/embedded-font-write.d.ts +1 -1
- 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 +50 -20
- package/dist/encrypt.d.cts +18 -2
- package/dist/encrypt.d.ts +18 -2
- package/dist/encrypt.js +35 -21
- package/dist/filters.d.cts +1 -1
- package/dist/filters.d.ts +1 -1
- package/dist/font-read.d.cts +1 -1
- package/dist/font-read.d.ts +1 -1
- package/dist/form.d.cts +1 -1
- package/dist/form.d.ts +1 -1
- package/dist/images-read.d.cts +1 -1
- package/dist/images-read.d.ts +1 -1
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/interpret.d.cts +1 -1
- package/dist/interpret.d.ts +1 -1
- package/dist/math-font-write.d.cts +1 -1
- package/dist/math-font-write.d.ts +1 -1
- package/dist/names.d.cts +1 -1
- package/dist/names.d.ts +1 -1
- package/dist/navigation.d.cts +1 -1
- package/dist/navigation.d.ts +1 -1
- 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.d.cts +1 -1
- package/dist/optional-content.d.ts +1 -1
- package/dist/parse.d.cts +1 -1
- package/dist/parse.d.ts +1 -1
- package/dist/predictors.d.cts +1 -1
- package/dist/predictors.d.ts +1 -1
- package/dist/serialize.d.cts +1 -1
- package/dist/serialize.d.ts +1 -1
- package/dist/structure.d.cts +1 -1
- package/dist/structure.d.ts +1 -1
- package/dist/tounicode.d.cts +1 -1
- package/dist/tounicode.d.ts +1 -1
- package/dist/write.cjs +29 -6
- package/dist/write.d.cts +2 -0
- package/dist/write.d.ts +2 -0
- package/dist/write.js +29 -6
- package/dist/xref.d.cts +1 -1
- package/dist/xref.d.ts +1 -1
- package/package.json +1 -1
package/dist/write.js
CHANGED
|
@@ -6,6 +6,8 @@ import { writeObject } from "./serialize.js";
|
|
|
6
6
|
import { deflate } from "./bytes/flate.js";
|
|
7
7
|
import { base64ToBytes } from "./util/base64.js";
|
|
8
8
|
import { throwIfAborted } from "./util/abort.js";
|
|
9
|
+
import { randomBytes } from "./crypto/random.js";
|
|
10
|
+
import { createStandardEncryptor, encryptIndirectObject } from "./encrypt-write.js";
|
|
9
11
|
import { readJpegInfo } from "./image/jpeg-info.js";
|
|
10
12
|
import { decodePng } from "./image/png-decode.js";
|
|
11
13
|
import { collectEmbeddedGlyphs } from "./embedded-font.js";
|
|
@@ -29,6 +31,7 @@ const FLAG_SERIF = 2;
|
|
|
29
31
|
const FLAG_NONSYMBOLIC = 32;
|
|
30
32
|
const FLAG_ITALIC = 64;
|
|
31
33
|
const FLAG_FORCE_BOLD = 262144;
|
|
34
|
+
const FILE_ID_BYTES = 16;
|
|
32
35
|
function textToPdfString(text) {
|
|
33
36
|
const bytes = new Uint8Array(2 + text.length * 2);
|
|
34
37
|
bytes[0] = 254;
|
|
@@ -317,6 +320,7 @@ function writePdf(doc, options = {}) {
|
|
|
317
320
|
pageNum: nextObjNum++,
|
|
318
321
|
contentsNum: nextObjNum++
|
|
319
322
|
}));
|
|
323
|
+
const encryptDictNum = options.encryption === void 0 ? void 0 : nextObjNum++;
|
|
320
324
|
const objects = [];
|
|
321
325
|
objects.push({
|
|
322
326
|
num: catalogNum,
|
|
@@ -501,10 +505,24 @@ function writePdf(doc, options = {}) {
|
|
|
501
505
|
value: pdfDict(pageEntries)
|
|
502
506
|
});
|
|
503
507
|
});
|
|
508
|
+
let fileId;
|
|
509
|
+
let encryptedObjects = objects;
|
|
510
|
+
if (options.encryption !== void 0 && encryptDictNum !== void 0) {
|
|
511
|
+
fileId = randomBytes(FILE_ID_BYTES);
|
|
512
|
+
const encryptor = createStandardEncryptor(options.encryption, fileId);
|
|
513
|
+
encryptedObjects = objects.map(({ num, value }) => ({
|
|
514
|
+
num,
|
|
515
|
+
value: encryptIndirectObject(value, num, 0, encryptor)
|
|
516
|
+
}));
|
|
517
|
+
encryptedObjects.push({
|
|
518
|
+
num: encryptDictNum,
|
|
519
|
+
value: encryptor.encryptDict
|
|
520
|
+
});
|
|
521
|
+
}
|
|
504
522
|
const writer = new ByteWriter();
|
|
505
523
|
writer.writeAscii("%PDF-1.7\n");
|
|
506
524
|
const offsets = /* @__PURE__ */ new Map();
|
|
507
|
-
for (const { num, value } of
|
|
525
|
+
for (const { num, value } of encryptedObjects) {
|
|
508
526
|
offsets.set(num, writer.length);
|
|
509
527
|
writer.writeAscii(`${num} 0 obj\n`);
|
|
510
528
|
writeObject(writer, value);
|
|
@@ -520,12 +538,17 @@ function writePdf(doc, options = {}) {
|
|
|
520
538
|
if (offset === void 0) throw new Error(`object ${num} was allocated but never written -- this is a writePdf internal invariant violation`);
|
|
521
539
|
writer.writeAscii(xrefEntry(offset, 0, true));
|
|
522
540
|
}
|
|
541
|
+
const trailerEntries = /* @__PURE__ */ new Map([
|
|
542
|
+
["Size", pdfNum(maxObjNum + 1)],
|
|
543
|
+
["Root", pdfRef(catalogNum, 0)],
|
|
544
|
+
["Info", pdfRef(infoNum, 0)]
|
|
545
|
+
]);
|
|
546
|
+
if (fileId !== void 0 && encryptDictNum !== void 0) {
|
|
547
|
+
trailerEntries.set("ID", pdfArray([pdfHexString(fileId), pdfHexString(fileId)]));
|
|
548
|
+
trailerEntries.set("Encrypt", pdfRef(encryptDictNum, 0));
|
|
549
|
+
}
|
|
523
550
|
writer.writeAscii("trailer\n");
|
|
524
|
-
writeObject(writer, pdfDict(
|
|
525
|
-
Size: pdfNum(maxObjNum + 1),
|
|
526
|
-
Root: pdfRef(catalogNum, 0),
|
|
527
|
-
Info: pdfRef(infoNum, 0)
|
|
528
|
-
}));
|
|
551
|
+
writeObject(writer, pdfDict(trailerEntries));
|
|
529
552
|
writer.writeAscii("\nstartxref\n");
|
|
530
553
|
writer.writeAscii(`${xrefOffset}\n`);
|
|
531
554
|
writer.writeAscii("%%EOF");
|
package/dist/xref.d.cts
CHANGED
package/dist/xref.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pdf-codec",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "Hand-written, dependency-minimal PDF codec: parses arbitrary real-world PDFs and generates new ones, built on its own codec-owned LayoutDocument item model and Zod 4 codecs.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|