pdfnative 1.0.3 → 1.0.4

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/dist/index.js CHANGED
@@ -4621,9 +4621,26 @@ function buildStructureTree(root, startObjNum, pageObjToStructParents) {
4621
4621
  totalObjects: nextObj - startObjNum
4622
4622
  };
4623
4623
  }
4624
- function buildXMPMetadata(title, createDate, pdfaPart = 2, pdfaConformance = "B") {
4624
+ function buildPdfMetadata(now = /* @__PURE__ */ new Date()) {
4625
+ const pad2 = (n) => String(n).padStart(2, "0");
4626
+ const yyyy = now.getFullYear();
4627
+ const mm = pad2(now.getMonth() + 1);
4628
+ const dd = pad2(now.getDate());
4629
+ const hh = pad2(now.getHours());
4630
+ const mi = pad2(now.getMinutes());
4631
+ const ss = pad2(now.getSeconds());
4632
+ const tzMinutes = -now.getTimezoneOffset();
4633
+ const tzSign = tzMinutes >= 0 ? "+" : "-";
4634
+ const tzAbs = Math.abs(tzMinutes);
4635
+ const tzH = pad2(Math.floor(tzAbs / 60));
4636
+ const tzM = pad2(tzAbs % 60);
4637
+ const pdfDate = `D:${yyyy}${mm}${dd}${hh}${mi}${ss}${tzSign}${tzH}'${tzM}'`;
4638
+ const xmpDate = `${yyyy}-${mm}-${dd}T${hh}:${mi}:${ss}${tzSign}${tzH}:${tzM}`;
4639
+ return { pdfDate, xmpDate };
4640
+ }
4641
+ function buildXMPMetadata(title, createDate, pdfaPart = 2, pdfaConformance = "B", author) {
4625
4642
  const escapedTitle = escapeXml(title);
4626
- return [
4643
+ const lines = [
4627
4644
  '<?xpacket begin="\xEF\xBB\xBF" id="W5M0MpCehiHzreSzNTczkc9d"?>',
4628
4645
  '<x:xmpmeta xmlns:x="adobe:ns:meta/">',
4629
4646
  ' <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">',
@@ -4632,17 +4649,24 @@ function buildXMPMetadata(title, createDate, pdfaPart = 2, pdfaConformance = "B"
4632
4649
  ' xmlns:pdf="http://ns.adobe.com/pdf/1.3/"',
4633
4650
  ' xmlns:xmp="http://ns.adobe.com/xap/1.0/"',
4634
4651
  ' xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">',
4635
- ` <dc:title><rdf:Alt><rdf:li xml:lang="x-default">${escapedTitle}</rdf:li></rdf:Alt></dc:title>`,
4636
- " <dc:creator><rdf:Seq><rdf:li>pdfnative</rdf:li></rdf:Seq></dc:creator>",
4652
+ ` <dc:title><rdf:Alt><rdf:li xml:lang="x-default">${escapedTitle}</rdf:li></rdf:Alt></dc:title>`
4653
+ ];
4654
+ if (author !== void 0 && author !== "") {
4655
+ lines.push(` <dc:creator><rdf:Seq><rdf:li>${escapeXml(author)}</rdf:li></rdf:Seq></dc:creator>`);
4656
+ }
4657
+ lines.push(
4637
4658
  " <pdf:Producer>pdfnative</pdf:Producer>",
4638
4659
  ` <xmp:CreateDate>${createDate}</xmp:CreateDate>`,
4660
+ ` <xmp:ModifyDate>${createDate}</xmp:ModifyDate>`,
4661
+ ` <xmp:MetadataDate>${createDate}</xmp:MetadataDate>`,
4639
4662
  ` <pdfaid:part>${pdfaPart}</pdfaid:part>`,
4640
4663
  ` <pdfaid:conformance>${pdfaConformance}</pdfaid:conformance>`,
4641
4664
  " </rdf:Description>",
4642
4665
  " </rdf:RDF>",
4643
4666
  "</x:xmpmeta>",
4644
4667
  '<?xpacket end="w"?>'
4645
- ].join("\n");
4668
+ );
4669
+ return lines.join("\n");
4646
4670
  }
4647
4671
  function buildOutputIntentDict(iccStreamObjNum, subtype = "GTS_PDFA1") {
4648
4672
  return `<< /Type /OutputIntent /S /${subtype} /OutputConditionIdentifier (sRGB IEC61966-2.1) /RegistryName (http://www.color.org) /DestOutputProfile ${iccStreamObjNum} 0 R >>`;
@@ -5254,7 +5278,7 @@ endstream`);
5254
5278
  _offset += d;
5255
5279
  }, objOffsets, parts };
5256
5280
  }
5257
- function writeXrefTrailer(w, totalObjs, infoObjNum, encState) {
5281
+ function writeXrefTrailer(w, totalObjs, infoObjNum, encState, idSeed = "") {
5258
5282
  let encryptObjNum = 0;
5259
5283
  if (encState) {
5260
5284
  encryptObjNum = totalObjs + 1;
@@ -5272,11 +5296,13 @@ function writeXrefTrailer(w, totalObjs, infoObjNum, encState) {
5272
5296
  `);
5273
5297
  }
5274
5298
  w.emit("trailer\n");
5299
+ const docId = encState ? encState.docId : md5(new TextEncoder().encode(`pdfnative|${idSeed}|${totalObjs}`));
5300
+ const idArray = buildIdArray(docId);
5275
5301
  if (encState) {
5276
- w.emit(`<< /Size ${totalObjs + 1} /Root 1 0 R /Info ${infoObjNum} 0 R /Encrypt ${encryptObjNum} 0 R /ID ${buildIdArray(encState.docId)} >>
5302
+ w.emit(`<< /Size ${totalObjs + 1} /Root 1 0 R /Info ${infoObjNum} 0 R /Encrypt ${encryptObjNum} 0 R /ID ${idArray} >>
5277
5303
  `);
5278
5304
  } else {
5279
- w.emit(`<< /Size ${totalObjs + 1} /Root 1 0 R /Info ${infoObjNum} 0 R >>
5305
+ w.emit(`<< /Size ${totalObjs + 1} /Root 1 0 R /Info ${infoObjNum} 0 R /ID ${idArray} >>
5280
5306
  `);
5281
5307
  }
5282
5308
  w.emit("startxref\n");
@@ -6012,10 +6038,7 @@ function buildPDF(params, layoutOptions) {
6012
6038
  }
6013
6039
  const baseObjCount = enc.isUnicode ? 4 + fontEntries.length * 5 + wmExtraObjs + totalPages * 2 : 4 + wmExtraObjs + totalPages * 2;
6014
6040
  const infoObjNum = baseObjCount + 1;
6015
- const now = /* @__PURE__ */ new Date();
6016
- const pad2 = (n) => String(n).padStart(2, "0");
6017
- const pdfDate = `D:${now.getFullYear()}${pad2(now.getMonth() + 1)}${pad2(now.getDate())}${pad2(now.getHours())}${pad2(now.getMinutes())}${pad2(now.getSeconds())}`;
6018
- const isoDate = `${now.getFullYear()}-${pad2(now.getMonth() + 1)}-${pad2(now.getDate())}T${pad2(now.getHours())}:${pad2(now.getMinutes())}:${pad2(now.getSeconds())}`;
6041
+ const { pdfDate, xmpDate: isoDate } = buildPdfMetadata();
6019
6042
  const infoTitle = params.docTitle || title || "";
6020
6043
  emitObj(
6021
6044
  infoObjNum,
@@ -6095,7 +6118,7 @@ endobj
6095
6118
  }
6096
6119
  }
6097
6120
  const writer = { emit, emitObj, emitStreamObj, offset: getOffset, adjustOffset, objOffsets, parts };
6098
- writeXrefTrailer(writer, totalObjs, infoObjNum, encState);
6121
+ writeXrefTrailer(writer, totalObjs, infoObjNum, encState, `${infoTitle}|${pdfDate}`);
6099
6122
  return parts.join("");
6100
6123
  }
6101
6124
  function buildPDFBytes(params, layoutOptions) {
@@ -9701,10 +9724,7 @@ function buildDocumentPDF(params, layoutOptions) {
9701
9724
  }
9702
9725
  const baseObjCount = enc.isUnicode ? 4 + fontEntries.length * 5 + imageCount + wmExtraObjs + totalPages * 2 + totalAnnots + totalFormObjs + formFontObjs : 4 + imageCount + wmExtraObjs + totalPages * 2 + totalAnnots + totalFormObjs + formFontObjs;
9703
9726
  const infoObjNum = baseObjCount + 1;
9704
- const now = /* @__PURE__ */ new Date();
9705
- const pad2 = (n) => String(n).padStart(2, "0");
9706
- const pdfDate = `D:${now.getFullYear()}${pad2(now.getMonth() + 1)}${pad2(now.getDate())}${pad2(now.getHours())}${pad2(now.getMinutes())}${pad2(now.getSeconds())}`;
9707
- const isoDate = `${now.getFullYear()}-${pad2(now.getMonth() + 1)}-${pad2(now.getDate())}T${pad2(now.getHours())}:${pad2(now.getMinutes())}:${pad2(now.getSeconds())}`;
9727
+ const { pdfDate, xmpDate: isoDate } = buildPdfMetadata();
9708
9728
  const infoTitle = params.title ?? "";
9709
9729
  const metaParts = [`/Title ${encodePdfTextString(infoTitle)}`, "/Producer (pdfnative)", `/CreationDate (${pdfDate})`];
9710
9730
  if (params.metadata?.author) {
@@ -9732,7 +9752,7 @@ function buildDocumentPDF(params, layoutOptions) {
9732
9752
  structTreeRootObjNum = tree.structTreeRootObjNum;
9733
9753
  totalObjs = treeStart + tree.totalObjects - 1;
9734
9754
  xmpObjNum = totalObjs + 1;
9735
- const xmpContent = buildXMPMetadata(infoTitle, isoDate, pdfaConfig.pdfaPart, pdfaConfig.pdfaConformance);
9755
+ const xmpContent = buildXMPMetadata(infoTitle, isoDate, pdfaConfig.pdfaPart, pdfaConfig.pdfaConformance, params.metadata?.author);
9736
9756
  emitStreamObj(
9737
9757
  xmpObjNum,
9738
9758
  `<< /Type /Metadata /Subtype /XML /Length ${xmpContent.length}`,
@@ -9838,7 +9858,7 @@ endobj
9838
9858
  }
9839
9859
  }
9840
9860
  const writer = { emit, emitObj, emitStreamObj, offset: getOffset, adjustOffset, objOffsets, parts };
9841
- writeXrefTrailer(writer, totalObjs, infoObjNum, encState);
9861
+ writeXrefTrailer(writer, totalObjs, infoObjNum, encState, `${infoTitle}|${pdfDate}`);
9842
9862
  return parts.join("");
9843
9863
  }
9844
9864
  function buildDocumentPDFBytes(params, layoutOptions) {