pdfnative 1.0.3 → 1.0.5

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");
@@ -5473,6 +5499,7 @@ var DEFAULT_TEXT_COLOR = "0.75 0.75 0.75";
5473
5499
  var DEFAULT_TEXT_OPACITY = 0.15;
5474
5500
  var DEFAULT_TEXT_ANGLE = -45;
5475
5501
  var DEFAULT_IMAGE_OPACITY = 0.1;
5502
+ var DEFAULT_CAP_HEIGHT_RATIO = 0.718;
5476
5503
  function validateWatermark(watermark, pdfaLevel) {
5477
5504
  if (pdfaLevel === "pdfa1b") {
5478
5505
  const textOpacity = watermark.text?.opacity ?? DEFAULT_TEXT_OPACITY;
@@ -5527,10 +5554,12 @@ function _buildTextWatermarkOps(wm, pgW, pgH, enc, gsName) {
5527
5554
  const cy = pgH / 2;
5528
5555
  const textWidth = enc.tw(wm.text, sz);
5529
5556
  const offsetX = -textWidth / 2;
5530
- const offsetY = -sz / 2;
5557
+ const fd = enc.fontData;
5558
+ const capHeightRatio = fd ? fd.metrics.capHeight / fd.metrics.unitsPerEm : DEFAULT_CAP_HEIGHT_RATIO;
5559
+ const offsetY = -sz * capHeightRatio / 2;
5531
5560
  const tx = cx + offsetX * cos - offsetY * sin;
5532
5561
  const ty = cy + offsetX * sin + offsetY * cos;
5533
- const escapedText = pdfString(wm.text);
5562
+ const escapedText = enc.ps(wm.text);
5534
5563
  const ops = [
5535
5564
  "q",
5536
5565
  `${gsName} gs`,
@@ -6012,10 +6041,7 @@ function buildPDF(params, layoutOptions) {
6012
6041
  }
6013
6042
  const baseObjCount = enc.isUnicode ? 4 + fontEntries.length * 5 + wmExtraObjs + totalPages * 2 : 4 + wmExtraObjs + totalPages * 2;
6014
6043
  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())}`;
6044
+ const { pdfDate, xmpDate: isoDate } = buildPdfMetadata();
6019
6045
  const infoTitle = params.docTitle || title || "";
6020
6046
  emitObj(
6021
6047
  infoObjNum,
@@ -6095,7 +6121,7 @@ endobj
6095
6121
  }
6096
6122
  }
6097
6123
  const writer = { emit, emitObj, emitStreamObj, offset: getOffset, adjustOffset, objOffsets, parts };
6098
- writeXrefTrailer(writer, totalObjs, infoObjNum, encState);
6124
+ writeXrefTrailer(writer, totalObjs, infoObjNum, encState, `${infoTitle}|${pdfDate}`);
6099
6125
  return parts.join("");
6100
6126
  }
6101
6127
  function buildPDFBytes(params, layoutOptions) {
@@ -9701,10 +9727,7 @@ function buildDocumentPDF(params, layoutOptions) {
9701
9727
  }
9702
9728
  const baseObjCount = enc.isUnicode ? 4 + fontEntries.length * 5 + imageCount + wmExtraObjs + totalPages * 2 + totalAnnots + totalFormObjs + formFontObjs : 4 + imageCount + wmExtraObjs + totalPages * 2 + totalAnnots + totalFormObjs + formFontObjs;
9703
9729
  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())}`;
9730
+ const { pdfDate, xmpDate: isoDate } = buildPdfMetadata();
9708
9731
  const infoTitle = params.title ?? "";
9709
9732
  const metaParts = [`/Title ${encodePdfTextString(infoTitle)}`, "/Producer (pdfnative)", `/CreationDate (${pdfDate})`];
9710
9733
  if (params.metadata?.author) {
@@ -9732,7 +9755,7 @@ function buildDocumentPDF(params, layoutOptions) {
9732
9755
  structTreeRootObjNum = tree.structTreeRootObjNum;
9733
9756
  totalObjs = treeStart + tree.totalObjects - 1;
9734
9757
  xmpObjNum = totalObjs + 1;
9735
- const xmpContent = buildXMPMetadata(infoTitle, isoDate, pdfaConfig.pdfaPart, pdfaConfig.pdfaConformance);
9758
+ const xmpContent = buildXMPMetadata(infoTitle, isoDate, pdfaConfig.pdfaPart, pdfaConfig.pdfaConformance, params.metadata?.author);
9736
9759
  emitStreamObj(
9737
9760
  xmpObjNum,
9738
9761
  `<< /Type /Metadata /Subtype /XML /Length ${xmpContent.length}`,
@@ -9838,7 +9861,7 @@ endobj
9838
9861
  }
9839
9862
  }
9840
9863
  const writer = { emit, emitObj, emitStreamObj, offset: getOffset, adjustOffset, objOffsets, parts };
9841
- writeXrefTrailer(writer, totalObjs, infoObjNum, encState);
9864
+ writeXrefTrailer(writer, totalObjs, infoObjNum, encState, `${infoTitle}|${pdfDate}`);
9842
9865
  return parts.join("");
9843
9866
  }
9844
9867
  function buildDocumentPDFBytes(params, layoutOptions) {