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.
Files changed (66) hide show
  1. package/README.md +6 -5
  2. package/dist/annotations.d.cts +1 -1
  3. package/dist/annotations.d.ts +1 -1
  4. package/dist/attachments.d.cts +1 -1
  5. package/dist/attachments.d.ts +1 -1
  6. package/dist/content-read.d.cts +1 -1
  7. package/dist/content-read.d.ts +1 -1
  8. package/dist/crypto/random.cjs +9 -0
  9. package/dist/crypto/random.d.cts +4 -0
  10. package/dist/crypto/random.d.ts +4 -0
  11. package/dist/crypto/random.js +8 -0
  12. package/dist/document.d.cts +1 -1
  13. package/dist/document.d.ts +1 -1
  14. package/dist/embedded-font-write.d.cts +1 -1
  15. package/dist/embedded-font-write.d.ts +1 -1
  16. package/dist/encrypt-write.cjs +234 -0
  17. package/dist/encrypt-write.d.cts +31 -0
  18. package/dist/encrypt-write.d.ts +31 -0
  19. package/dist/encrypt-write.js +231 -0
  20. package/dist/encrypt.cjs +50 -20
  21. package/dist/encrypt.d.cts +18 -2
  22. package/dist/encrypt.d.ts +18 -2
  23. package/dist/encrypt.js +35 -21
  24. package/dist/filters.d.cts +1 -1
  25. package/dist/filters.d.ts +1 -1
  26. package/dist/font-read.d.cts +1 -1
  27. package/dist/font-read.d.ts +1 -1
  28. package/dist/form.d.cts +1 -1
  29. package/dist/form.d.ts +1 -1
  30. package/dist/images-read.d.cts +1 -1
  31. package/dist/images-read.d.ts +1 -1
  32. package/dist/index.cjs +2 -0
  33. package/dist/index.d.cts +2 -1
  34. package/dist/index.d.ts +2 -1
  35. package/dist/index.js +2 -1
  36. package/dist/interpret.d.cts +1 -1
  37. package/dist/interpret.d.ts +1 -1
  38. package/dist/math-font-write.d.cts +1 -1
  39. package/dist/math-font-write.d.ts +1 -1
  40. package/dist/names.d.cts +1 -1
  41. package/dist/names.d.ts +1 -1
  42. package/dist/navigation.d.cts +1 -1
  43. package/dist/navigation.d.ts +1 -1
  44. package/dist/objects-B7HdMeWS.d.cts +59 -0
  45. package/dist/objects-B7HdMeWS.d.ts +59 -0
  46. package/dist/objects.d.cts +1 -58
  47. package/dist/objects.d.ts +1 -58
  48. package/dist/optional-content.d.cts +1 -1
  49. package/dist/optional-content.d.ts +1 -1
  50. package/dist/parse.d.cts +1 -1
  51. package/dist/parse.d.ts +1 -1
  52. package/dist/predictors.d.cts +1 -1
  53. package/dist/predictors.d.ts +1 -1
  54. package/dist/serialize.d.cts +1 -1
  55. package/dist/serialize.d.ts +1 -1
  56. package/dist/structure.d.cts +1 -1
  57. package/dist/structure.d.ts +1 -1
  58. package/dist/tounicode.d.cts +1 -1
  59. package/dist/tounicode.d.ts +1 -1
  60. package/dist/write.cjs +29 -6
  61. package/dist/write.d.cts +2 -0
  62. package/dist/write.d.ts +2 -0
  63. package/dist/write.js +29 -6
  64. package/dist/xref.d.cts +1 -1
  65. package/dist/xref.d.ts +1 -1
  66. 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 objects) {
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
@@ -1,5 +1,5 @@
1
1
  import { PdfDiagnosticSink } from "./diagnostics.cjs";
2
- import { PdfDict } from "./objects.cjs";
2
+ import { n as PdfDict } from "./objects-B7HdMeWS.cjs";
3
3
  //#region src/xref.d.ts
4
4
  interface XrefOffsetEntry {
5
5
  readonly type: "offset";
package/dist/xref.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { PdfDiagnosticSink } from "./diagnostics.js";
2
- import { PdfDict } from "./objects.js";
2
+ import { n as PdfDict } from "./objects-B7HdMeWS.js";
3
3
  //#region src/xref.d.ts
4
4
  interface XrefOffsetEntry {
5
5
  readonly type: "offset";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdf-codec",
3
- "version": "4.0.13",
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": {