@yudaocode/univer-pdfs 1.6.217 → 1.6.219
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 +8 -8
- package/lib/cjs/facade/index.js +2 -1
- package/lib/cjs/facade/index.js.map +1 -1
- package/lib/cjs/index.js +4071 -857
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/jsx/index.js +1688 -418
- package/lib/cjs/jsx/index.js.map +1 -1
- package/lib/es/{chunk-EMSHTYYU.js → chunk-PQUNQAVL.js} +2072 -289
- package/lib/es/chunk-PQUNQAVL.js.map +1 -0
- package/lib/es/facade/index.js +2 -1
- package/lib/es/facade/index.js.map +1 -1
- package/lib/es/index.js +1588 -114
- package/lib/es/index.js.map +1 -1
- package/lib/es/jsx/index.js +1 -1
- package/lib/types/index.d.ts +303 -4
- package/lib/types/jsx/index.d.ts +2 -2
- package/lib/types/jsx/jsx-dev-runtime.d.ts +1 -1
- package/lib/types/jsx/jsx-runtime.d.ts +1 -1
- package/lib/types/{jsx-dev-runtime-Dm72R_Vm.d.ts → jsx-dev-runtime-B1jMa7VW.d.ts} +87 -4
- package/package.json +1 -1
- package/lib/es/chunk-EMSHTYYU.js.map +0 -1
package/lib/cjs/jsx/index.js
CHANGED
|
@@ -296,6 +296,22 @@ var PDF_PAGE_PRESETS = {
|
|
|
296
296
|
INVOICE_240_93: { width: mmToPt(240), height: mmToPt(93), preset: "INVOICE_240_93" },
|
|
297
297
|
CUSTOM: { width: 595.28, height: 841.89, preset: "CUSTOM" }
|
|
298
298
|
};
|
|
299
|
+
function resolvePdfCropBoxPt(cropBox) {
|
|
300
|
+
if (!cropBox) return null;
|
|
301
|
+
const rawLeft = cropBox.left ?? cropBox.leftPt;
|
|
302
|
+
const rawTop = cropBox.top ?? cropBox.topPt;
|
|
303
|
+
const rawWidth = cropBox.width ?? cropBox.widthPt;
|
|
304
|
+
const rawHeight = cropBox.height ?? cropBox.heightPt;
|
|
305
|
+
if (rawLeft === void 0 && rawTop === void 0 && rawWidth === void 0 && rawHeight === void 0) {
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
return {
|
|
309
|
+
left: Math.max(0, Number(rawLeft) || 0),
|
|
310
|
+
top: Math.max(0, Number(rawTop) || 0),
|
|
311
|
+
width: Math.max(8, Number(rawWidth) || 8),
|
|
312
|
+
height: Math.max(8, Number(rawHeight) || 8)
|
|
313
|
+
};
|
|
314
|
+
}
|
|
299
315
|
var PdfElementType = /* @__PURE__ */ ((PdfElementType2) => {
|
|
300
316
|
PdfElementType2["TEXT"] = "text";
|
|
301
317
|
PdfElementType2["LIST"] = "list";
|
|
@@ -408,7 +424,9 @@ function createDefaultPdfUnitData(partial) {
|
|
|
408
424
|
nativeTextSpansByPage: partial?.editState?.nativeTextSpansByPage ? import_core.Tools.deepClone(partial.editState.nativeTextSpansByPage) : partial?.metadata?.nativeTextSpansByPage ? import_core.Tools.deepClone(partial.metadata.nativeTextSpansByPage) : void 0
|
|
409
425
|
},
|
|
410
426
|
...partial?.layers !== void 0 ? { layers: import_core.Tools.deepClone(partial.layers) } : {},
|
|
427
|
+
...partial?.bookmarks !== void 0 ? { bookmarks: import_core.Tools.deepClone(partial.bookmarks) } : partial?.metadata?.bookmarks !== void 0 ? { bookmarks: import_core.Tools.deepClone(partial.metadata.bookmarks) } : {},
|
|
411
428
|
...partial?.attachments !== void 0 ? { attachments: import_core.Tools.deepClone(partial.attachments) } : {},
|
|
429
|
+
...partial?.security !== void 0 ? { security: import_core.Tools.deepClone(partial.security) } : partial?.metadata?.security !== void 0 ? { security: import_core.Tools.deepClone(partial.metadata.security) } : {},
|
|
412
430
|
...partial?.securityPermissions !== void 0 ? { securityPermissions: import_core.Tools.deepClone(partial.securityPermissions) } : {},
|
|
413
431
|
...partial?.signatureEnvelope !== void 0 ? { signatureEnvelope: import_core.Tools.deepClone(partial.signatureEnvelope) } : {},
|
|
414
432
|
...partial?.pdfACompliant !== void 0 ? { pdfACompliant: partial.pdfACompliant } : {},
|
|
@@ -2710,10 +2728,148 @@ function fromPdfmeTemplate(template, options) {
|
|
|
2710
2728
|
}
|
|
2711
2729
|
|
|
2712
2730
|
// src/exporter/pdf-exporter.ts
|
|
2713
|
-
var
|
|
2714
|
-
var
|
|
2731
|
+
var import_core4 = require("@univerjs/core");
|
|
2732
|
+
var import_pdf_lib4 = require("pdf-lib");
|
|
2715
2733
|
var import_qrcode2 = __toESM(require("qrcode"));
|
|
2716
2734
|
|
|
2735
|
+
// src/exporter/pdf-stamps.ts
|
|
2736
|
+
var PDF_STANDARD_STAMP_PRESETS = [
|
|
2737
|
+
{
|
|
2738
|
+
id: "approved",
|
|
2739
|
+
title: "\u5DF2\u6279\u51C6",
|
|
2740
|
+
subtitle: "APPROVED",
|
|
2741
|
+
color: "#16A34A",
|
|
2742
|
+
shape: "rect"
|
|
2743
|
+
},
|
|
2744
|
+
{
|
|
2745
|
+
id: "rejected",
|
|
2746
|
+
title: "\u5DF2\u9A73\u56DE",
|
|
2747
|
+
subtitle: "REJECTED",
|
|
2748
|
+
color: "#DC2626",
|
|
2749
|
+
shape: "rect"
|
|
2750
|
+
},
|
|
2751
|
+
{
|
|
2752
|
+
id: "confidential",
|
|
2753
|
+
title: "\u673A\u5BC6\u6587\u4EF6",
|
|
2754
|
+
subtitle: "CONFIDENTIAL",
|
|
2755
|
+
color: "#B91C1C",
|
|
2756
|
+
shape: "pill"
|
|
2757
|
+
},
|
|
2758
|
+
{
|
|
2759
|
+
id: "draft",
|
|
2760
|
+
title: "\u8349\u7A3F\u4EC5\u4F9B\u53C2\u8003",
|
|
2761
|
+
subtitle: "DRAFT ONLY",
|
|
2762
|
+
color: "#2563EB",
|
|
2763
|
+
shape: "pill"
|
|
2764
|
+
},
|
|
2765
|
+
{
|
|
2766
|
+
id: "reviewed",
|
|
2767
|
+
title: "\u5DF2\u5BA1\u6838",
|
|
2768
|
+
subtitle: "REVIEWED",
|
|
2769
|
+
color: "#0D9488",
|
|
2770
|
+
shape: "rect"
|
|
2771
|
+
},
|
|
2772
|
+
{
|
|
2773
|
+
id: "urgent",
|
|
2774
|
+
title: "\u7279\u6025\u529E\u7406",
|
|
2775
|
+
subtitle: "URGENT",
|
|
2776
|
+
color: "#EA580C",
|
|
2777
|
+
shape: "pill"
|
|
2778
|
+
},
|
|
2779
|
+
{
|
|
2780
|
+
id: "void",
|
|
2781
|
+
title: "\u5DF2\u4F5C\u5E9F",
|
|
2782
|
+
subtitle: "VOID",
|
|
2783
|
+
color: "#64748B",
|
|
2784
|
+
shape: "rect"
|
|
2785
|
+
},
|
|
2786
|
+
{
|
|
2787
|
+
id: "archived",
|
|
2788
|
+
title: "\u5DF2\u5F52\u6863",
|
|
2789
|
+
subtitle: "ARCHIVED",
|
|
2790
|
+
color: "#4F46E5",
|
|
2791
|
+
shape: "circle"
|
|
2792
|
+
}
|
|
2793
|
+
];
|
|
2794
|
+
var PDF_STANDARD_STAMP_PRESET_MAP = PDF_STANDARD_STAMP_PRESETS.reduce(
|
|
2795
|
+
(acc, item) => {
|
|
2796
|
+
acc[item.id] = item;
|
|
2797
|
+
return acc;
|
|
2798
|
+
},
|
|
2799
|
+
{}
|
|
2800
|
+
);
|
|
2801
|
+
function unescapeXmlEntities(raw) {
|
|
2802
|
+
return raw.replace(/"/g, '"').replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
|
|
2803
|
+
}
|
|
2804
|
+
function extractSvgTextDescriptors(svgXml, boxWidthPt, boxHeightPt) {
|
|
2805
|
+
if (!svgXml || boxWidthPt <= 0 || boxHeightPt <= 0) return [];
|
|
2806
|
+
const vbMatch = /viewBox\s*=\s*["']([^"']+)["']/i.exec(svgXml);
|
|
2807
|
+
let vbX = 0;
|
|
2808
|
+
let vbY = 0;
|
|
2809
|
+
let vbW = boxWidthPt;
|
|
2810
|
+
let vbH = boxHeightPt;
|
|
2811
|
+
if (vbMatch) {
|
|
2812
|
+
const nums = vbMatch[1].trim().split(/[\s,]+/).map(Number);
|
|
2813
|
+
if (nums.length >= 4 && nums[2] > 0 && nums[3] > 0) {
|
|
2814
|
+
vbX = nums[0];
|
|
2815
|
+
vbY = nums[1];
|
|
2816
|
+
vbW = nums[2];
|
|
2817
|
+
vbH = nums[3];
|
|
2818
|
+
}
|
|
2819
|
+
}
|
|
2820
|
+
const scale = Math.min(boxWidthPt / vbW, boxHeightPt / vbH);
|
|
2821
|
+
const offsetX = (boxWidthPt - vbW * scale) / 2 - vbX * scale;
|
|
2822
|
+
const offsetY = (boxHeightPt - vbH * scale) / 2 - vbY * scale;
|
|
2823
|
+
const out = [];
|
|
2824
|
+
const textRe = /<text\b([^>]*)>([\s\S]*?)<\/text>/gi;
|
|
2825
|
+
let m;
|
|
2826
|
+
while ((m = textRe.exec(svgXml)) !== null) {
|
|
2827
|
+
const attrStr = m[1] ?? "";
|
|
2828
|
+
const inner = unescapeXmlEntities((m[2] ?? "").replace(/<[^>]+>/g, "").trim());
|
|
2829
|
+
if (!inner) continue;
|
|
2830
|
+
const getAttr = (name) => {
|
|
2831
|
+
const re = new RegExp(`\\b${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)')`, "i");
|
|
2832
|
+
const hit = re.exec(attrStr);
|
|
2833
|
+
return hit ? hit[1] ?? hit[2] : void 0;
|
|
2834
|
+
};
|
|
2835
|
+
const rawX = parseFloat(getAttr("x") ?? "0") || 0;
|
|
2836
|
+
const rawY = parseFloat(getAttr("y") ?? "0") || 0;
|
|
2837
|
+
const rawFs = parseFloat(getAttr("font-size") ?? "12") || 12;
|
|
2838
|
+
const anchor = (getAttr("text-anchor") ?? "start").toLowerCase();
|
|
2839
|
+
const weight = (getAttr("font-weight") ?? "normal").toLowerCase();
|
|
2840
|
+
const fill = getAttr("fill") ?? "#1B1C1F";
|
|
2841
|
+
const fontSizePt = Math.max(5, Math.round(rawFs * scale * 100) / 100);
|
|
2842
|
+
const xPt = offsetX + rawX * scale;
|
|
2843
|
+
const baselineYPt = offsetY + rawY * scale;
|
|
2844
|
+
const heightPt = Math.max(fontSizePt * 1.35, 8);
|
|
2845
|
+
const topPt = Math.max(0, baselineYPt - fontSizePt * 0.88);
|
|
2846
|
+
let align = "left";
|
|
2847
|
+
let leftPt = xPt;
|
|
2848
|
+
let widthPt = Math.max(24, boxWidthPt - xPt);
|
|
2849
|
+
if (anchor === "middle") {
|
|
2850
|
+
align = "center";
|
|
2851
|
+
widthPt = boxWidthPt;
|
|
2852
|
+
leftPt = 0;
|
|
2853
|
+
} else if (anchor === "end") {
|
|
2854
|
+
align = "right";
|
|
2855
|
+
widthPt = Math.max(24, xPt);
|
|
2856
|
+
leftPt = 0;
|
|
2857
|
+
}
|
|
2858
|
+
out.push({
|
|
2859
|
+
text: inner,
|
|
2860
|
+
leftPt,
|
|
2861
|
+
topPt,
|
|
2862
|
+
widthPt,
|
|
2863
|
+
heightPt,
|
|
2864
|
+
fontSizePt,
|
|
2865
|
+
bold: weight === "bold" || Number(weight) >= 600,
|
|
2866
|
+
color: fill,
|
|
2867
|
+
align
|
|
2868
|
+
});
|
|
2869
|
+
}
|
|
2870
|
+
return out;
|
|
2871
|
+
}
|
|
2872
|
+
|
|
2717
2873
|
// src/exporter/pdf-2d-barcodes.ts
|
|
2718
2874
|
function collapseRowToRuns(row) {
|
|
2719
2875
|
const runs = [];
|
|
@@ -4185,7 +4341,1005 @@ function encodeBarcodeByFormat(format, raw) {
|
|
|
4185
4341
|
}
|
|
4186
4342
|
|
|
4187
4343
|
// src/exporter/pdf-native-text.ts
|
|
4344
|
+
var import_core3 = require("@univerjs/core");
|
|
4345
|
+
var import_pdf_lib2 = require("pdf-lib");
|
|
4346
|
+
|
|
4347
|
+
// src/exporter/pdf-security.ts
|
|
4188
4348
|
var import_pdf_lib = require("pdf-lib");
|
|
4349
|
+
var PdfSecurityError = class extends Error {
|
|
4350
|
+
constructor(code, message) {
|
|
4351
|
+
super(message);
|
|
4352
|
+
this.name = "PdfSecurityError";
|
|
4353
|
+
this.code = code;
|
|
4354
|
+
}
|
|
4355
|
+
};
|
|
4356
|
+
var PDF_PASSWORD_PADDING = new Uint8Array([
|
|
4357
|
+
40,
|
|
4358
|
+
191,
|
|
4359
|
+
78,
|
|
4360
|
+
94,
|
|
4361
|
+
78,
|
|
4362
|
+
117,
|
|
4363
|
+
138,
|
|
4364
|
+
65,
|
|
4365
|
+
100,
|
|
4366
|
+
0,
|
|
4367
|
+
78,
|
|
4368
|
+
86,
|
|
4369
|
+
255,
|
|
4370
|
+
250,
|
|
4371
|
+
1,
|
|
4372
|
+
8,
|
|
4373
|
+
46,
|
|
4374
|
+
46,
|
|
4375
|
+
0,
|
|
4376
|
+
182,
|
|
4377
|
+
208,
|
|
4378
|
+
104,
|
|
4379
|
+
62,
|
|
4380
|
+
128,
|
|
4381
|
+
47,
|
|
4382
|
+
12,
|
|
4383
|
+
169,
|
|
4384
|
+
254,
|
|
4385
|
+
100,
|
|
4386
|
+
83,
|
|
4387
|
+
105,
|
|
4388
|
+
122
|
|
4389
|
+
]);
|
|
4390
|
+
var DEFAULT_ALL_PERMISSIONS = Object.freeze({
|
|
4391
|
+
print: true,
|
|
4392
|
+
highResPrint: true,
|
|
4393
|
+
modify: true,
|
|
4394
|
+
copy: true,
|
|
4395
|
+
annotate: true,
|
|
4396
|
+
fillForms: true,
|
|
4397
|
+
assemble: true
|
|
4398
|
+
});
|
|
4399
|
+
var MD5_SHIFTS = [
|
|
4400
|
+
7,
|
|
4401
|
+
12,
|
|
4402
|
+
17,
|
|
4403
|
+
22,
|
|
4404
|
+
7,
|
|
4405
|
+
12,
|
|
4406
|
+
17,
|
|
4407
|
+
22,
|
|
4408
|
+
7,
|
|
4409
|
+
12,
|
|
4410
|
+
17,
|
|
4411
|
+
22,
|
|
4412
|
+
7,
|
|
4413
|
+
12,
|
|
4414
|
+
17,
|
|
4415
|
+
22,
|
|
4416
|
+
5,
|
|
4417
|
+
9,
|
|
4418
|
+
14,
|
|
4419
|
+
20,
|
|
4420
|
+
5,
|
|
4421
|
+
9,
|
|
4422
|
+
14,
|
|
4423
|
+
20,
|
|
4424
|
+
5,
|
|
4425
|
+
9,
|
|
4426
|
+
14,
|
|
4427
|
+
20,
|
|
4428
|
+
5,
|
|
4429
|
+
9,
|
|
4430
|
+
14,
|
|
4431
|
+
20,
|
|
4432
|
+
4,
|
|
4433
|
+
11,
|
|
4434
|
+
16,
|
|
4435
|
+
23,
|
|
4436
|
+
4,
|
|
4437
|
+
11,
|
|
4438
|
+
16,
|
|
4439
|
+
23,
|
|
4440
|
+
4,
|
|
4441
|
+
11,
|
|
4442
|
+
16,
|
|
4443
|
+
23,
|
|
4444
|
+
4,
|
|
4445
|
+
11,
|
|
4446
|
+
16,
|
|
4447
|
+
23,
|
|
4448
|
+
6,
|
|
4449
|
+
10,
|
|
4450
|
+
15,
|
|
4451
|
+
21,
|
|
4452
|
+
6,
|
|
4453
|
+
10,
|
|
4454
|
+
15,
|
|
4455
|
+
21,
|
|
4456
|
+
6,
|
|
4457
|
+
10,
|
|
4458
|
+
15,
|
|
4459
|
+
21,
|
|
4460
|
+
6,
|
|
4461
|
+
10,
|
|
4462
|
+
15,
|
|
4463
|
+
21
|
|
4464
|
+
];
|
|
4465
|
+
var MD5_K = (() => {
|
|
4466
|
+
const table = new Uint32Array(64);
|
|
4467
|
+
for (let i = 0; i < 64; i++) {
|
|
4468
|
+
table[i] = Math.floor(Math.abs(Math.sin(i + 1)) * 4294967296) >>> 0;
|
|
4469
|
+
}
|
|
4470
|
+
return table;
|
|
4471
|
+
})();
|
|
4472
|
+
function rotl32(x, n) {
|
|
4473
|
+
return (x << n | x >>> 32 - n) >>> 0;
|
|
4474
|
+
}
|
|
4475
|
+
function md5Bytes(data) {
|
|
4476
|
+
const msgLen = data.byteLength;
|
|
4477
|
+
const rem = (msgLen + 9) % 64;
|
|
4478
|
+
const padZeroes = rem === 0 ? 0 : 64 - rem;
|
|
4479
|
+
const totalLen = msgLen + 9 + padZeroes;
|
|
4480
|
+
const buf = new Uint8Array(totalLen);
|
|
4481
|
+
buf.set(data, 0);
|
|
4482
|
+
buf[msgLen] = 128;
|
|
4483
|
+
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
4484
|
+
const bitLenLow = msgLen * 8 >>> 0;
|
|
4485
|
+
const bitLenHigh = Math.floor(msgLen * 8 / 4294967296) >>> 0;
|
|
4486
|
+
view.setUint32(totalLen - 8, bitLenLow, true);
|
|
4487
|
+
view.setUint32(totalLen - 4, bitLenHigh, true);
|
|
4488
|
+
let a0 = 1732584193;
|
|
4489
|
+
let b0 = 4023233417;
|
|
4490
|
+
let c0 = 2562383102;
|
|
4491
|
+
let d0 = 271733878;
|
|
4492
|
+
const M = new Uint32Array(16);
|
|
4493
|
+
for (let offset = 0; offset < totalLen; offset += 64) {
|
|
4494
|
+
for (let j = 0; j < 16; j++) {
|
|
4495
|
+
M[j] = view.getUint32(offset + j * 4, true);
|
|
4496
|
+
}
|
|
4497
|
+
let A = a0;
|
|
4498
|
+
let B = b0;
|
|
4499
|
+
let C = c0;
|
|
4500
|
+
let D = d0;
|
|
4501
|
+
for (let i = 0; i < 64; i++) {
|
|
4502
|
+
let F;
|
|
4503
|
+
let g;
|
|
4504
|
+
if (i < 16) {
|
|
4505
|
+
F = B & C | ~B & D;
|
|
4506
|
+
g = i;
|
|
4507
|
+
} else if (i < 32) {
|
|
4508
|
+
F = D & B | ~D & C;
|
|
4509
|
+
g = (5 * i + 1) % 16;
|
|
4510
|
+
} else if (i < 48) {
|
|
4511
|
+
F = B ^ C ^ D;
|
|
4512
|
+
g = (3 * i + 5) % 16;
|
|
4513
|
+
} else {
|
|
4514
|
+
F = C ^ (B | ~D);
|
|
4515
|
+
g = 7 * i % 16;
|
|
4516
|
+
}
|
|
4517
|
+
const sum = A + F + MD5_K[i] + M[g] >>> 0;
|
|
4518
|
+
A = D;
|
|
4519
|
+
D = C;
|
|
4520
|
+
C = B;
|
|
4521
|
+
B = B + rotl32(sum, MD5_SHIFTS[i]) >>> 0;
|
|
4522
|
+
}
|
|
4523
|
+
a0 = a0 + A >>> 0;
|
|
4524
|
+
b0 = b0 + B >>> 0;
|
|
4525
|
+
c0 = c0 + C >>> 0;
|
|
4526
|
+
d0 = d0 + D >>> 0;
|
|
4527
|
+
}
|
|
4528
|
+
const out = new Uint8Array(16);
|
|
4529
|
+
const outView = new DataView(out.buffer);
|
|
4530
|
+
outView.setUint32(0, a0, true);
|
|
4531
|
+
outView.setUint32(4, b0, true);
|
|
4532
|
+
outView.setUint32(8, c0, true);
|
|
4533
|
+
outView.setUint32(12, d0, true);
|
|
4534
|
+
return out;
|
|
4535
|
+
}
|
|
4536
|
+
function rc4Bytes(key, data) {
|
|
4537
|
+
const keyLen = key.byteLength;
|
|
4538
|
+
if (keyLen === 0) return data.slice();
|
|
4539
|
+
const S = new Uint8Array(256);
|
|
4540
|
+
for (let i2 = 0; i2 < 256; i2++) {
|
|
4541
|
+
S[i2] = i2;
|
|
4542
|
+
}
|
|
4543
|
+
let j = 0;
|
|
4544
|
+
for (let i2 = 0; i2 < 256; i2++) {
|
|
4545
|
+
j = j + S[i2] + key[i2 % keyLen] & 255;
|
|
4546
|
+
const tmp = S[i2];
|
|
4547
|
+
S[i2] = S[j];
|
|
4548
|
+
S[j] = tmp;
|
|
4549
|
+
}
|
|
4550
|
+
const out = new Uint8Array(data.byteLength);
|
|
4551
|
+
let i = 0;
|
|
4552
|
+
j = 0;
|
|
4553
|
+
for (let k = 0; k < data.byteLength; k++) {
|
|
4554
|
+
i = i + 1 & 255;
|
|
4555
|
+
j = j + S[i] & 255;
|
|
4556
|
+
const tmp = S[i];
|
|
4557
|
+
S[i] = S[j];
|
|
4558
|
+
S[j] = tmp;
|
|
4559
|
+
out[k] = data[k] ^ S[S[i] + S[j] & 255];
|
|
4560
|
+
}
|
|
4561
|
+
return out;
|
|
4562
|
+
}
|
|
4563
|
+
function bytesToHex(bytes) {
|
|
4564
|
+
let hex = "";
|
|
4565
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
4566
|
+
hex += bytes[i].toString(16).padStart(2, "0").toUpperCase();
|
|
4567
|
+
}
|
|
4568
|
+
return hex;
|
|
4569
|
+
}
|
|
4570
|
+
function hexToBytes(hexRaw) {
|
|
4571
|
+
const cleaned = hexRaw.replace(/\s+/g, "");
|
|
4572
|
+
const padded = cleaned.length % 2 === 1 ? `${cleaned}0` : cleaned;
|
|
4573
|
+
const out = new Uint8Array(padded.length / 2);
|
|
4574
|
+
for (let i = 0; i < out.byteLength; i++) {
|
|
4575
|
+
out[i] = parseInt(padded.slice(i * 2, i * 2 + 2), 16) || 0;
|
|
4576
|
+
}
|
|
4577
|
+
return out;
|
|
4578
|
+
}
|
|
4579
|
+
function encodePasswordToBytes(password) {
|
|
4580
|
+
if (!password) return new Uint8Array(0);
|
|
4581
|
+
let hasHighCodePoint = false;
|
|
4582
|
+
for (let i = 0; i < password.length; i++) {
|
|
4583
|
+
if (password.charCodeAt(i) > 255) {
|
|
4584
|
+
hasHighCodePoint = true;
|
|
4585
|
+
break;
|
|
4586
|
+
}
|
|
4587
|
+
}
|
|
4588
|
+
if (hasHighCodePoint) {
|
|
4589
|
+
return new TextEncoder().encode(password);
|
|
4590
|
+
}
|
|
4591
|
+
const out = new Uint8Array(password.length);
|
|
4592
|
+
for (let i = 0; i < password.length; i++) {
|
|
4593
|
+
out[i] = password.charCodeAt(i) & 255;
|
|
4594
|
+
}
|
|
4595
|
+
return out;
|
|
4596
|
+
}
|
|
4597
|
+
function padPdfPassword(password) {
|
|
4598
|
+
const raw = password instanceof Uint8Array ? password : encodePasswordToBytes(password);
|
|
4599
|
+
const out = new Uint8Array(32);
|
|
4600
|
+
const copyLen = Math.min(32, raw.byteLength);
|
|
4601
|
+
out.set(raw.subarray(0, copyLen), 0);
|
|
4602
|
+
if (copyLen < 32) {
|
|
4603
|
+
out.set(PDF_PASSWORD_PADDING.subarray(0, 32 - copyLen), copyLen);
|
|
4604
|
+
}
|
|
4605
|
+
return out;
|
|
4606
|
+
}
|
|
4607
|
+
function encodePdfPermissionFlags(permissions, revision = 3) {
|
|
4608
|
+
const allowPrint = permissions?.print !== false;
|
|
4609
|
+
const allowModify = permissions?.modify !== false;
|
|
4610
|
+
const allowCopy = permissions?.copy !== false;
|
|
4611
|
+
const allowAnnotate = permissions?.annotate !== false;
|
|
4612
|
+
const allowFillForms = permissions?.fillForms !== false;
|
|
4613
|
+
const allowAssemble = permissions?.assemble !== false;
|
|
4614
|
+
const allowHighResPrint = permissions?.highResPrint !== void 0 ? Boolean(permissions.highResPrint) : allowPrint;
|
|
4615
|
+
let mask = 4294963392 | 1 << 9;
|
|
4616
|
+
if (allowPrint) mask |= 1 << 2;
|
|
4617
|
+
if (allowModify) mask |= 1 << 3;
|
|
4618
|
+
if (allowCopy) mask |= 1 << 4;
|
|
4619
|
+
if (allowAnnotate) mask |= 1 << 5;
|
|
4620
|
+
if (revision >= 3) {
|
|
4621
|
+
if (allowFillForms) mask |= 1 << 8;
|
|
4622
|
+
if (allowAssemble) mask |= 1 << 10;
|
|
4623
|
+
if (allowHighResPrint && allowPrint) mask |= 1 << 11;
|
|
4624
|
+
} else {
|
|
4625
|
+
mask |= 1 << 8 | 1 << 10 | 1 << 11;
|
|
4626
|
+
}
|
|
4627
|
+
return mask | 0;
|
|
4628
|
+
}
|
|
4629
|
+
function decodePdfPermissionFlags(pMask, revision = 3) {
|
|
4630
|
+
const print = (pMask & 1 << 2) !== 0;
|
|
4631
|
+
const modify = (pMask & 1 << 3) !== 0;
|
|
4632
|
+
const copy = (pMask & 1 << 4) !== 0;
|
|
4633
|
+
const annotate = (pMask & 1 << 5) !== 0;
|
|
4634
|
+
const fillForms = revision >= 3 ? (pMask & 1 << 8) !== 0 : annotate;
|
|
4635
|
+
const assemble = revision >= 3 ? (pMask & 1 << 10) !== 0 : modify;
|
|
4636
|
+
const highResPrint = revision >= 3 ? print && (pMask & 1 << 11) !== 0 : print;
|
|
4637
|
+
return {
|
|
4638
|
+
print,
|
|
4639
|
+
highResPrint,
|
|
4640
|
+
modify,
|
|
4641
|
+
copy,
|
|
4642
|
+
annotate,
|
|
4643
|
+
fillForms,
|
|
4644
|
+
assemble
|
|
4645
|
+
};
|
|
4646
|
+
}
|
|
4647
|
+
function isPdfSecurityActive(security) {
|
|
4648
|
+
if (!security || security.enabled === false) return false;
|
|
4649
|
+
if (security.enabled === true) return true;
|
|
4650
|
+
if (security.userPassword && security.userPassword.length > 0) return true;
|
|
4651
|
+
if (security.ownerPassword && security.ownerPassword.length > 0) return true;
|
|
4652
|
+
if (security.permissions && Object.keys(security.permissions).length > 0) return true;
|
|
4653
|
+
return false;
|
|
4654
|
+
}
|
|
4655
|
+
function computeOwnerPasswordValue(userPassword, ownerPassword, revision, keyLengthBits) {
|
|
4656
|
+
const effectiveOwner = ownerPassword || userPassword || "";
|
|
4657
|
+
const ownerPad = padPdfPassword(effectiveOwner);
|
|
4658
|
+
let hash = md5Bytes(ownerPad);
|
|
4659
|
+
if (revision >= 3) {
|
|
4660
|
+
for (let i = 0; i < 50; i++) {
|
|
4661
|
+
hash = md5Bytes(hash);
|
|
4662
|
+
}
|
|
4663
|
+
}
|
|
4664
|
+
const keyByteLen = revision >= 3 ? Math.max(5, Math.min(16, keyLengthBits >>> 3)) : 5;
|
|
4665
|
+
const userPad = padPdfPassword(userPassword);
|
|
4666
|
+
let encrypted = rc4Bytes(hash.subarray(0, keyByteLen), userPad);
|
|
4667
|
+
if (revision >= 3) {
|
|
4668
|
+
const derivedKey = new Uint8Array(keyByteLen);
|
|
4669
|
+
for (let iter = 1; iter <= 19; iter++) {
|
|
4670
|
+
for (let k = 0; k < keyByteLen; k++) {
|
|
4671
|
+
derivedKey[k] = hash[k] ^ iter;
|
|
4672
|
+
}
|
|
4673
|
+
encrypted = rc4Bytes(derivedKey, encrypted);
|
|
4674
|
+
}
|
|
4675
|
+
}
|
|
4676
|
+
return encrypted;
|
|
4677
|
+
}
|
|
4678
|
+
function computeFileEncryptionKeyFromPadded(paddedUserPassword, oValue, pFlagsSigned, fileId, revision, keyLengthBits) {
|
|
4679
|
+
const buf = new Uint8Array(32 + oValue.byteLength + 4 + fileId.byteLength);
|
|
4680
|
+
let offset = 0;
|
|
4681
|
+
buf.set(paddedUserPassword.subarray(0, 32), offset);
|
|
4682
|
+
offset += 32;
|
|
4683
|
+
buf.set(oValue, offset);
|
|
4684
|
+
offset += oValue.byteLength;
|
|
4685
|
+
buf[offset++] = pFlagsSigned & 255;
|
|
4686
|
+
buf[offset++] = pFlagsSigned >>> 8 & 255;
|
|
4687
|
+
buf[offset++] = pFlagsSigned >>> 16 & 255;
|
|
4688
|
+
buf[offset++] = pFlagsSigned >>> 24 & 255;
|
|
4689
|
+
buf.set(fileId, offset);
|
|
4690
|
+
let hash = md5Bytes(buf);
|
|
4691
|
+
const keyByteLen = revision >= 3 ? Math.max(5, Math.min(16, keyLengthBits >>> 3)) : 5;
|
|
4692
|
+
if (revision >= 3) {
|
|
4693
|
+
for (let i = 0; i < 50; i++) {
|
|
4694
|
+
hash = md5Bytes(hash.subarray(0, keyByteLen));
|
|
4695
|
+
}
|
|
4696
|
+
}
|
|
4697
|
+
return hash.subarray(0, keyByteLen);
|
|
4698
|
+
}
|
|
4699
|
+
function computeUserPasswordValue(encryptionKey, fileId, revision) {
|
|
4700
|
+
if (revision === 2) {
|
|
4701
|
+
return rc4Bytes(encryptionKey, PDF_PASSWORD_PADDING);
|
|
4702
|
+
}
|
|
4703
|
+
const hashInput = new Uint8Array(32 + fileId.byteLength);
|
|
4704
|
+
hashInput.set(PDF_PASSWORD_PADDING, 0);
|
|
4705
|
+
hashInput.set(fileId, 32);
|
|
4706
|
+
let check = rc4Bytes(encryptionKey, md5Bytes(hashInput));
|
|
4707
|
+
const keyLen = encryptionKey.byteLength;
|
|
4708
|
+
const derivedKey = new Uint8Array(keyLen);
|
|
4709
|
+
for (let iter = 1; iter <= 19; iter++) {
|
|
4710
|
+
for (let k = 0; k < keyLen; k++) {
|
|
4711
|
+
derivedKey[k] = encryptionKey[k] ^ iter;
|
|
4712
|
+
}
|
|
4713
|
+
check = rc4Bytes(derivedKey, check);
|
|
4714
|
+
}
|
|
4715
|
+
const out = new Uint8Array(32);
|
|
4716
|
+
out.set(check.subarray(0, 16), 0);
|
|
4717
|
+
out.set(PDF_PASSWORD_PADDING.subarray(0, 16), 16);
|
|
4718
|
+
return out;
|
|
4719
|
+
}
|
|
4720
|
+
function authenticatePaddedUserPassword(paddedPassword, oValue, uValue, pFlagsSigned, fileId, revision, keyLengthBits) {
|
|
4721
|
+
const candidateKey = computeFileEncryptionKeyFromPadded(
|
|
4722
|
+
paddedPassword,
|
|
4723
|
+
oValue,
|
|
4724
|
+
pFlagsSigned,
|
|
4725
|
+
fileId,
|
|
4726
|
+
revision,
|
|
4727
|
+
keyLengthBits
|
|
4728
|
+
);
|
|
4729
|
+
const expectedU = computeUserPasswordValue(candidateKey, fileId, revision);
|
|
4730
|
+
const compareLen = revision >= 3 ? 16 : 32;
|
|
4731
|
+
if (uValue.byteLength < compareLen) return null;
|
|
4732
|
+
let diff = 0;
|
|
4733
|
+
for (let i = 0; i < compareLen; i++) {
|
|
4734
|
+
diff |= expectedU[i] ^ uValue[i];
|
|
4735
|
+
}
|
|
4736
|
+
return diff === 0 ? candidateKey : null;
|
|
4737
|
+
}
|
|
4738
|
+
function authenticateOwnerPassword(ownerPassword, oValue, uValue, pFlagsSigned, fileId, revision, keyLengthBits) {
|
|
4739
|
+
const ownerPad = padPdfPassword(ownerPassword);
|
|
4740
|
+
let hash = md5Bytes(ownerPad);
|
|
4741
|
+
if (revision >= 3) {
|
|
4742
|
+
for (let i = 0; i < 50; i++) {
|
|
4743
|
+
hash = md5Bytes(hash);
|
|
4744
|
+
}
|
|
4745
|
+
}
|
|
4746
|
+
const keyByteLen = revision >= 3 ? Math.max(5, Math.min(16, keyLengthBits >>> 3)) : 5;
|
|
4747
|
+
let recoveredUserPad;
|
|
4748
|
+
if (revision >= 3) {
|
|
4749
|
+
recoveredUserPad = oValue.slice(0, 32);
|
|
4750
|
+
const derivedKey = new Uint8Array(keyByteLen);
|
|
4751
|
+
for (let iter = 19; iter >= 0; iter--) {
|
|
4752
|
+
for (let k = 0; k < keyByteLen; k++) {
|
|
4753
|
+
derivedKey[k] = hash[k] ^ iter;
|
|
4754
|
+
}
|
|
4755
|
+
recoveredUserPad = rc4Bytes(derivedKey, recoveredUserPad);
|
|
4756
|
+
}
|
|
4757
|
+
} else {
|
|
4758
|
+
recoveredUserPad = rc4Bytes(hash.subarray(0, 5), oValue.subarray(0, 32));
|
|
4759
|
+
}
|
|
4760
|
+
return authenticatePaddedUserPassword(
|
|
4761
|
+
recoveredUserPad,
|
|
4762
|
+
oValue,
|
|
4763
|
+
uValue,
|
|
4764
|
+
pFlagsSigned,
|
|
4765
|
+
fileId,
|
|
4766
|
+
revision,
|
|
4767
|
+
keyLengthBits
|
|
4768
|
+
);
|
|
4769
|
+
}
|
|
4770
|
+
function buildObjectEncryptionKey(encryptionKey, objectNumber, generationNumber) {
|
|
4771
|
+
const n = encryptionKey.byteLength;
|
|
4772
|
+
const buf = new Uint8Array(n + 5);
|
|
4773
|
+
buf.set(encryptionKey, 0);
|
|
4774
|
+
buf[n] = objectNumber & 255;
|
|
4775
|
+
buf[n + 1] = objectNumber >>> 8 & 255;
|
|
4776
|
+
buf[n + 2] = objectNumber >>> 16 & 255;
|
|
4777
|
+
buf[n + 3] = generationNumber & 255;
|
|
4778
|
+
buf[n + 4] = generationNumber >>> 8 & 255;
|
|
4779
|
+
const digest = md5Bytes(buf);
|
|
4780
|
+
return digest.subarray(0, Math.min(n + 5, 16));
|
|
4781
|
+
}
|
|
4782
|
+
function decodePdfLiteralBodyBytes(body) {
|
|
4783
|
+
const bytes = [];
|
|
4784
|
+
for (let i = 0; i < body.length; i++) {
|
|
4785
|
+
const ch = body.charCodeAt(i) & 255;
|
|
4786
|
+
if (ch !== 92) {
|
|
4787
|
+
bytes.push(ch);
|
|
4788
|
+
continue;
|
|
4789
|
+
}
|
|
4790
|
+
if (i + 1 >= body.length) break;
|
|
4791
|
+
const next = body.charCodeAt(++i) & 255;
|
|
4792
|
+
if (next === 13) {
|
|
4793
|
+
if (i + 1 < body.length && (body.charCodeAt(i + 1) & 255) === 10) i++;
|
|
4794
|
+
} else if (next === 10) {
|
|
4795
|
+
} else if (next === 110) {
|
|
4796
|
+
bytes.push(10);
|
|
4797
|
+
} else if (next === 114) {
|
|
4798
|
+
bytes.push(13);
|
|
4799
|
+
} else if (next === 116) {
|
|
4800
|
+
bytes.push(9);
|
|
4801
|
+
} else if (next === 98) {
|
|
4802
|
+
bytes.push(8);
|
|
4803
|
+
} else if (next === 102) {
|
|
4804
|
+
bytes.push(12);
|
|
4805
|
+
} else if (next >= 48 && next <= 55) {
|
|
4806
|
+
let oct = String.fromCharCode(next);
|
|
4807
|
+
for (let k = 0; k < 2 && i + 1 < body.length; k++) {
|
|
4808
|
+
const peek = body.charCodeAt(i + 1) & 255;
|
|
4809
|
+
if (peek >= 48 && peek <= 55) {
|
|
4810
|
+
oct += String.fromCharCode(peek);
|
|
4811
|
+
i++;
|
|
4812
|
+
} else {
|
|
4813
|
+
break;
|
|
4814
|
+
}
|
|
4815
|
+
}
|
|
4816
|
+
bytes.push(parseInt(oct, 8) & 255);
|
|
4817
|
+
} else {
|
|
4818
|
+
bytes.push(next);
|
|
4819
|
+
}
|
|
4820
|
+
}
|
|
4821
|
+
return new Uint8Array(bytes);
|
|
4822
|
+
}
|
|
4823
|
+
function extractPdfStringRawBytes(strObj) {
|
|
4824
|
+
if (strObj instanceof import_pdf_lib.PDFHexString) {
|
|
4825
|
+
return strObj.asBytes();
|
|
4826
|
+
}
|
|
4827
|
+
const serialized = strObj.asString();
|
|
4828
|
+
const inner = serialized.startsWith("(") && serialized.endsWith(")") ? serialized.slice(1, -1) : serialized;
|
|
4829
|
+
return decodePdfLiteralBodyBytes(inner);
|
|
4830
|
+
}
|
|
4831
|
+
function isSignatureDict(dict) {
|
|
4832
|
+
const typeVal = dict.get(import_pdf_lib.PDFName.of("Type"));
|
|
4833
|
+
if (typeVal instanceof import_pdf_lib.PDFName && typeVal.asString() === "/Sig") return true;
|
|
4834
|
+
const ftVal = dict.get(import_pdf_lib.PDFName.of("FT"));
|
|
4835
|
+
if (ftVal instanceof import_pdf_lib.PDFName && ftVal.asString() === "/Sig") return true;
|
|
4836
|
+
return false;
|
|
4837
|
+
}
|
|
4838
|
+
function transformPdfObjectStringsWithRc4(obj, objKey) {
|
|
4839
|
+
if (obj instanceof import_pdf_lib.PDFHexString || obj instanceof import_pdf_lib.PDFString) {
|
|
4840
|
+
const raw = extractPdfStringRawBytes(obj);
|
|
4841
|
+
const transformed = rc4Bytes(objKey, raw);
|
|
4842
|
+
return import_pdf_lib.PDFHexString.of(bytesToHex(transformed));
|
|
4843
|
+
}
|
|
4844
|
+
if (obj instanceof import_pdf_lib.PDFArray) {
|
|
4845
|
+
for (let i = 0; i < obj.size(); i++) {
|
|
4846
|
+
const item = obj.get(i);
|
|
4847
|
+
const next = transformPdfObjectStringsWithRc4(item, objKey);
|
|
4848
|
+
if (next !== item) {
|
|
4849
|
+
obj.set(i, next);
|
|
4850
|
+
}
|
|
4851
|
+
}
|
|
4852
|
+
return obj;
|
|
4853
|
+
}
|
|
4854
|
+
if (obj instanceof import_pdf_lib.PDFDict) {
|
|
4855
|
+
const isSig = isSignatureDict(obj);
|
|
4856
|
+
for (const [key, val] of obj.entries()) {
|
|
4857
|
+
if (isSig && key.asString() === "/Contents") {
|
|
4858
|
+
continue;
|
|
4859
|
+
}
|
|
4860
|
+
const next = transformPdfObjectStringsWithRc4(val, objKey);
|
|
4861
|
+
if (next !== val) {
|
|
4862
|
+
obj.set(key, next);
|
|
4863
|
+
}
|
|
4864
|
+
}
|
|
4865
|
+
return obj;
|
|
4866
|
+
}
|
|
4867
|
+
return obj;
|
|
4868
|
+
}
|
|
4869
|
+
function extractOrCreateDocumentFileId(pdfDoc) {
|
|
4870
|
+
const existingId = pdfDoc.context.trailerInfo.ID;
|
|
4871
|
+
if (existingId) {
|
|
4872
|
+
const idArr = existingId instanceof import_pdf_lib.PDFArray ? existingId : pdfDoc.context.lookupMaybe(existingId, import_pdf_lib.PDFArray);
|
|
4873
|
+
if (idArr && idArr.size() > 0) {
|
|
4874
|
+
const first = idArr.get(0);
|
|
4875
|
+
if (first instanceof import_pdf_lib.PDFHexString || first instanceof import_pdf_lib.PDFString) {
|
|
4876
|
+
const bytes = extractPdfStringRawBytes(first);
|
|
4877
|
+
if (bytes.byteLength > 0) {
|
|
4878
|
+
const hex = bytesToHex(bytes);
|
|
4879
|
+
pdfDoc.context.trailerInfo.ID = pdfDoc.context.obj([
|
|
4880
|
+
import_pdf_lib.PDFHexString.of(hex),
|
|
4881
|
+
import_pdf_lib.PDFHexString.of(hex)
|
|
4882
|
+
]);
|
|
4883
|
+
return bytes;
|
|
4884
|
+
}
|
|
4885
|
+
}
|
|
4886
|
+
}
|
|
4887
|
+
}
|
|
4888
|
+
const seedBytes = new Uint8Array(32);
|
|
4889
|
+
if (typeof globalThis.crypto !== "undefined" && typeof globalThis.crypto.getRandomValues === "function") {
|
|
4890
|
+
globalThis.crypto.getRandomValues(seedBytes);
|
|
4891
|
+
} else {
|
|
4892
|
+
const fallbackText = `univer-pdf-${Date.now()}-${Math.random()}-${pdfDoc.getPageCount()}`;
|
|
4893
|
+
const enc = new TextEncoder().encode(fallbackText);
|
|
4894
|
+
seedBytes.set(enc.subarray(0, Math.min(32, enc.byteLength)));
|
|
4895
|
+
}
|
|
4896
|
+
const fileId = md5Bytes(seedBytes);
|
|
4897
|
+
const idHex = bytesToHex(fileId);
|
|
4898
|
+
pdfDoc.context.trailerInfo.ID = pdfDoc.context.obj([
|
|
4899
|
+
import_pdf_lib.PDFHexString.of(idHex),
|
|
4900
|
+
import_pdf_lib.PDFHexString.of(idHex)
|
|
4901
|
+
]);
|
|
4902
|
+
return fileId;
|
|
4903
|
+
}
|
|
4904
|
+
async function applyPdfSecurityToDocument(pdfDoc, security) {
|
|
4905
|
+
if (security.enabled === false) return;
|
|
4906
|
+
await pdfDoc.flush();
|
|
4907
|
+
const keyLength = security.keyLength === 40 ? 40 : 128;
|
|
4908
|
+
const version = keyLength === 40 ? 1 : 2;
|
|
4909
|
+
const revision = keyLength === 40 ? 2 : 3;
|
|
4910
|
+
const userPassword = security.userPassword ?? "";
|
|
4911
|
+
const ownerPassword = security.ownerPassword && security.ownerPassword.length > 0 ? security.ownerPassword : userPassword.length > 0 ? userPassword : "univer-pdf-owner-guard";
|
|
4912
|
+
const fileId = extractOrCreateDocumentFileId(pdfDoc);
|
|
4913
|
+
const pSigned = encodePdfPermissionFlags(security.permissions, revision);
|
|
4914
|
+
const oBytes = computeOwnerPasswordValue(userPassword, ownerPassword, revision, keyLength);
|
|
4915
|
+
const encryptionKey = computeFileEncryptionKeyFromPadded(
|
|
4916
|
+
padPdfPassword(userPassword),
|
|
4917
|
+
oBytes,
|
|
4918
|
+
pSigned,
|
|
4919
|
+
fileId,
|
|
4920
|
+
revision,
|
|
4921
|
+
keyLength
|
|
4922
|
+
);
|
|
4923
|
+
const uBytes = computeUserPasswordValue(encryptionKey, fileId, revision);
|
|
4924
|
+
const idRef = pdfDoc.context.trailerInfo.ID instanceof import_pdf_lib.PDFRef ? pdfDoc.context.trailerInfo.ID : null;
|
|
4925
|
+
for (const [ref, obj] of pdfDoc.context.enumerateIndirectObjects()) {
|
|
4926
|
+
if (idRef && ref.objectNumber === idRef.objectNumber && ref.generationNumber === idRef.generationNumber) {
|
|
4927
|
+
continue;
|
|
4928
|
+
}
|
|
4929
|
+
const objKey = buildObjectEncryptionKey(encryptionKey, ref.objectNumber, ref.generationNumber);
|
|
4930
|
+
if (obj instanceof import_pdf_lib.PDFRawStream) {
|
|
4931
|
+
transformPdfObjectStringsWithRc4(obj.dict, objKey);
|
|
4932
|
+
const encryptedStream = rc4Bytes(objKey, obj.contents);
|
|
4933
|
+
obj.dict.set(import_pdf_lib.PDFName.of("Length"), import_pdf_lib.PDFNumber.of(encryptedStream.byteLength));
|
|
4934
|
+
pdfDoc.context.assign(ref, import_pdf_lib.PDFRawStream.of(obj.dict, encryptedStream));
|
|
4935
|
+
} else if (obj instanceof import_pdf_lib.PDFStream) {
|
|
4936
|
+
transformPdfObjectStringsWithRc4(obj.dict, objKey);
|
|
4937
|
+
const encodedContents = obj.getContents();
|
|
4938
|
+
const encryptedStream = rc4Bytes(objKey, encodedContents);
|
|
4939
|
+
obj.dict.set(import_pdf_lib.PDFName.of("Length"), import_pdf_lib.PDFNumber.of(encryptedStream.byteLength));
|
|
4940
|
+
pdfDoc.context.assign(ref, import_pdf_lib.PDFRawStream.of(obj.dict, encryptedStream));
|
|
4941
|
+
} else {
|
|
4942
|
+
const next = transformPdfObjectStringsWithRc4(obj, objKey);
|
|
4943
|
+
if (next !== obj) {
|
|
4944
|
+
pdfDoc.context.assign(ref, next);
|
|
4945
|
+
}
|
|
4946
|
+
}
|
|
4947
|
+
}
|
|
4948
|
+
const encryptDict = pdfDoc.context.obj({
|
|
4949
|
+
Filter: import_pdf_lib.PDFName.of("Standard"),
|
|
4950
|
+
V: import_pdf_lib.PDFNumber.of(version),
|
|
4951
|
+
R: import_pdf_lib.PDFNumber.of(revision),
|
|
4952
|
+
Length: import_pdf_lib.PDFNumber.of(keyLength),
|
|
4953
|
+
P: import_pdf_lib.PDFNumber.of(pSigned),
|
|
4954
|
+
O: import_pdf_lib.PDFHexString.of(bytesToHex(oBytes)),
|
|
4955
|
+
U: import_pdf_lib.PDFHexString.of(bytesToHex(uBytes))
|
|
4956
|
+
});
|
|
4957
|
+
const encryptRef = pdfDoc.context.register(encryptDict);
|
|
4958
|
+
pdfDoc.context.trailerInfo.Encrypt = encryptRef;
|
|
4959
|
+
}
|
|
4960
|
+
async function encryptPdfBytes(pdfBytes, security) {
|
|
4961
|
+
if (!security || security.enabled === false) {
|
|
4962
|
+
return pdfBytes.slice();
|
|
4963
|
+
}
|
|
4964
|
+
const cleanBytes = inspectPdfEncryption(pdfBytes).encrypted ? await decryptPdfBytes(pdfBytes, security.ownerPassword || security.userPassword || "") : pdfBytes;
|
|
4965
|
+
const pdfDoc = await import_pdf_lib.PDFDocument.load(cleanBytes, { updateMetadata: false });
|
|
4966
|
+
await applyPdfSecurityToDocument(pdfDoc, security);
|
|
4967
|
+
return await pdfDoc.save({ useObjectStreams: false, updateFieldAppearances: false });
|
|
4968
|
+
}
|
|
4969
|
+
function bytesToLatin1(bytes) {
|
|
4970
|
+
let out = "";
|
|
4971
|
+
const chunk = 32768;
|
|
4972
|
+
for (let i = 0; i < bytes.byteLength; i += chunk) {
|
|
4973
|
+
out += String.fromCharCode(...bytes.subarray(i, i + chunk));
|
|
4974
|
+
}
|
|
4975
|
+
return out;
|
|
4976
|
+
}
|
|
4977
|
+
function extractBalancedPdfDict(text, startIdx) {
|
|
4978
|
+
const openIdx = text.indexOf("<<", startIdx);
|
|
4979
|
+
if (openIdx < 0) return null;
|
|
4980
|
+
let depth = 0;
|
|
4981
|
+
let inLiteral = false;
|
|
4982
|
+
let literalDepth = 0;
|
|
4983
|
+
for (let i = openIdx; i < text.length; i++) {
|
|
4984
|
+
const ch = text.charCodeAt(i);
|
|
4985
|
+
if (inLiteral) {
|
|
4986
|
+
if (ch === 92) {
|
|
4987
|
+
i++;
|
|
4988
|
+
continue;
|
|
4989
|
+
}
|
|
4990
|
+
if (ch === 40) literalDepth++;
|
|
4991
|
+
else if (ch === 41) {
|
|
4992
|
+
literalDepth--;
|
|
4993
|
+
if (literalDepth <= 0) {
|
|
4994
|
+
inLiteral = false;
|
|
4995
|
+
}
|
|
4996
|
+
}
|
|
4997
|
+
continue;
|
|
4998
|
+
}
|
|
4999
|
+
if (ch === 40) {
|
|
5000
|
+
inLiteral = true;
|
|
5001
|
+
literalDepth = 1;
|
|
5002
|
+
continue;
|
|
5003
|
+
}
|
|
5004
|
+
if (ch === 37) {
|
|
5005
|
+
while (i < text.length && text.charCodeAt(i) !== 10 && text.charCodeAt(i) !== 13) {
|
|
5006
|
+
i++;
|
|
5007
|
+
}
|
|
5008
|
+
continue;
|
|
5009
|
+
}
|
|
5010
|
+
if (ch === 60 && text.charCodeAt(i + 1) === 60) {
|
|
5011
|
+
depth++;
|
|
5012
|
+
i++;
|
|
5013
|
+
continue;
|
|
5014
|
+
}
|
|
5015
|
+
if (ch === 62 && text.charCodeAt(i + 1) === 62) {
|
|
5016
|
+
depth--;
|
|
5017
|
+
i++;
|
|
5018
|
+
if (depth === 0) {
|
|
5019
|
+
return text.slice(openIdx, i + 1);
|
|
5020
|
+
}
|
|
5021
|
+
}
|
|
5022
|
+
}
|
|
5023
|
+
return null;
|
|
5024
|
+
}
|
|
5025
|
+
function parsePdfStringTokenAfterKey(dictText, keyName) {
|
|
5026
|
+
const re = new RegExp(`\\/${keyName}(?![A-Za-z0-9_.+-])\\s*`);
|
|
5027
|
+
const match = re.exec(dictText);
|
|
5028
|
+
if (!match) return null;
|
|
5029
|
+
let pos = match.index + match[0].length;
|
|
5030
|
+
while (pos < dictText.length && /\s/.test(dictText[pos])) pos++;
|
|
5031
|
+
if (pos >= dictText.length) return null;
|
|
5032
|
+
if (dictText[pos] === "<" && dictText[pos + 1] !== "<") {
|
|
5033
|
+
const close = dictText.indexOf(">", pos + 1);
|
|
5034
|
+
if (close < 0) return null;
|
|
5035
|
+
return hexToBytes(dictText.slice(pos + 1, close));
|
|
5036
|
+
}
|
|
5037
|
+
if (dictText[pos] === "(") {
|
|
5038
|
+
let depth = 1;
|
|
5039
|
+
let i = pos + 1;
|
|
5040
|
+
while (i < dictText.length && depth > 0) {
|
|
5041
|
+
const ch = dictText.charCodeAt(i);
|
|
5042
|
+
if (ch === 92) {
|
|
5043
|
+
i += 2;
|
|
5044
|
+
continue;
|
|
5045
|
+
}
|
|
5046
|
+
if (ch === 40) depth++;
|
|
5047
|
+
else if (ch === 41) depth--;
|
|
5048
|
+
i++;
|
|
5049
|
+
}
|
|
5050
|
+
if (depth === 0) {
|
|
5051
|
+
return decodePdfLiteralBodyBytes(dictText.slice(pos + 1, i - 1));
|
|
5052
|
+
}
|
|
5053
|
+
}
|
|
5054
|
+
return null;
|
|
5055
|
+
}
|
|
5056
|
+
function parseFirstPdfArrayStringBytes(dictText, keyName) {
|
|
5057
|
+
const re = new RegExp(`\\/${keyName}(?![A-Za-z0-9_.+-])\\s*\\[`);
|
|
5058
|
+
const match = re.exec(dictText);
|
|
5059
|
+
if (!match) return null;
|
|
5060
|
+
let pos = match.index + match[0].length;
|
|
5061
|
+
while (pos < dictText.length && /\s/.test(dictText[pos])) pos++;
|
|
5062
|
+
if (pos >= dictText.length) return null;
|
|
5063
|
+
if (dictText[pos] === "<" && dictText[pos + 1] !== "<") {
|
|
5064
|
+
const close = dictText.indexOf(">", pos + 1);
|
|
5065
|
+
if (close >= 0) return hexToBytes(dictText.slice(pos + 1, close));
|
|
5066
|
+
} else if (dictText[pos] === "(") {
|
|
5067
|
+
let depth = 1;
|
|
5068
|
+
let i = pos + 1;
|
|
5069
|
+
while (i < dictText.length && depth > 0) {
|
|
5070
|
+
const ch = dictText.charCodeAt(i);
|
|
5071
|
+
if (ch === 92) {
|
|
5072
|
+
i += 2;
|
|
5073
|
+
continue;
|
|
5074
|
+
}
|
|
5075
|
+
if (ch === 40) depth++;
|
|
5076
|
+
else if (ch === 41) depth--;
|
|
5077
|
+
i++;
|
|
5078
|
+
}
|
|
5079
|
+
if (depth === 0) {
|
|
5080
|
+
return decodePdfLiteralBodyBytes(dictText.slice(pos + 1, i - 1));
|
|
5081
|
+
}
|
|
5082
|
+
}
|
|
5083
|
+
return null;
|
|
5084
|
+
}
|
|
5085
|
+
function parseRawEncryptFromPdfBytes(pdfBytes) {
|
|
5086
|
+
if (!pdfBytes || pdfBytes.byteLength < 16) return null;
|
|
5087
|
+
const rawText = bytesToLatin1(pdfBytes);
|
|
5088
|
+
let containerDict = null;
|
|
5089
|
+
let searchEnd = rawText.length;
|
|
5090
|
+
while (searchEnd > 0) {
|
|
5091
|
+
const trailerIdx = rawText.lastIndexOf("trailer", searchEnd - 1);
|
|
5092
|
+
if (trailerIdx < 0) break;
|
|
5093
|
+
const prevCh = trailerIdx > 0 ? rawText[trailerIdx - 1] : "\n";
|
|
5094
|
+
const afterCh = rawText[trailerIdx + 7] ?? " ";
|
|
5095
|
+
if (/[\s\r\n>]/.test(prevCh) && /[\s\r\n<]/.test(afterCh)) {
|
|
5096
|
+
const candidate = extractBalancedPdfDict(rawText, trailerIdx + 7);
|
|
5097
|
+
if (candidate && /\/Encrypt\b/.test(candidate)) {
|
|
5098
|
+
containerDict = candidate;
|
|
5099
|
+
break;
|
|
5100
|
+
}
|
|
5101
|
+
if (candidate && !containerDict) {
|
|
5102
|
+
containerDict = candidate;
|
|
5103
|
+
}
|
|
5104
|
+
}
|
|
5105
|
+
searchEnd = trailerIdx;
|
|
5106
|
+
}
|
|
5107
|
+
if (!containerDict || !/\/Encrypt\b/.test(containerDict)) {
|
|
5108
|
+
const xrefMatch = /<<[\s\S]{0,2048}?\/Type\s*\/XRef[\s\S]{0,2048}?>>/.exec(rawText);
|
|
5109
|
+
if (xrefMatch && /\/Encrypt\b/.test(xrefMatch[0])) {
|
|
5110
|
+
containerDict = xrefMatch[0];
|
|
5111
|
+
}
|
|
5112
|
+
}
|
|
5113
|
+
if (!containerDict || !/\/Encrypt\b/.test(containerDict)) {
|
|
5114
|
+
return null;
|
|
5115
|
+
}
|
|
5116
|
+
let encryptDictText = null;
|
|
5117
|
+
const refMatch = /\/Encrypt\s+(\d+)\s+(\d+)\s+R\b/.exec(containerDict);
|
|
5118
|
+
if (refMatch) {
|
|
5119
|
+
const objNum = refMatch[1];
|
|
5120
|
+
const genNum = refMatch[2];
|
|
5121
|
+
const objRe = new RegExp(`(?:^|[\\r\\n\\s])${objNum}\\s+${genNum}\\s+obj\\b`, "g");
|
|
5122
|
+
let lastObjMatch = null;
|
|
5123
|
+
let m;
|
|
5124
|
+
while ((m = objRe.exec(rawText)) !== null) {
|
|
5125
|
+
lastObjMatch = m;
|
|
5126
|
+
}
|
|
5127
|
+
if (lastObjMatch) {
|
|
5128
|
+
encryptDictText = extractBalancedPdfDict(rawText, lastObjMatch.index + lastObjMatch[0].length);
|
|
5129
|
+
}
|
|
5130
|
+
} else if (/\/Encrypt\s*<</.test(containerDict)) {
|
|
5131
|
+
const inlineIdx = containerDict.search(/\/Encrypt\s*<</);
|
|
5132
|
+
encryptDictText = extractBalancedPdfDict(containerDict, inlineIdx);
|
|
5133
|
+
}
|
|
5134
|
+
if (!encryptDictText) return null;
|
|
5135
|
+
const filterMatch = /\/Filter\s*\/([A-Za-z0-9_.+-]+)/.exec(encryptDictText);
|
|
5136
|
+
const vMatch = /\/V\s+(-?\d+)/.exec(encryptDictText);
|
|
5137
|
+
const rMatch = /\/R\s+(-?\d+)/.exec(encryptDictText);
|
|
5138
|
+
const lenMatch = /\/Length\s+(\d+)/.exec(encryptDictText);
|
|
5139
|
+
const pMatch = /\/P\s+(-?\d+)/.exec(encryptDictText);
|
|
5140
|
+
const filter = filterMatch ? filterMatch[1] : "Standard";
|
|
5141
|
+
const version = vMatch ? parseInt(vMatch[1], 10) : 1;
|
|
5142
|
+
const revision = rMatch ? parseInt(rMatch[1], 10) : version >= 2 ? 3 : 2;
|
|
5143
|
+
const keyLength = lenMatch ? parseInt(lenMatch[1], 10) : version >= 2 ? 128 : 40;
|
|
5144
|
+
const pSigned = pMatch ? parseInt(pMatch[1], 10) | 0 : -4;
|
|
5145
|
+
const oBytes = parsePdfStringTokenAfterKey(encryptDictText, "O") ?? new Uint8Array(0);
|
|
5146
|
+
const uBytes = parsePdfStringTokenAfterKey(encryptDictText, "U") ?? new Uint8Array(0);
|
|
5147
|
+
let fileId = parseFirstPdfArrayStringBytes(containerDict, "ID");
|
|
5148
|
+
if (!fileId) {
|
|
5149
|
+
const idRefMatch = /\/ID\s+(\d+)\s+(\d+)\s+R\b/.exec(containerDict);
|
|
5150
|
+
if (idRefMatch) {
|
|
5151
|
+
const idObjRe = new RegExp(`(?:^|[\\r\\n\\s])${idRefMatch[1]}\\s+${idRefMatch[2]}\\s+obj\\b([\\s\\S]{0,512}?)endobj`);
|
|
5152
|
+
const idObjM = idObjRe.exec(rawText);
|
|
5153
|
+
if (idObjM) {
|
|
5154
|
+
fileId = parseFirstPdfArrayStringBytes(`/ID ${idObjM[1]}`, "ID");
|
|
5155
|
+
}
|
|
5156
|
+
}
|
|
5157
|
+
}
|
|
5158
|
+
return {
|
|
5159
|
+
filter,
|
|
5160
|
+
version,
|
|
5161
|
+
revision,
|
|
5162
|
+
keyLength,
|
|
5163
|
+
pSigned,
|
|
5164
|
+
oBytes,
|
|
5165
|
+
uBytes,
|
|
5166
|
+
fileId: fileId ?? new Uint8Array(0)
|
|
5167
|
+
};
|
|
5168
|
+
}
|
|
5169
|
+
function extractEncryptMetadataFromDoc(doc) {
|
|
5170
|
+
const encEntry = doc.context.trailerInfo.Encrypt;
|
|
5171
|
+
if (!encEntry) return null;
|
|
5172
|
+
const encDict = encEntry instanceof import_pdf_lib.PDFDict ? encEntry : doc.context.lookupMaybe(encEntry, import_pdf_lib.PDFDict);
|
|
5173
|
+
if (!encDict) return null;
|
|
5174
|
+
const filterObj = encDict.get(import_pdf_lib.PDFName.of("Filter"));
|
|
5175
|
+
const filter = filterObj instanceof import_pdf_lib.PDFName ? filterObj.decodeText() : "Standard";
|
|
5176
|
+
const vObj = encDict.get(import_pdf_lib.PDFName.of("V"));
|
|
5177
|
+
const version = vObj instanceof import_pdf_lib.PDFNumber ? vObj.asNumber() : 1;
|
|
5178
|
+
const rObj = encDict.get(import_pdf_lib.PDFName.of("R"));
|
|
5179
|
+
const revision = rObj instanceof import_pdf_lib.PDFNumber ? rObj.asNumber() : version >= 2 ? 3 : 2;
|
|
5180
|
+
const lenObj = encDict.get(import_pdf_lib.PDFName.of("Length"));
|
|
5181
|
+
const keyLength = lenObj instanceof import_pdf_lib.PDFNumber ? lenObj.asNumber() : version >= 2 ? 128 : 40;
|
|
5182
|
+
const pObj = encDict.get(import_pdf_lib.PDFName.of("P"));
|
|
5183
|
+
const pSigned = pObj instanceof import_pdf_lib.PDFNumber ? pObj.asNumber() | 0 : -4;
|
|
5184
|
+
const oObj = encDict.get(import_pdf_lib.PDFName.of("O"));
|
|
5185
|
+
const oBytes = oObj instanceof import_pdf_lib.PDFHexString || oObj instanceof import_pdf_lib.PDFString ? extractPdfStringRawBytes(oObj) : new Uint8Array(0);
|
|
5186
|
+
const uObj = encDict.get(import_pdf_lib.PDFName.of("U"));
|
|
5187
|
+
const uBytes = uObj instanceof import_pdf_lib.PDFHexString || uObj instanceof import_pdf_lib.PDFString ? extractPdfStringRawBytes(uObj) : new Uint8Array(0);
|
|
5188
|
+
let fileId = new Uint8Array(0);
|
|
5189
|
+
const idEntry = doc.context.trailerInfo.ID;
|
|
5190
|
+
const idArr = idEntry instanceof import_pdf_lib.PDFArray ? idEntry : doc.context.lookupMaybe(idEntry, import_pdf_lib.PDFArray);
|
|
5191
|
+
if (idArr && idArr.size() > 0) {
|
|
5192
|
+
const first = idArr.get(0);
|
|
5193
|
+
if (first instanceof import_pdf_lib.PDFHexString || first instanceof import_pdf_lib.PDFString) {
|
|
5194
|
+
fileId = extractPdfStringRawBytes(first);
|
|
5195
|
+
}
|
|
5196
|
+
}
|
|
5197
|
+
return {
|
|
5198
|
+
filter,
|
|
5199
|
+
version,
|
|
5200
|
+
revision,
|
|
5201
|
+
keyLength,
|
|
5202
|
+
pSigned,
|
|
5203
|
+
oBytes,
|
|
5204
|
+
uBytes,
|
|
5205
|
+
fileId
|
|
5206
|
+
};
|
|
5207
|
+
}
|
|
5208
|
+
function inspectPdfEncryption(pdfBytes) {
|
|
5209
|
+
const meta = parseRawEncryptFromPdfBytes(pdfBytes);
|
|
5210
|
+
if (!meta) {
|
|
5211
|
+
return {
|
|
5212
|
+
encrypted: false,
|
|
5213
|
+
requiresOpenPassword: false,
|
|
5214
|
+
permissions: { ...DEFAULT_ALL_PERMISSIONS }
|
|
5215
|
+
};
|
|
5216
|
+
}
|
|
5217
|
+
let requiresOpenPassword = true;
|
|
5218
|
+
if (meta.filter === "Standard" && (meta.version === 1 || meta.version === 2) && (meta.revision === 2 || meta.revision === 3) && meta.oBytes.byteLength >= 32 && meta.uBytes.byteLength >= 16) {
|
|
5219
|
+
const emptyKey = authenticatePaddedUserPassword(
|
|
5220
|
+
padPdfPassword(""),
|
|
5221
|
+
meta.oBytes,
|
|
5222
|
+
meta.uBytes,
|
|
5223
|
+
meta.pSigned,
|
|
5224
|
+
meta.fileId,
|
|
5225
|
+
meta.revision,
|
|
5226
|
+
meta.keyLength
|
|
5227
|
+
);
|
|
5228
|
+
requiresOpenPassword = emptyKey === null;
|
|
5229
|
+
}
|
|
5230
|
+
return {
|
|
5231
|
+
encrypted: true,
|
|
5232
|
+
requiresOpenPassword,
|
|
5233
|
+
filter: meta.filter,
|
|
5234
|
+
version: meta.version,
|
|
5235
|
+
revision: meta.revision,
|
|
5236
|
+
keyLength: meta.keyLength,
|
|
5237
|
+
permissionFlagsRaw: meta.pSigned,
|
|
5238
|
+
permissions: decodePdfPermissionFlags(meta.pSigned, meta.revision)
|
|
5239
|
+
};
|
|
5240
|
+
}
|
|
5241
|
+
async function decryptPdfBytes(pdfBytes, password) {
|
|
5242
|
+
let meta = parseRawEncryptFromPdfBytes(pdfBytes);
|
|
5243
|
+
const doc = await import_pdf_lib.PDFDocument.load(pdfBytes, {
|
|
5244
|
+
ignoreEncryption: true,
|
|
5245
|
+
updateMetadata: false
|
|
5246
|
+
});
|
|
5247
|
+
if (!meta && doc.isEncrypted) {
|
|
5248
|
+
meta = extractEncryptMetadataFromDoc(doc);
|
|
5249
|
+
}
|
|
5250
|
+
if (!meta) {
|
|
5251
|
+
return pdfBytes.slice();
|
|
5252
|
+
}
|
|
5253
|
+
if (meta.filter !== "Standard" || meta.version !== 1 && meta.version !== 2 || meta.revision !== 2 && meta.revision !== 3) {
|
|
5254
|
+
throw new PdfSecurityError(
|
|
5255
|
+
"PDF_UNSUPPORTED_ENCRYPTION",
|
|
5256
|
+
`Unsupported PDF encryption handler (Filter=/${meta.filter}, V=${meta.version}, R=${meta.revision}).`
|
|
5257
|
+
);
|
|
5258
|
+
}
|
|
5259
|
+
const rev = meta.revision;
|
|
5260
|
+
const emptyOpenKey = authenticatePaddedUserPassword(
|
|
5261
|
+
padPdfPassword(""),
|
|
5262
|
+
meta.oBytes,
|
|
5263
|
+
meta.uBytes,
|
|
5264
|
+
meta.pSigned,
|
|
5265
|
+
meta.fileId,
|
|
5266
|
+
rev,
|
|
5267
|
+
meta.keyLength
|
|
5268
|
+
);
|
|
5269
|
+
const requiresOpenPassword = emptyOpenKey === null;
|
|
5270
|
+
const candidatePw = password ?? "";
|
|
5271
|
+
if (requiresOpenPassword && candidatePw.length === 0) {
|
|
5272
|
+
throw new PdfSecurityError(
|
|
5273
|
+
"PDF_PASSWORD_REQUIRED",
|
|
5274
|
+
"Encrypted PDF document requires an open password."
|
|
5275
|
+
);
|
|
5276
|
+
}
|
|
5277
|
+
let encryptionKey = null;
|
|
5278
|
+
if (candidatePw.length === 0 && emptyOpenKey) {
|
|
5279
|
+
encryptionKey = emptyOpenKey;
|
|
5280
|
+
} else {
|
|
5281
|
+
encryptionKey = authenticatePaddedUserPassword(
|
|
5282
|
+
padPdfPassword(candidatePw),
|
|
5283
|
+
meta.oBytes,
|
|
5284
|
+
meta.uBytes,
|
|
5285
|
+
meta.pSigned,
|
|
5286
|
+
meta.fileId,
|
|
5287
|
+
rev,
|
|
5288
|
+
meta.keyLength
|
|
5289
|
+
);
|
|
5290
|
+
if (!encryptionKey) {
|
|
5291
|
+
encryptionKey = authenticateOwnerPassword(
|
|
5292
|
+
candidatePw,
|
|
5293
|
+
meta.oBytes,
|
|
5294
|
+
meta.uBytes,
|
|
5295
|
+
meta.pSigned,
|
|
5296
|
+
meta.fileId,
|
|
5297
|
+
rev,
|
|
5298
|
+
meta.keyLength
|
|
5299
|
+
);
|
|
5300
|
+
}
|
|
5301
|
+
}
|
|
5302
|
+
if (!encryptionKey) {
|
|
5303
|
+
throw new PdfSecurityError(
|
|
5304
|
+
"PDF_INVALID_PASSWORD",
|
|
5305
|
+
"Incorrect password for encrypted PDF document."
|
|
5306
|
+
);
|
|
5307
|
+
}
|
|
5308
|
+
const encryptRef = doc.context.trailerInfo.Encrypt instanceof import_pdf_lib.PDFRef ? doc.context.trailerInfo.Encrypt : null;
|
|
5309
|
+
const idRef = doc.context.trailerInfo.ID instanceof import_pdf_lib.PDFRef ? doc.context.trailerInfo.ID : null;
|
|
5310
|
+
for (const [ref, obj] of doc.context.enumerateIndirectObjects()) {
|
|
5311
|
+
if (encryptRef && ref.objectNumber === encryptRef.objectNumber && ref.generationNumber === encryptRef.generationNumber) {
|
|
5312
|
+
continue;
|
|
5313
|
+
}
|
|
5314
|
+
if (idRef && ref.objectNumber === idRef.objectNumber && ref.generationNumber === idRef.generationNumber) {
|
|
5315
|
+
continue;
|
|
5316
|
+
}
|
|
5317
|
+
const objKey = buildObjectEncryptionKey(encryptionKey, ref.objectNumber, ref.generationNumber);
|
|
5318
|
+
if (obj instanceof import_pdf_lib.PDFRawStream) {
|
|
5319
|
+
transformPdfObjectStringsWithRc4(obj.dict, objKey);
|
|
5320
|
+
const decryptedStream = rc4Bytes(objKey, obj.contents);
|
|
5321
|
+
obj.dict.set(import_pdf_lib.PDFName.of("Length"), import_pdf_lib.PDFNumber.of(decryptedStream.byteLength));
|
|
5322
|
+
doc.context.assign(ref, import_pdf_lib.PDFRawStream.of(obj.dict, decryptedStream));
|
|
5323
|
+
} else if (obj instanceof import_pdf_lib.PDFStream) {
|
|
5324
|
+
transformPdfObjectStringsWithRc4(obj.dict, objKey);
|
|
5325
|
+
const decryptedStream = rc4Bytes(objKey, obj.getContents());
|
|
5326
|
+
obj.dict.set(import_pdf_lib.PDFName.of("Length"), import_pdf_lib.PDFNumber.of(decryptedStream.byteLength));
|
|
5327
|
+
doc.context.assign(ref, import_pdf_lib.PDFRawStream.of(obj.dict, decryptedStream));
|
|
5328
|
+
} else {
|
|
5329
|
+
const next = transformPdfObjectStringsWithRc4(obj, objKey);
|
|
5330
|
+
if (next !== obj) {
|
|
5331
|
+
doc.context.assign(ref, next);
|
|
5332
|
+
}
|
|
5333
|
+
}
|
|
5334
|
+
}
|
|
5335
|
+
delete doc.context.trailerInfo.Encrypt;
|
|
5336
|
+
if (encryptRef) {
|
|
5337
|
+
doc.context.delete(encryptRef);
|
|
5338
|
+
}
|
|
5339
|
+
return await doc.save({ useObjectStreams: false, updateFieldAppearances: false });
|
|
5340
|
+
}
|
|
5341
|
+
|
|
5342
|
+
// src/exporter/pdf-native-text.ts
|
|
4189
5343
|
var WIN_ANSI_EXTENDED = {
|
|
4190
5344
|
128: 8364,
|
|
4191
5345
|
130: 8218,
|
|
@@ -4226,17 +5380,17 @@ function multiplyPdfMatrix(m1, m2) {
|
|
|
4226
5380
|
];
|
|
4227
5381
|
}
|
|
4228
5382
|
function decodeSingleStreamObject(doc, objOrRef) {
|
|
4229
|
-
const resolved = objOrRef instanceof
|
|
5383
|
+
const resolved = objOrRef instanceof import_pdf_lib2.PDFRef ? doc.context.lookup(objOrRef) : objOrRef;
|
|
4230
5384
|
if (!resolved) return new Uint8Array(0);
|
|
4231
|
-
if (resolved instanceof
|
|
5385
|
+
if (resolved instanceof import_pdf_lib2.PDFRawStream) {
|
|
4232
5386
|
try {
|
|
4233
|
-
const decoded = (0,
|
|
5387
|
+
const decoded = (0, import_pdf_lib2.decodePDFRawStream)(resolved).decode();
|
|
4234
5388
|
return decoded instanceof Uint8Array ? decoded : new Uint8Array(decoded);
|
|
4235
5389
|
} catch {
|
|
4236
5390
|
return resolved.contents;
|
|
4237
5391
|
}
|
|
4238
5392
|
}
|
|
4239
|
-
if (resolved instanceof
|
|
5393
|
+
if (resolved instanceof import_pdf_lib2.PDFStream) {
|
|
4240
5394
|
const anyStream = resolved;
|
|
4241
5395
|
if (typeof anyStream.getUnencodedContents === "function") {
|
|
4242
5396
|
return anyStream.getUnencodedContents();
|
|
@@ -4248,9 +5402,9 @@ function decodeSingleStreamObject(doc, objOrRef) {
|
|
|
4248
5402
|
return new Uint8Array(0);
|
|
4249
5403
|
}
|
|
4250
5404
|
function decodeContentsEntry(doc, contentsEntry) {
|
|
4251
|
-
const resolved = contentsEntry instanceof
|
|
5405
|
+
const resolved = contentsEntry instanceof import_pdf_lib2.PDFRef ? doc.context.lookup(contentsEntry) : contentsEntry;
|
|
4252
5406
|
if (!resolved) return new Uint8Array(0);
|
|
4253
|
-
if (resolved instanceof
|
|
5407
|
+
if (resolved instanceof import_pdf_lib2.PDFArray) {
|
|
4254
5408
|
const parts = [];
|
|
4255
5409
|
let total = 0;
|
|
4256
5410
|
for (let i = 0; i < resolved.size(); i++) {
|
|
@@ -4345,21 +5499,21 @@ function parseToUnicodeCMap(cmapText) {
|
|
|
4345
5499
|
function extractResourcesFontMap(doc, resourcesDict) {
|
|
4346
5500
|
const fonts = /* @__PURE__ */ new Map();
|
|
4347
5501
|
if (!resourcesDict) return fonts;
|
|
4348
|
-
const fontObj = resourcesDict.get(
|
|
4349
|
-
const fontDict = doc.context.lookupMaybe(fontObj,
|
|
5502
|
+
const fontObj = resourcesDict.get(import_pdf_lib2.PDFName.of("Font"));
|
|
5503
|
+
const fontDict = doc.context.lookupMaybe(fontObj, import_pdf_lib2.PDFDict);
|
|
4350
5504
|
if (!fontDict) return fonts;
|
|
4351
5505
|
for (const [keyObj, valObj] of fontDict.entries()) {
|
|
4352
5506
|
const cleanKey = keyObj.decodeText ? keyObj.decodeText() : keyObj.asString().replace(/^\//, "");
|
|
4353
|
-
const fDict = doc.context.lookupMaybe(valObj,
|
|
5507
|
+
const fDict = doc.context.lookupMaybe(valObj, import_pdf_lib2.PDFDict);
|
|
4354
5508
|
if (!fDict) continue;
|
|
4355
|
-
const subtypeObj = fDict.get(
|
|
4356
|
-
const subtype = subtypeObj instanceof
|
|
4357
|
-
const encObj = fDict.get(
|
|
4358
|
-
const encoding = encObj instanceof
|
|
5509
|
+
const subtypeObj = fDict.get(import_pdf_lib2.PDFName.of("Subtype"));
|
|
5510
|
+
const subtype = subtypeObj instanceof import_pdf_lib2.PDFName ? subtypeObj.decodeText() : "";
|
|
5511
|
+
const encObj = fDict.get(import_pdf_lib2.PDFName.of("Encoding"));
|
|
5512
|
+
const encoding = encObj instanceof import_pdf_lib2.PDFName ? encObj.decodeText() : "";
|
|
4359
5513
|
const isUtf16Cid = /UTF16|UCS2/i.test(encoding);
|
|
4360
5514
|
let toUnicode = /* @__PURE__ */ new Map();
|
|
4361
5515
|
let bytesPerCode = subtype === "Type0" ? 2 : 1;
|
|
4362
|
-
const toUniEntry = fDict.get(
|
|
5516
|
+
const toUniEntry = fDict.get(import_pdf_lib2.PDFName.of("ToUnicode"));
|
|
4363
5517
|
if (toUniEntry) {
|
|
4364
5518
|
const cmapBytes = decodeSingleStreamObject(doc, toUniEntry);
|
|
4365
5519
|
if (cmapBytes.byteLength > 0) {
|
|
@@ -4816,22 +5970,22 @@ function parsePdfContentStreamTextOps(doc, streamBytes, resourcesDict, mediaBox,
|
|
|
4816
5970
|
case "Do": {
|
|
4817
5971
|
const nameTok = operands[operands.length - 1];
|
|
4818
5972
|
if (nameTok?.type === "name" && resourcesDict) {
|
|
4819
|
-
const xObjEntry = resourcesDict.get(
|
|
4820
|
-
const xObjDict = doc.context.lookupMaybe(xObjEntry,
|
|
4821
|
-
const targetObj = xObjDict?.get(
|
|
4822
|
-
const resolvedXObj = targetObj instanceof
|
|
4823
|
-
if (resolvedXObj instanceof
|
|
4824
|
-
const subType = resolvedXObj.dict.get(
|
|
4825
|
-
if (subType instanceof
|
|
5973
|
+
const xObjEntry = resourcesDict.get(import_pdf_lib2.PDFName.of("XObject"));
|
|
5974
|
+
const xObjDict = doc.context.lookupMaybe(xObjEntry, import_pdf_lib2.PDFDict);
|
|
5975
|
+
const targetObj = xObjDict?.get(import_pdf_lib2.PDFName.of(nameTok.value));
|
|
5976
|
+
const resolvedXObj = targetObj instanceof import_pdf_lib2.PDFRef ? doc.context.lookup(targetObj) : targetObj;
|
|
5977
|
+
if (resolvedXObj instanceof import_pdf_lib2.PDFRawStream) {
|
|
5978
|
+
const subType = resolvedXObj.dict.get(import_pdf_lib2.PDFName.of("Subtype"));
|
|
5979
|
+
if (subType instanceof import_pdf_lib2.PDFName && subType.decodeText() === "Form") {
|
|
4826
5980
|
const formBytes = decodeSingleStreamObject(doc, resolvedXObj);
|
|
4827
|
-
const formRes = doc.context.lookupMaybe(resolvedXObj.dict.get(
|
|
5981
|
+
const formRes = doc.context.lookupMaybe(resolvedXObj.dict.get(import_pdf_lib2.PDFName.of("Resources")), import_pdf_lib2.PDFDict) ?? resourcesDict;
|
|
4828
5982
|
let formMat = [...state.ctm];
|
|
4829
|
-
const matArr = doc.context.lookupMaybe(resolvedXObj.dict.get(
|
|
5983
|
+
const matArr = doc.context.lookupMaybe(resolvedXObj.dict.get(import_pdf_lib2.PDFName.of("Matrix")), import_pdf_lib2.PDFArray);
|
|
4830
5984
|
if (matArr && matArr.size() >= 6) {
|
|
4831
5985
|
const nums = [];
|
|
4832
5986
|
for (let k = 0; k < 6; k++) {
|
|
4833
5987
|
const nObj = matArr.get(k);
|
|
4834
|
-
nums.push(nObj instanceof
|
|
5988
|
+
nums.push(nObj instanceof import_pdf_lib2.PDFNumber ? nObj.asNumber() : 0);
|
|
4835
5989
|
}
|
|
4836
5990
|
formMat = multiplyPdfMatrix(
|
|
4837
5991
|
[nums[0], nums[1], nums[2], nums[3], nums[4], nums[5]],
|
|
@@ -4887,12 +6041,12 @@ function stripPdfPageNativeTextInRects(doc, page, stripRects) {
|
|
|
4887
6041
|
}
|
|
4888
6042
|
const newStream = doc.context.flateStream(sanitized);
|
|
4889
6043
|
const newRef = doc.context.register(newStream);
|
|
4890
|
-
page.node.set(
|
|
6044
|
+
page.node.set(import_pdf_lib2.PDFName.of("Contents"), newRef);
|
|
4891
6045
|
return matchingOps.length;
|
|
4892
6046
|
}
|
|
4893
6047
|
|
|
4894
6048
|
// src/exporter/pdf-svg-vector.ts
|
|
4895
|
-
var
|
|
6049
|
+
var import_pdf_lib3 = require("pdf-lib");
|
|
4896
6050
|
function parseObjectPositionRatios(objectPosition) {
|
|
4897
6051
|
if (!objectPosition || !objectPosition.trim()) {
|
|
4898
6052
|
return { ax: 0.5, ayTop: 0.5 };
|
|
@@ -4995,12 +6149,12 @@ function computeElementCenterTransformMatrix(box, angleDeg = 0, flipX = false, f
|
|
|
4995
6149
|
function toPdfLibColor(parsed, colorType) {
|
|
4996
6150
|
if (colorType === "cmyk" || parsed.sourceSpace === "cmyk") {
|
|
4997
6151
|
if (typeof parsed.c === "number" && typeof parsed.m === "number" && typeof parsed.y === "number" && typeof parsed.k === "number") {
|
|
4998
|
-
return (0,
|
|
6152
|
+
return (0, import_pdf_lib3.cmyk)(parsed.c, parsed.m, parsed.y, parsed.k);
|
|
4999
6153
|
}
|
|
5000
6154
|
const conv = rgbToCmyk(parsed.r, parsed.g, parsed.b);
|
|
5001
|
-
return (0,
|
|
6155
|
+
return (0, import_pdf_lib3.cmyk)(conv.c, conv.m, conv.y, conv.k);
|
|
5002
6156
|
}
|
|
5003
|
-
return (0,
|
|
6157
|
+
return (0, import_pdf_lib3.rgb)(parsed.r, parsed.g, parsed.b);
|
|
5004
6158
|
}
|
|
5005
6159
|
function multiplySvgMat(m1, m2) {
|
|
5006
6160
|
return [
|
|
@@ -5536,7 +6690,7 @@ function drawVectorSvgOnPdfPage(pdfPage, pageHeight, left, top, width, height, s
|
|
|
5536
6690
|
const ops = parseSvgToVectorCommands(svgXml, width, height, opacity);
|
|
5537
6691
|
if (ops.length === 0) return 0;
|
|
5538
6692
|
const pdfY = pageHeight - top - height;
|
|
5539
|
-
pdfPage.pushOperators((0,
|
|
6693
|
+
pdfPage.pushOperators((0, import_pdf_lib3.pushGraphicsState)(), (0, import_pdf_lib3.rectangle)(left, pdfY, width, height), (0, import_pdf_lib3.clip)(), (0, import_pdf_lib3.endPath)());
|
|
5540
6694
|
try {
|
|
5541
6695
|
for (const op of ops) {
|
|
5542
6696
|
pdfPage.drawSvgPath(op.pathData, {
|
|
@@ -5551,7 +6705,7 @@ function drawVectorSvgOnPdfPage(pdfPage, pageHeight, left, top, width, height, s
|
|
|
5551
6705
|
});
|
|
5552
6706
|
}
|
|
5553
6707
|
} finally {
|
|
5554
|
-
pdfPage.pushOperators((0,
|
|
6708
|
+
pdfPage.pushOperators((0, import_pdf_lib3.popGraphicsState)());
|
|
5555
6709
|
}
|
|
5556
6710
|
return ops.length;
|
|
5557
6711
|
}
|
|
@@ -5884,8 +7038,8 @@ function registerSubsetType0FontInDoc(pdfDoc, fontBytes, usedCodePoints, fontTag
|
|
|
5884
7038
|
Subtype: "CIDFontType2",
|
|
5885
7039
|
BaseFont: fontTag,
|
|
5886
7040
|
CIDSystemInfo: {
|
|
5887
|
-
Registry:
|
|
5888
|
-
Ordering:
|
|
7041
|
+
Registry: import_pdf_lib4.PDFString.of("Adobe"),
|
|
7042
|
+
Ordering: import_pdf_lib4.PDFString.of("Identity"),
|
|
5889
7043
|
Supplement: 0
|
|
5890
7044
|
},
|
|
5891
7045
|
FontDescriptor: fontDescriptorRef,
|
|
@@ -6281,8 +7435,8 @@ function initVectorCjkEmbedState(pdfDoc, snapshot, effectiveData, options, preRe
|
|
|
6281
7435
|
Subtype: "CIDFontType2",
|
|
6282
7436
|
BaseFont: "STSong-Light",
|
|
6283
7437
|
CIDSystemInfo: {
|
|
6284
|
-
Registry:
|
|
6285
|
-
Ordering:
|
|
7438
|
+
Registry: import_pdf_lib4.PDFString.of("Adobe"),
|
|
7439
|
+
Ordering: import_pdf_lib4.PDFString.of("GB1"),
|
|
6286
7440
|
Supplement: 4
|
|
6287
7441
|
},
|
|
6288
7442
|
FontDescriptor: fontDescriptorRef,
|
|
@@ -6474,25 +7628,25 @@ function drawVectorCjkTextBox(page, pageHeight, text, left, top, width, height,
|
|
|
6474
7628
|
}
|
|
6475
7629
|
const fillOp = colorType === "cmyk" ? (() => {
|
|
6476
7630
|
const cm = rgbToCmyk(color.r, color.g, color.b);
|
|
6477
|
-
return (0,
|
|
6478
|
-
})() : (0,
|
|
7631
|
+
return (0, import_pdf_lib4.setFillingCmykColor)(cm.c, cm.m, cm.y, cm.k);
|
|
7632
|
+
})() : (0, import_pdf_lib4.setFillingRgbColor)(color.r, color.g, color.b);
|
|
6479
7633
|
const syntheticBoldOps = wantBold && !usedDedicatedBold ? [
|
|
6480
|
-
|
|
6481
|
-
(0,
|
|
7634
|
+
import_pdf_lib4.PDFOperator.of("Tr", [import_pdf_lib4.PDFNumber.of(2)]),
|
|
7635
|
+
(0, import_pdf_lib4.setLineWidth)(Math.max(0.25, fontSize * 0.032)),
|
|
6482
7636
|
colorType === "cmyk" ? (() => {
|
|
6483
7637
|
const cm = rgbToCmyk(color.r, color.g, color.b);
|
|
6484
|
-
return (0,
|
|
6485
|
-
})() : (0,
|
|
7638
|
+
return (0, import_pdf_lib4.setStrokingCmykColor)(cm.c, cm.m, cm.y, cm.k);
|
|
7639
|
+
})() : (0, import_pdf_lib4.setStrokingRgbColor)(color.r, color.g, color.b)
|
|
6486
7640
|
] : [];
|
|
6487
7641
|
const decoCol = toPdfLibColor(color, colorType);
|
|
6488
7642
|
const decoThickness = Math.max(0.6, fontSize * 0.065);
|
|
6489
7643
|
const applyClip = !noWrapSingleLine && width > 0 && height > 0;
|
|
6490
7644
|
if (applyClip) {
|
|
6491
7645
|
page.pushOperators(
|
|
6492
|
-
(0,
|
|
6493
|
-
(0,
|
|
6494
|
-
(0,
|
|
6495
|
-
(0,
|
|
7646
|
+
(0, import_pdf_lib4.pushGraphicsState)(),
|
|
7647
|
+
(0, import_pdf_lib4.rectangle)(left, pdfY, width, height),
|
|
7648
|
+
(0, import_pdf_lib4.clip)(),
|
|
7649
|
+
(0, import_pdf_lib4.endPath)()
|
|
6496
7650
|
);
|
|
6497
7651
|
}
|
|
6498
7652
|
for (let i = 0; i < lines.length; i++) {
|
|
@@ -6520,25 +7674,25 @@ function drawVectorCjkTextBox(page, pageHeight, text, left, top, width, height,
|
|
|
6520
7674
|
}
|
|
6521
7675
|
const hexStr = encodeVectorCjkLineHex(line, state, activeSubset);
|
|
6522
7676
|
const totalTc = charSpacing + extraJustifyTc;
|
|
6523
|
-
const posOp = style?.italic ?
|
|
6524
|
-
|
|
6525
|
-
|
|
6526
|
-
|
|
6527
|
-
|
|
6528
|
-
|
|
6529
|
-
|
|
6530
|
-
]) : (0,
|
|
7677
|
+
const posOp = style?.italic ? import_pdf_lib4.PDFOperator.of("Tm", [
|
|
7678
|
+
import_pdf_lib4.PDFNumber.of(1),
|
|
7679
|
+
import_pdf_lib4.PDFNumber.of(0),
|
|
7680
|
+
import_pdf_lib4.PDFNumber.of(0.2126),
|
|
7681
|
+
import_pdf_lib4.PDFNumber.of(1),
|
|
7682
|
+
import_pdf_lib4.PDFNumber.of(drawX),
|
|
7683
|
+
import_pdf_lib4.PDFNumber.of(drawY)
|
|
7684
|
+
]) : (0, import_pdf_lib4.moveText)(drawX, drawY);
|
|
6531
7685
|
page.pushOperators(
|
|
6532
|
-
(0,
|
|
6533
|
-
(0,
|
|
7686
|
+
(0, import_pdf_lib4.pushGraphicsState)(),
|
|
7687
|
+
(0, import_pdf_lib4.beginText)(),
|
|
6534
7688
|
fillOp,
|
|
6535
7689
|
...syntheticBoldOps,
|
|
6536
|
-
(0,
|
|
6537
|
-
...totalTc !== 0 ? [(0,
|
|
7690
|
+
(0, import_pdf_lib4.setFontAndSize)(fontKey, fontSize),
|
|
7691
|
+
...totalTc !== 0 ? [(0, import_pdf_lib4.setCharacterSpacing)(totalTc)] : [],
|
|
6538
7692
|
posOp,
|
|
6539
|
-
(0,
|
|
6540
|
-
(0,
|
|
6541
|
-
(0,
|
|
7693
|
+
(0, import_pdf_lib4.showText)(import_pdf_lib4.PDFHexString.of(hexStr)),
|
|
7694
|
+
(0, import_pdf_lib4.endText)(),
|
|
7695
|
+
(0, import_pdf_lib4.popGraphicsState)()
|
|
6542
7696
|
);
|
|
6543
7697
|
if (style?.underline) {
|
|
6544
7698
|
const ulY = drawY - fontSize * 0.12;
|
|
@@ -6562,7 +7716,7 @@ function drawVectorCjkTextBox(page, pageHeight, text, left, top, width, height,
|
|
|
6562
7716
|
}
|
|
6563
7717
|
}
|
|
6564
7718
|
if (applyClip) {
|
|
6565
|
-
page.pushOperators((0,
|
|
7719
|
+
page.pushOperators((0, import_pdf_lib4.popGraphicsState)());
|
|
6566
7720
|
}
|
|
6567
7721
|
}
|
|
6568
7722
|
function renderTextToPngBytesInBrowser(text, widthPt, heightPt, style) {
|
|
@@ -6880,21 +8034,21 @@ var STANDARD_FONT_CACHE = /* @__PURE__ */ new WeakMap();
|
|
|
6880
8034
|
function resolveStandardPdfFont(fontFamily, bold, italic) {
|
|
6881
8035
|
const fam = (fontFamily ?? "").toLowerCase();
|
|
6882
8036
|
if (fam.includes("courier") || fam.includes("mono") || fam.includes("consolas")) {
|
|
6883
|
-
if (bold && italic) return
|
|
6884
|
-
if (bold) return
|
|
6885
|
-
if (italic) return
|
|
6886
|
-
return
|
|
8037
|
+
if (bold && italic) return import_pdf_lib4.StandardFonts.CourierBoldOblique;
|
|
8038
|
+
if (bold) return import_pdf_lib4.StandardFonts.CourierBold;
|
|
8039
|
+
if (italic) return import_pdf_lib4.StandardFonts.CourierOblique;
|
|
8040
|
+
return import_pdf_lib4.StandardFonts.Courier;
|
|
6887
8041
|
}
|
|
6888
8042
|
if ((fam.includes("times") || fam.includes("serif") || fam.includes("georgia")) && !fam.includes("sans")) {
|
|
6889
|
-
if (bold && italic) return
|
|
6890
|
-
if (bold) return
|
|
6891
|
-
if (italic) return
|
|
6892
|
-
return
|
|
8043
|
+
if (bold && italic) return import_pdf_lib4.StandardFonts.TimesRomanBoldItalic;
|
|
8044
|
+
if (bold) return import_pdf_lib4.StandardFonts.TimesRomanBold;
|
|
8045
|
+
if (italic) return import_pdf_lib4.StandardFonts.TimesRomanItalic;
|
|
8046
|
+
return import_pdf_lib4.StandardFonts.TimesRoman;
|
|
6893
8047
|
}
|
|
6894
|
-
if (bold && italic) return
|
|
6895
|
-
if (bold) return
|
|
6896
|
-
if (italic) return
|
|
6897
|
-
return
|
|
8048
|
+
if (bold && italic) return import_pdf_lib4.StandardFonts.HelveticaBoldOblique;
|
|
8049
|
+
if (bold) return import_pdf_lib4.StandardFonts.HelveticaBold;
|
|
8050
|
+
if (italic) return import_pdf_lib4.StandardFonts.HelveticaOblique;
|
|
8051
|
+
return import_pdf_lib4.StandardFonts.Helvetica;
|
|
6898
8052
|
}
|
|
6899
8053
|
async function getOrEmbedStandardFont(pdfDoc, fontName) {
|
|
6900
8054
|
let docMap = STANDARD_FONT_CACHE.get(pdfDoc);
|
|
@@ -7037,10 +8191,10 @@ async function drawTextBox(pdfDoc, page, pageHeight, text, left, top, width, hei
|
|
|
7037
8191
|
const applyAsciiClip = !extraOpts?.noWrapSingleLine && bgWidth > 0 && height > 0;
|
|
7038
8192
|
if (applyAsciiClip) {
|
|
7039
8193
|
page.pushOperators(
|
|
7040
|
-
(0,
|
|
7041
|
-
(0,
|
|
7042
|
-
(0,
|
|
7043
|
-
(0,
|
|
8194
|
+
(0, import_pdf_lib4.pushGraphicsState)(),
|
|
8195
|
+
(0, import_pdf_lib4.rectangle)(left, pdfY, bgWidth, height),
|
|
8196
|
+
(0, import_pdf_lib4.clip)(),
|
|
8197
|
+
(0, import_pdf_lib4.endPath)()
|
|
7044
8198
|
);
|
|
7045
8199
|
}
|
|
7046
8200
|
for (let i = 0; i < wrappedLines.length; i++) {
|
|
@@ -7073,10 +8227,10 @@ async function drawTextBox(pdfDoc, page, pageHeight, text, left, top, width, hei
|
|
|
7073
8227
|
}
|
|
7074
8228
|
const totalTc = charSpacing + extraJustifyTc;
|
|
7075
8229
|
if (totalTc !== 0 || extraWordSpacing !== 0) {
|
|
7076
|
-
const ops = [(0,
|
|
7077
|
-
if (totalTc !== 0) ops.push((0,
|
|
8230
|
+
const ops = [(0, import_pdf_lib4.pushGraphicsState)()];
|
|
8231
|
+
if (totalTc !== 0) ops.push((0, import_pdf_lib4.setCharacterSpacing)(totalTc));
|
|
7078
8232
|
if (extraWordSpacing !== 0) {
|
|
7079
|
-
ops.push(
|
|
8233
|
+
ops.push(import_pdf_lib4.PDFOperator.of("Tw", [import_pdf_lib4.PDFNumber.of(extraWordSpacing)]));
|
|
7080
8234
|
}
|
|
7081
8235
|
page.pushOperators(...ops);
|
|
7082
8236
|
page.drawText(lineText, {
|
|
@@ -7087,7 +8241,7 @@ async function drawTextBox(pdfDoc, page, pageHeight, text, left, top, width, hei
|
|
|
7087
8241
|
color: pdfCol,
|
|
7088
8242
|
opacity: color.a
|
|
7089
8243
|
});
|
|
7090
|
-
page.pushOperators((0,
|
|
8244
|
+
page.pushOperators((0, import_pdf_lib4.popGraphicsState)());
|
|
7091
8245
|
} else {
|
|
7092
8246
|
page.drawText(lineText, {
|
|
7093
8247
|
x: drawX,
|
|
@@ -7120,7 +8274,7 @@ async function drawTextBox(pdfDoc, page, pageHeight, text, left, top, width, hei
|
|
|
7120
8274
|
}
|
|
7121
8275
|
}
|
|
7122
8276
|
if (applyAsciiClip) {
|
|
7123
|
-
page.pushOperators((0,
|
|
8277
|
+
page.pushOperators((0, import_pdf_lib4.popGraphicsState)());
|
|
7124
8278
|
}
|
|
7125
8279
|
}
|
|
7126
8280
|
function parseNumericCellValue(rawValue) {
|
|
@@ -7832,7 +8986,7 @@ async function drawRichOrColumnTextBox(pdfDoc, page, pageHeight, spans, left, to
|
|
|
7832
8986
|
const key = `${code ? "C" : "S"}-${bold ? "B" : "N"}-${italic ? "I" : "R"}`;
|
|
7833
8987
|
const existing = fontCache.get(key);
|
|
7834
8988
|
if (existing) return existing;
|
|
7835
|
-
const stdName = code ? bold && italic ?
|
|
8989
|
+
const stdName = code ? bold && italic ? import_pdf_lib4.StandardFonts.CourierBoldOblique : bold ? import_pdf_lib4.StandardFonts.CourierBold : italic ? import_pdf_lib4.StandardFonts.CourierOblique : import_pdf_lib4.StandardFonts.Courier : bold && italic ? import_pdf_lib4.StandardFonts.HelveticaBoldOblique : bold ? import_pdf_lib4.StandardFonts.HelveticaBold : italic ? import_pdf_lib4.StandardFonts.HelveticaOblique : import_pdf_lib4.StandardFonts.Helvetica;
|
|
7836
8990
|
const embedded = await pdfDoc.embedFont(stdName);
|
|
7837
8991
|
fontCache.set(key, embedded);
|
|
7838
8992
|
return embedded;
|
|
@@ -7885,34 +9039,34 @@ async function drawRichOrColumnTextBox(pdfDoc, page, pageHeight, spans, left, to
|
|
|
7885
9039
|
const hexStr = encodeVectorCjkLineHex(seg.text, cjkState, activeSubset);
|
|
7886
9040
|
const fillOp = colorType === "cmyk" ? (() => {
|
|
7887
9041
|
const cm = rgbToCmyk(col.r, col.g, col.b);
|
|
7888
|
-
return (0,
|
|
7889
|
-
})() : (0,
|
|
9042
|
+
return (0, import_pdf_lib4.setFillingCmykColor)(cm.c, cm.m, cm.y, cm.k);
|
|
9043
|
+
})() : (0, import_pdf_lib4.setFillingRgbColor)(col.r, col.g, col.b);
|
|
7890
9044
|
const boldOps = seg.bold && !usedDedicatedBold ? [
|
|
7891
|
-
|
|
7892
|
-
(0,
|
|
9045
|
+
import_pdf_lib4.PDFOperator.of("Tr", [import_pdf_lib4.PDFNumber.of(2)]),
|
|
9046
|
+
(0, import_pdf_lib4.setLineWidth)(Math.max(0.25, seg.fontSize * 0.032)),
|
|
7893
9047
|
colorType === "cmyk" ? (() => {
|
|
7894
9048
|
const cm = rgbToCmyk(col.r, col.g, col.b);
|
|
7895
|
-
return (0,
|
|
7896
|
-
})() : (0,
|
|
9049
|
+
return (0, import_pdf_lib4.setStrokingCmykColor)(cm.c, cm.m, cm.y, cm.k);
|
|
9050
|
+
})() : (0, import_pdf_lib4.setStrokingRgbColor)(col.r, col.g, col.b)
|
|
7897
9051
|
] : [];
|
|
7898
|
-
const posOp = seg.italic ?
|
|
7899
|
-
|
|
7900
|
-
|
|
7901
|
-
|
|
7902
|
-
|
|
7903
|
-
|
|
7904
|
-
|
|
7905
|
-
]) : (0,
|
|
9052
|
+
const posOp = seg.italic ? import_pdf_lib4.PDFOperator.of("Tm", [
|
|
9053
|
+
import_pdf_lib4.PDFNumber.of(1),
|
|
9054
|
+
import_pdf_lib4.PDFNumber.of(0),
|
|
9055
|
+
import_pdf_lib4.PDFNumber.of(0.2126),
|
|
9056
|
+
import_pdf_lib4.PDFNumber.of(1),
|
|
9057
|
+
import_pdf_lib4.PDFNumber.of(segX),
|
|
9058
|
+
import_pdf_lib4.PDFNumber.of(baselineY)
|
|
9059
|
+
]) : (0, import_pdf_lib4.moveText)(segX, baselineY);
|
|
7906
9060
|
page.pushOperators(
|
|
7907
|
-
(0,
|
|
7908
|
-
(0,
|
|
9061
|
+
(0, import_pdf_lib4.pushGraphicsState)(),
|
|
9062
|
+
(0, import_pdf_lib4.beginText)(),
|
|
7909
9063
|
fillOp,
|
|
7910
9064
|
...boldOps,
|
|
7911
|
-
(0,
|
|
9065
|
+
(0, import_pdf_lib4.setFontAndSize)(fontKey, seg.fontSize),
|
|
7912
9066
|
posOp,
|
|
7913
|
-
(0,
|
|
7914
|
-
(0,
|
|
7915
|
-
(0,
|
|
9067
|
+
(0, import_pdf_lib4.showText)(import_pdf_lib4.PDFHexString.of(hexStr)),
|
|
9068
|
+
(0, import_pdf_lib4.endText)(),
|
|
9069
|
+
(0, import_pdf_lib4.popGraphicsState)()
|
|
7916
9070
|
);
|
|
7917
9071
|
} else if (hasNonAscii(seg.text)) {
|
|
7918
9072
|
const pngBytes = renderTextToPngBytesInBrowser(seg.text, Math.max(8, seg.widthPt + 4), seg.fontSize * 1.35, {
|
|
@@ -8052,10 +9206,11 @@ function attachPdfOutlinesTree(pdfDoc, bookmarks) {
|
|
|
8052
9206
|
const selfRef = itemRefs[i];
|
|
8053
9207
|
const clampedPageIdx = Math.max(0, Math.min(allPages.length - 1, Math.floor(item.pageIndex || 0)));
|
|
8054
9208
|
const targetPage = allPages[clampedPageIdx];
|
|
8055
|
-
const
|
|
8056
|
-
const
|
|
9209
|
+
const effectiveTopOffset = item.topPt ?? item.topOffsetPt;
|
|
9210
|
+
const destY = effectiveTopOffset !== void 0 ? Math.round(Math.max(0, targetPage.getHeight() - effectiveTopOffset) * 100) / 100 : null;
|
|
9211
|
+
const destArray = pdfDoc.context.obj([targetPage.ref, import_pdf_lib4.PDFName.of("XYZ"), null, destY, null]);
|
|
8057
9212
|
const dictObj = {
|
|
8058
|
-
Title:
|
|
9213
|
+
Title: import_pdf_lib4.PDFHexString.fromText(item.title || "Bookmark"),
|
|
8059
9214
|
Parent: parentRef,
|
|
8060
9215
|
Dest: destArray
|
|
8061
9216
|
};
|
|
@@ -8088,15 +9243,15 @@ function attachPdfOutlinesTree(pdfDoc, bookmarks) {
|
|
|
8088
9243
|
Count: rootChain.totalCount
|
|
8089
9244
|
})
|
|
8090
9245
|
);
|
|
8091
|
-
pdfDoc.catalog.set(
|
|
9246
|
+
pdfDoc.catalog.set(import_pdf_lib4.PDFName.of("Outlines"), outlinesRef);
|
|
8092
9247
|
}
|
|
8093
9248
|
function ensurePagePropertiesDict(pdfDoc, pdfPage) {
|
|
8094
9249
|
pdfPage.node.normalize();
|
|
8095
9250
|
const res = pdfPage.node.Resources();
|
|
8096
|
-
let props = pdfDoc.context.lookupMaybe(res.get(
|
|
9251
|
+
let props = pdfDoc.context.lookupMaybe(res.get(import_pdf_lib4.PDFName.of("Properties")), import_pdf_lib4.PDFDict);
|
|
8097
9252
|
if (!props) {
|
|
8098
9253
|
props = pdfDoc.context.obj({});
|
|
8099
|
-
res.set(
|
|
9254
|
+
res.set(import_pdf_lib4.PDFName.of("Properties"), props);
|
|
8100
9255
|
}
|
|
8101
9256
|
return props;
|
|
8102
9257
|
}
|
|
@@ -8112,11 +9267,11 @@ function attachOptionalContentLayers(pdfDoc, layers) {
|
|
|
8112
9267
|
const isPrint = layer.printable !== false;
|
|
8113
9268
|
const ocgDict = pdfDoc.context.obj({
|
|
8114
9269
|
Type: "OCG",
|
|
8115
|
-
Name:
|
|
8116
|
-
Intent: [
|
|
9270
|
+
Name: import_pdf_lib4.PDFHexString.fromText(layer.name || layer.id),
|
|
9271
|
+
Intent: [import_pdf_lib4.PDFName.of("View"), import_pdf_lib4.PDFName.of("Design")],
|
|
8117
9272
|
Usage: {
|
|
8118
|
-
View: { ViewState:
|
|
8119
|
-
Print: { PrintState:
|
|
9273
|
+
View: { ViewState: import_pdf_lib4.PDFName.of(isVis ? "ON" : "OFF") },
|
|
9274
|
+
Print: { PrintState: import_pdf_lib4.PDFName.of(isPrint ? "ON" : "OFF") }
|
|
8120
9275
|
}
|
|
8121
9276
|
});
|
|
8122
9277
|
const ocgRef = pdfDoc.context.register(ocgDict);
|
|
@@ -8131,11 +9286,11 @@ function attachOptionalContentLayers(pdfDoc, layers) {
|
|
|
8131
9286
|
});
|
|
8132
9287
|
});
|
|
8133
9288
|
pdfDoc.catalog.set(
|
|
8134
|
-
|
|
9289
|
+
import_pdf_lib4.PDFName.of("OCProperties"),
|
|
8135
9290
|
pdfDoc.context.obj({
|
|
8136
9291
|
OCGs: allRefs,
|
|
8137
9292
|
D: {
|
|
8138
|
-
BaseState:
|
|
9293
|
+
BaseState: import_pdf_lib4.PDFName.of("ON"),
|
|
8139
9294
|
ON: onRefs,
|
|
8140
9295
|
OFF: offRefs,
|
|
8141
9296
|
Order: allRefs,
|
|
@@ -8154,7 +9309,7 @@ function attachEmbeddedFileAttachments(pdfDoc, attachments) {
|
|
|
8154
9309
|
const subtypeSafe = (att.mimeType || "application/octet-stream").replace(/\//g, "#2F");
|
|
8155
9310
|
const efStream = pdfDoc.context.flateStream(fileBytes, {
|
|
8156
9311
|
Type: "EmbeddedFile",
|
|
8157
|
-
Subtype:
|
|
9312
|
+
Subtype: import_pdf_lib4.PDFName.of(subtypeSafe),
|
|
8158
9313
|
Params: {
|
|
8159
9314
|
Size: fileBytes.byteLength
|
|
8160
9315
|
}
|
|
@@ -8162,30 +9317,30 @@ function attachEmbeddedFileAttachments(pdfDoc, attachments) {
|
|
|
8162
9317
|
const efRef = pdfDoc.context.register(efStream);
|
|
8163
9318
|
const filespecObj = {
|
|
8164
9319
|
Type: "Filespec",
|
|
8165
|
-
F:
|
|
8166
|
-
UF:
|
|
9320
|
+
F: import_pdf_lib4.PDFString.of((att.fileName || "attachment.bin").replace(/[^\x20-\x7E]/g, "_")),
|
|
9321
|
+
UF: import_pdf_lib4.PDFHexString.fromText(att.fileName || "attachment.bin"),
|
|
8167
9322
|
EF: {
|
|
8168
9323
|
F: efRef,
|
|
8169
9324
|
UF: efRef
|
|
8170
9325
|
}
|
|
8171
9326
|
};
|
|
8172
9327
|
if (att.description) {
|
|
8173
|
-
filespecObj.Desc =
|
|
9328
|
+
filespecObj.Desc = import_pdf_lib4.PDFHexString.fromText(att.description);
|
|
8174
9329
|
}
|
|
8175
9330
|
const filespecRef = pdfDoc.context.register(pdfDoc.context.obj(filespecObj));
|
|
8176
|
-
namesArr.push(
|
|
9331
|
+
namesArr.push(import_pdf_lib4.PDFHexString.fromText(att.fileName || "attachment.bin"), filespecRef);
|
|
8177
9332
|
}
|
|
8178
9333
|
const efTreeRef = pdfDoc.context.register(
|
|
8179
9334
|
pdfDoc.context.obj({
|
|
8180
9335
|
Names: namesArr
|
|
8181
9336
|
})
|
|
8182
9337
|
);
|
|
8183
|
-
const existingNames = pdfDoc.context.lookupMaybe(pdfDoc.catalog.get(
|
|
9338
|
+
const existingNames = pdfDoc.context.lookupMaybe(pdfDoc.catalog.get(import_pdf_lib4.PDFName.of("Names")), import_pdf_lib4.PDFDict);
|
|
8184
9339
|
if (existingNames) {
|
|
8185
|
-
existingNames.set(
|
|
9340
|
+
existingNames.set(import_pdf_lib4.PDFName.of("EmbeddedFiles"), efTreeRef);
|
|
8186
9341
|
} else {
|
|
8187
9342
|
pdfDoc.catalog.set(
|
|
8188
|
-
|
|
9343
|
+
import_pdf_lib4.PDFName.of("Names"),
|
|
8189
9344
|
pdfDoc.context.obj({
|
|
8190
9345
|
EmbeddedFiles: efTreeRef
|
|
8191
9346
|
})
|
|
@@ -8229,7 +9384,7 @@ function attachPdfAComplianceMetadata(pdfDoc, snapshot, includePdfUa) {
|
|
|
8229
9384
|
Length: xmpBytes.byteLength
|
|
8230
9385
|
});
|
|
8231
9386
|
const metadataRef = pdfDoc.context.register(metadataStream);
|
|
8232
|
-
pdfDoc.catalog.set(
|
|
9387
|
+
pdfDoc.catalog.set(import_pdf_lib4.PDFName.of("Metadata"), metadataRef);
|
|
8233
9388
|
const srgbHeaderBytes = new TextEncoder().encode("sRGB IEC61966-2.1 ICC Output Profile");
|
|
8234
9389
|
const iccStream = pdfDoc.context.flateStream(srgbHeaderBytes, {
|
|
8235
9390
|
N: 3,
|
|
@@ -8240,13 +9395,13 @@ function attachPdfAComplianceMetadata(pdfDoc, snapshot, includePdfUa) {
|
|
|
8240
9395
|
pdfDoc.context.obj({
|
|
8241
9396
|
Type: "OutputIntent",
|
|
8242
9397
|
S: "GTS_PDFA1",
|
|
8243
|
-
OutputConditionIdentifier:
|
|
8244
|
-
RegistryName:
|
|
8245
|
-
Info:
|
|
9398
|
+
OutputConditionIdentifier: import_pdf_lib4.PDFString.of("sRGB IEC61966-2.1"),
|
|
9399
|
+
RegistryName: import_pdf_lib4.PDFString.of("http://www.color.org"),
|
|
9400
|
+
Info: import_pdf_lib4.PDFString.of("sRGB IEC61966-2.1"),
|
|
8246
9401
|
DestOutputProfile: iccRef
|
|
8247
9402
|
})
|
|
8248
9403
|
);
|
|
8249
|
-
pdfDoc.catalog.set(
|
|
9404
|
+
pdfDoc.catalog.set(import_pdf_lib4.PDFName.of("OutputIntents"), pdfDoc.context.obj([outputIntentRef]));
|
|
8250
9405
|
}
|
|
8251
9406
|
function resolveElementStructRole(el) {
|
|
8252
9407
|
switch (el.type) {
|
|
@@ -8278,9 +9433,9 @@ function resolveElementStructRole(el) {
|
|
|
8278
9433
|
}
|
|
8279
9434
|
}
|
|
8280
9435
|
function attachTaggedStructTreeRoot(pdfDoc, snapshot, items, totalPages) {
|
|
8281
|
-
pdfDoc.catalog.set(
|
|
8282
|
-
pdfDoc.catalog.set(
|
|
8283
|
-
pdfDoc.catalog.set(
|
|
9436
|
+
pdfDoc.catalog.set(import_pdf_lib4.PDFName.of("Lang"), import_pdf_lib4.PDFString.of(snapshot.locale ? String(snapshot.locale) : "zh-CN"));
|
|
9437
|
+
pdfDoc.catalog.set(import_pdf_lib4.PDFName.of("MarkInfo"), pdfDoc.context.obj({ Marked: import_pdf_lib4.PDFBool.True }));
|
|
9438
|
+
pdfDoc.catalog.set(import_pdf_lib4.PDFName.of("ViewerPreferences"), pdfDoc.context.obj({ DisplayDocTitle: import_pdf_lib4.PDFBool.True }));
|
|
8284
9439
|
if (!snapshot.metadata?.sanitized && snapshot.name) {
|
|
8285
9440
|
pdfDoc.setTitle(snapshot.name);
|
|
8286
9441
|
}
|
|
@@ -8292,16 +9447,16 @@ function attachTaggedStructTreeRoot(pdfDoc, snapshot, items, totalPages) {
|
|
|
8292
9447
|
const elemRef = pdfDoc.context.nextRef();
|
|
8293
9448
|
const elemDict = {
|
|
8294
9449
|
Type: "StructElem",
|
|
8295
|
-
S:
|
|
9450
|
+
S: import_pdf_lib4.PDFName.of(item.role),
|
|
8296
9451
|
P: docElemRef,
|
|
8297
9452
|
Pg: item.pageRef,
|
|
8298
|
-
K:
|
|
9453
|
+
K: import_pdf_lib4.PDFNumber.of(item.mcid)
|
|
8299
9454
|
};
|
|
8300
9455
|
if (item.altText) {
|
|
8301
|
-
elemDict.Alt =
|
|
9456
|
+
elemDict.Alt = import_pdf_lib4.PDFHexString.fromText(item.altText);
|
|
8302
9457
|
}
|
|
8303
9458
|
if (item.title) {
|
|
8304
|
-
elemDict.T =
|
|
9459
|
+
elemDict.T = import_pdf_lib4.PDFHexString.fromText(item.title);
|
|
8305
9460
|
}
|
|
8306
9461
|
pdfDoc.context.assign(elemRef, pdfDoc.context.obj(elemDict));
|
|
8307
9462
|
childElemRefs.push(elemRef);
|
|
@@ -8316,7 +9471,7 @@ function attachTaggedStructTreeRoot(pdfDoc, snapshot, items, totalPages) {
|
|
|
8316
9471
|
docElemRef,
|
|
8317
9472
|
pdfDoc.context.obj({
|
|
8318
9473
|
Type: "StructElem",
|
|
8319
|
-
S:
|
|
9474
|
+
S: import_pdf_lib4.PDFName.of("Document"),
|
|
8320
9475
|
P: structTreeRootRef,
|
|
8321
9476
|
K: childElemRefs
|
|
8322
9477
|
})
|
|
@@ -8324,7 +9479,7 @@ function attachTaggedStructTreeRoot(pdfDoc, snapshot, items, totalPages) {
|
|
|
8324
9479
|
const numsList = [];
|
|
8325
9480
|
for (let p = 0; p < totalPages; p++) {
|
|
8326
9481
|
const refsForPage = (byPageMcid.get(p) ?? []).filter(Boolean);
|
|
8327
|
-
numsList.push(
|
|
9482
|
+
numsList.push(import_pdf_lib4.PDFNumber.of(p), pdfDoc.context.obj(refsForPage));
|
|
8328
9483
|
}
|
|
8329
9484
|
const parentTreeRef = pdfDoc.context.register(pdfDoc.context.obj({ Nums: numsList }));
|
|
8330
9485
|
pdfDoc.context.assign(
|
|
@@ -8336,14 +9491,14 @@ function attachTaggedStructTreeRoot(pdfDoc, snapshot, items, totalPages) {
|
|
|
8336
9491
|
ParentTreeNextKey: totalPages
|
|
8337
9492
|
})
|
|
8338
9493
|
);
|
|
8339
|
-
pdfDoc.catalog.set(
|
|
9494
|
+
pdfDoc.catalog.set(import_pdf_lib4.PDFName.of("StructTreeRoot"), structTreeRootRef);
|
|
8340
9495
|
}
|
|
8341
9496
|
function pruneUnreferencedObjectsAndCompressStreams(pdfDoc) {
|
|
8342
9497
|
for (const [ref, obj] of pdfDoc.context.enumerateIndirectObjects()) {
|
|
8343
|
-
if (obj instanceof
|
|
8344
|
-
const typeName = obj.dict.get(
|
|
8345
|
-
const isMetadata = typeName instanceof
|
|
8346
|
-
const hasFilter = Boolean(obj.dict.get(
|
|
9498
|
+
if (obj instanceof import_pdf_lib4.PDFRawStream) {
|
|
9499
|
+
const typeName = obj.dict.get(import_pdf_lib4.PDFName.of("Type"));
|
|
9500
|
+
const isMetadata = typeName instanceof import_pdf_lib4.PDFName && typeName.decodeText() === "Metadata";
|
|
9501
|
+
const hasFilter = Boolean(obj.dict.get(import_pdf_lib4.PDFName.of("Filter")));
|
|
8347
9502
|
if (!isMetadata && !hasFilter && obj.contents.byteLength > 64) {
|
|
8348
9503
|
const compressed = pdfDoc.context.flateStream(obj.contents);
|
|
8349
9504
|
for (const [k, v] of obj.dict.entries()) {
|
|
@@ -8360,26 +9515,26 @@ function pruneUnreferencedObjectsAndCompressStreams(pdfDoc) {
|
|
|
8360
9515
|
const refToString = (r) => `${r.objectNumber}:${r.generationNumber}`;
|
|
8361
9516
|
const visit = (node) => {
|
|
8362
9517
|
if (!node) return;
|
|
8363
|
-
if (node instanceof
|
|
9518
|
+
if (node instanceof import_pdf_lib4.PDFRef) {
|
|
8364
9519
|
const key = refToString(node);
|
|
8365
9520
|
if (reachable.has(key)) return;
|
|
8366
9521
|
reachable.add(key);
|
|
8367
9522
|
visit(pdfDoc.context.lookup(node));
|
|
8368
9523
|
return;
|
|
8369
9524
|
}
|
|
8370
|
-
if (node instanceof
|
|
9525
|
+
if (node instanceof import_pdf_lib4.PDFDict) {
|
|
8371
9526
|
for (const [, val] of node.entries()) {
|
|
8372
9527
|
visit(val);
|
|
8373
9528
|
}
|
|
8374
9529
|
return;
|
|
8375
9530
|
}
|
|
8376
|
-
if (node instanceof
|
|
9531
|
+
if (node instanceof import_pdf_lib4.PDFArray) {
|
|
8377
9532
|
for (let i = 0; i < node.size(); i++) {
|
|
8378
9533
|
visit(node.get(i));
|
|
8379
9534
|
}
|
|
8380
9535
|
return;
|
|
8381
9536
|
}
|
|
8382
|
-
if (node instanceof
|
|
9537
|
+
if (node instanceof import_pdf_lib4.PDFRawStream || node instanceof import_pdf_lib4.PDFStream) {
|
|
8383
9538
|
visit(node.dict);
|
|
8384
9539
|
}
|
|
8385
9540
|
};
|
|
@@ -8410,30 +9565,30 @@ async function drawInvisibleSearchableOcrSpans(pdfDoc, pdfPage, pageHeight, span
|
|
|
8410
9565
|
const { fontKey } = getOrRegisterPageCjkFontKey(pdfPage, localCjkState);
|
|
8411
9566
|
const hexStr = encodeVectorCjkLineHex(txt, localCjkState);
|
|
8412
9567
|
pdfPage.pushOperators(
|
|
8413
|
-
(0,
|
|
8414
|
-
(0,
|
|
8415
|
-
|
|
8416
|
-
(0,
|
|
8417
|
-
(0,
|
|
8418
|
-
(0,
|
|
8419
|
-
(0,
|
|
8420
|
-
(0,
|
|
9568
|
+
(0, import_pdf_lib4.pushGraphicsState)(),
|
|
9569
|
+
(0, import_pdf_lib4.beginText)(),
|
|
9570
|
+
import_pdf_lib4.PDFOperator.of("Tr", [import_pdf_lib4.PDFNumber.of(3)]),
|
|
9571
|
+
(0, import_pdf_lib4.setFontAndSize)(fontKey, fs),
|
|
9572
|
+
(0, import_pdf_lib4.moveText)(span.left, baselineY),
|
|
9573
|
+
(0, import_pdf_lib4.showText)(import_pdf_lib4.PDFHexString.of(hexStr)),
|
|
9574
|
+
(0, import_pdf_lib4.endText)(),
|
|
9575
|
+
(0, import_pdf_lib4.popGraphicsState)()
|
|
8421
9576
|
);
|
|
8422
9577
|
} else {
|
|
8423
9578
|
if (!helvFont) {
|
|
8424
|
-
helvFont = await pdfDoc.embedFont(
|
|
9579
|
+
helvFont = await pdfDoc.embedFont(import_pdf_lib4.StandardFonts.Helvetica);
|
|
8425
9580
|
}
|
|
8426
9581
|
const fontKey = pdfPage.node.newFontDictionary("FOcr", helvFont.ref);
|
|
8427
9582
|
const asciiHex = Array.from(txt).map((c) => (c.charCodeAt(0) & 255).toString(16).padStart(2, "0").toUpperCase()).join("");
|
|
8428
9583
|
pdfPage.pushOperators(
|
|
8429
|
-
(0,
|
|
8430
|
-
(0,
|
|
8431
|
-
|
|
8432
|
-
(0,
|
|
8433
|
-
(0,
|
|
8434
|
-
(0,
|
|
8435
|
-
(0,
|
|
8436
|
-
(0,
|
|
9584
|
+
(0, import_pdf_lib4.pushGraphicsState)(),
|
|
9585
|
+
(0, import_pdf_lib4.beginText)(),
|
|
9586
|
+
import_pdf_lib4.PDFOperator.of("Tr", [import_pdf_lib4.PDFNumber.of(3)]),
|
|
9587
|
+
(0, import_pdf_lib4.setFontAndSize)(fontKey, fs),
|
|
9588
|
+
(0, import_pdf_lib4.moveText)(span.left, baselineY),
|
|
9589
|
+
(0, import_pdf_lib4.showText)(import_pdf_lib4.PDFHexString.of(asciiHex)),
|
|
9590
|
+
(0, import_pdf_lib4.endText)(),
|
|
9591
|
+
(0, import_pdf_lib4.popGraphicsState)()
|
|
8437
9592
|
);
|
|
8438
9593
|
}
|
|
8439
9594
|
}
|
|
@@ -8502,7 +9657,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
8502
9657
|
const optimizeSize = Boolean(options?.optimizeSize);
|
|
8503
9658
|
const preResolvedFonts = await resolveRenderFontsForExport(snapshot, effectiveData, options);
|
|
8504
9659
|
throwIfExportAborted();
|
|
8505
|
-
const pdfDoc = await
|
|
9660
|
+
const pdfDoc = await import_pdf_lib4.PDFDocument.create();
|
|
8506
9661
|
if (snapshot.metadata?.sanitized) {
|
|
8507
9662
|
pdfDoc.setTitle("");
|
|
8508
9663
|
pdfDoc.setAuthor("");
|
|
@@ -8541,7 +9696,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
8541
9696
|
};
|
|
8542
9697
|
const ensureAcroHelvetica = async () => {
|
|
8543
9698
|
if (!acroHelveticaFont) {
|
|
8544
|
-
acroHelveticaFont = await getOrEmbedStandardFont(pdfDoc,
|
|
9699
|
+
acroHelveticaFont = await getOrEmbedStandardFont(pdfDoc, import_pdf_lib4.StandardFonts.Helvetica);
|
|
8545
9700
|
}
|
|
8546
9701
|
return acroHelveticaFont;
|
|
8547
9702
|
};
|
|
@@ -8702,7 +9857,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
8702
9857
|
if (!excludeBasePdf && snapshot.sourceDocumentRef?.dataUri) {
|
|
8703
9858
|
try {
|
|
8704
9859
|
const srcBytes = dataUriToBytes(snapshot.sourceDocumentRef.dataUri);
|
|
8705
|
-
sourcePdfDoc = await
|
|
9860
|
+
sourcePdfDoc = await import_pdf_lib4.PDFDocument.load(srcBytes);
|
|
8706
9861
|
} catch {
|
|
8707
9862
|
sourcePdfDoc = null;
|
|
8708
9863
|
}
|
|
@@ -8916,21 +10071,21 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
8916
10071
|
const [copied] = await pdfDoc.copyPages(sourcePdfDoc, [pageMeta.sourcePageNumber - 1]);
|
|
8917
10072
|
pdfPage = pdfDoc.addPage(copied);
|
|
8918
10073
|
if (snapshot.metadata?.importedAcroFormBase) {
|
|
8919
|
-
const existingAnnots = pdfDoc.context.lookupMaybe(pdfPage.node.get(
|
|
10074
|
+
const existingAnnots = pdfDoc.context.lookupMaybe(pdfPage.node.get(import_pdf_lib4.PDFName.of("Annots")), import_pdf_lib4.PDFArray);
|
|
8920
10075
|
if (existingAnnots) {
|
|
8921
10076
|
const filtered = [];
|
|
8922
10077
|
for (let ai = 0; ai < existingAnnots.size(); ai++) {
|
|
8923
10078
|
const aRef = existingAnnots.get(ai);
|
|
8924
|
-
const aDict = pdfDoc.context.lookupMaybe(aRef,
|
|
8925
|
-
const sub = aDict?.get(
|
|
8926
|
-
if (!(sub instanceof
|
|
10079
|
+
const aDict = pdfDoc.context.lookupMaybe(aRef, import_pdf_lib4.PDFDict);
|
|
10080
|
+
const sub = aDict?.get(import_pdf_lib4.PDFName.of("Subtype"));
|
|
10081
|
+
if (!(sub instanceof import_pdf_lib4.PDFName && sub.decodeText() === "Widget")) {
|
|
8927
10082
|
filtered.push(aRef);
|
|
8928
10083
|
}
|
|
8929
10084
|
}
|
|
8930
10085
|
if (filtered.length > 0) {
|
|
8931
|
-
pdfPage.node.set(
|
|
10086
|
+
pdfPage.node.set(import_pdf_lib4.PDFName.of("Annots"), pdfDoc.context.obj(filtered));
|
|
8932
10087
|
} else {
|
|
8933
|
-
pdfPage.node.delete(
|
|
10088
|
+
pdfPage.node.delete(import_pdf_lib4.PDFName.of("Annots"));
|
|
8934
10089
|
}
|
|
8935
10090
|
}
|
|
8936
10091
|
}
|
|
@@ -8941,7 +10096,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
8941
10096
|
let size = pageMeta.pageSize ?? snapshot.document.pageSize;
|
|
8942
10097
|
if (excludeBasePdf && snapshot.sourceDocumentRef?.dataUri && !pageMeta.pageSize) {
|
|
8943
10098
|
try {
|
|
8944
|
-
const tempSrc = await
|
|
10099
|
+
const tempSrc = await import_pdf_lib4.PDFDocument.load(dataUriToBytes(snapshot.sourceDocumentRef.dataUri));
|
|
8945
10100
|
const pIdx0 = Math.max(0, (pageMeta.sourcePageNumber ?? 1) - 1);
|
|
8946
10101
|
if (pIdx0 < tempSrc.getPageCount()) {
|
|
8947
10102
|
const refSz = tempSrc.getPage(pIdx0).getSize();
|
|
@@ -8958,19 +10113,21 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
8958
10113
|
y: 0,
|
|
8959
10114
|
width: size.width,
|
|
8960
10115
|
height: size.height,
|
|
8961
|
-
color: (0,
|
|
10116
|
+
color: (0, import_pdf_lib4.rgb)(bgColor.r, bgColor.g, bgColor.b)
|
|
8962
10117
|
});
|
|
8963
10118
|
}
|
|
8964
10119
|
}
|
|
8965
10120
|
const pageHeight = pdfPage.getHeight();
|
|
8966
10121
|
const pageWidth = pdfPage.getWidth();
|
|
8967
10122
|
if (pageMeta.rotation !== void 0) {
|
|
8968
|
-
pdfPage.setRotation((0,
|
|
10123
|
+
pdfPage.setRotation((0, import_pdf_lib4.degrees)(pageMeta.rotation));
|
|
8969
10124
|
}
|
|
8970
10125
|
if (pageMeta.cropBox) {
|
|
8971
|
-
const cb = pageMeta.cropBox;
|
|
8972
|
-
|
|
8973
|
-
|
|
10126
|
+
const cb = resolvePdfCropBoxPt(pageMeta.cropBox);
|
|
10127
|
+
if (cb) {
|
|
10128
|
+
const cropY = Math.max(0, pageHeight - cb.top - cb.height);
|
|
10129
|
+
pdfPage.setCropBox(cb.left, cropY, cb.width, cb.height);
|
|
10130
|
+
}
|
|
8974
10131
|
}
|
|
8975
10132
|
if (isCopiedBasePage && suppressedNativeRects.length > 0) {
|
|
8976
10133
|
for (const sRect of suppressedNativeRects) {
|
|
@@ -8979,14 +10136,14 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
8979
10136
|
y: pageHeight - sRect.top - sRect.height,
|
|
8980
10137
|
width: sRect.width,
|
|
8981
10138
|
height: sRect.height,
|
|
8982
|
-
color: (0,
|
|
10139
|
+
color: (0, import_pdf_lib4.rgb)(1, 1, 1),
|
|
8983
10140
|
borderWidth: 0
|
|
8984
10141
|
});
|
|
8985
10142
|
}
|
|
8986
10143
|
}
|
|
8987
10144
|
if (accessibilityTagged) {
|
|
8988
|
-
pdfPage.node.set(
|
|
8989
|
-
pdfPage.node.set(
|
|
10145
|
+
pdfPage.node.set(import_pdf_lib4.PDFName.of("StructParents"), import_pdf_lib4.PDFNumber.of(pIndex));
|
|
10146
|
+
pdfPage.node.set(import_pdf_lib4.PDFName.of("Tabs"), import_pdf_lib4.PDFName.of("S"));
|
|
8990
10147
|
}
|
|
8991
10148
|
let nextPageMcid = 0;
|
|
8992
10149
|
const activeDynamicRange = planItem.dynamicSplitRanges ? planItem.dynamicSplitRanges[chunkIndex] : void 0;
|
|
@@ -9041,9 +10198,9 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9041
10198
|
let hasOpenCenterTransform = false;
|
|
9042
10199
|
if (ocgEntry) {
|
|
9043
10200
|
const propsDict = ensurePagePropertiesDict(pdfDoc, pdfPage);
|
|
9044
|
-
propsDict.set(
|
|
10201
|
+
propsDict.set(import_pdf_lib4.PDFName.of(ocgEntry.propName), ocgEntry.ocgRef);
|
|
9045
10202
|
pdfPage.pushOperators(
|
|
9046
|
-
|
|
10203
|
+
import_pdf_lib4.PDFOperator.of("BDC", [import_pdf_lib4.PDFName.of("OC"), import_pdf_lib4.PDFName.of(ocgEntry.propName)])
|
|
9047
10204
|
);
|
|
9048
10205
|
openBdcCount++;
|
|
9049
10206
|
}
|
|
@@ -9051,10 +10208,10 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9051
10208
|
const mcid = nextPageMcid++;
|
|
9052
10209
|
const mcPropName = `MC_${mcid}`;
|
|
9053
10210
|
const propsDict = ensurePagePropertiesDict(pdfDoc, pdfPage);
|
|
9054
|
-
propsDict.set(
|
|
10211
|
+
propsDict.set(import_pdf_lib4.PDFName.of(mcPropName), pdfDoc.context.obj({ MCID: import_pdf_lib4.PDFNumber.of(mcid) }));
|
|
9055
10212
|
const role = resolveElementStructRole(el);
|
|
9056
10213
|
pdfPage.pushOperators(
|
|
9057
|
-
|
|
10214
|
+
import_pdf_lib4.PDFOperator.of("BDC", [import_pdf_lib4.PDFName.of(role), import_pdf_lib4.PDFName.of(mcPropName)])
|
|
9058
10215
|
);
|
|
9059
10216
|
openBdcCount++;
|
|
9060
10217
|
taggedStructItems.push({
|
|
@@ -9103,8 +10260,8 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9103
10260
|
) : null;
|
|
9104
10261
|
if (centerMatrix) {
|
|
9105
10262
|
pdfPage.pushOperators(
|
|
9106
|
-
(0,
|
|
9107
|
-
(0,
|
|
10263
|
+
(0, import_pdf_lib4.pushGraphicsState)(),
|
|
10264
|
+
(0, import_pdf_lib4.concatTransformationMatrix)(
|
|
9108
10265
|
centerMatrix[0],
|
|
9109
10266
|
centerMatrix[1],
|
|
9110
10267
|
centerMatrix[2],
|
|
@@ -9270,60 +10427,123 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9270
10427
|
{ colorType: elColorType }
|
|
9271
10428
|
);
|
|
9272
10429
|
} else if (el.type === "stamp" /* STAMP */) {
|
|
9273
|
-
|
|
9274
|
-
|
|
9275
|
-
|
|
9276
|
-
|
|
9277
|
-
|
|
9278
|
-
|
|
10430
|
+
if (el.svgContent) {
|
|
10431
|
+
drawVectorSvgOnPdfPage(
|
|
10432
|
+
pdfPage,
|
|
10433
|
+
pageHeight,
|
|
10434
|
+
left,
|
|
10435
|
+
top,
|
|
9279
10436
|
width,
|
|
9280
10437
|
height,
|
|
9281
|
-
|
|
9282
|
-
|
|
9283
|
-
|
|
9284
|
-
|
|
10438
|
+
el.svgContent,
|
|
10439
|
+
el.opacity ?? 0.9,
|
|
10440
|
+
elColorType
|
|
10441
|
+
);
|
|
10442
|
+
const stampTexts = extractSvgTextDescriptors(el.svgContent, width, height);
|
|
10443
|
+
for (const td of stampTexts) {
|
|
10444
|
+
try {
|
|
10445
|
+
await drawTextBox(
|
|
10446
|
+
pdfDoc,
|
|
10447
|
+
pdfPage,
|
|
10448
|
+
pageHeight,
|
|
10449
|
+
td.text,
|
|
10450
|
+
left + td.leftPt,
|
|
10451
|
+
top + td.topPt,
|
|
10452
|
+
td.widthPt,
|
|
10453
|
+
td.heightPt,
|
|
10454
|
+
{
|
|
10455
|
+
fontSize: td.fontSizePt,
|
|
10456
|
+
fontWeight: td.bold ? "bold" : "normal",
|
|
10457
|
+
color: td.color,
|
|
10458
|
+
textAlign: td.align,
|
|
10459
|
+
verticalAlign: "middle",
|
|
10460
|
+
padding: 0
|
|
10461
|
+
},
|
|
10462
|
+
cjkState,
|
|
10463
|
+
{ noWrapSingleLine: true, colorType: elColorType }
|
|
10464
|
+
);
|
|
10465
|
+
} catch {
|
|
10466
|
+
await drawTextBox(
|
|
10467
|
+
pdfDoc,
|
|
10468
|
+
pdfPage,
|
|
10469
|
+
pageHeight,
|
|
10470
|
+
td.text,
|
|
10471
|
+
left + td.leftPt,
|
|
10472
|
+
top + td.topPt,
|
|
10473
|
+
td.widthPt,
|
|
10474
|
+
td.heightPt,
|
|
10475
|
+
{
|
|
10476
|
+
fontSize: td.fontSizePt,
|
|
10477
|
+
fontWeight: td.bold ? "bold" : "normal",
|
|
10478
|
+
color: td.color,
|
|
10479
|
+
textAlign: td.align,
|
|
10480
|
+
verticalAlign: "middle",
|
|
10481
|
+
padding: 0
|
|
10482
|
+
},
|
|
10483
|
+
null,
|
|
10484
|
+
{ noWrapSingleLine: true, colorType: elColorType }
|
|
10485
|
+
);
|
|
10486
|
+
}
|
|
10487
|
+
}
|
|
9285
10488
|
} else {
|
|
9286
|
-
|
|
9287
|
-
|
|
9288
|
-
|
|
9289
|
-
|
|
9290
|
-
|
|
9291
|
-
|
|
9292
|
-
|
|
9293
|
-
|
|
9294
|
-
|
|
9295
|
-
|
|
9296
|
-
|
|
9297
|
-
|
|
9298
|
-
|
|
9299
|
-
|
|
9300
|
-
|
|
9301
|
-
|
|
9302
|
-
|
|
9303
|
-
|
|
9304
|
-
|
|
9305
|
-
|
|
10489
|
+
const sealColor = parseHexColor(el.color) ?? { r: 0.86, g: 0.15, b: 0.15, a: 1 };
|
|
10490
|
+
const sealPdfCol = toPdfLibColor(sealColor, elColorType);
|
|
10491
|
+
if (el.stampShape === "rect" || el.stampShape === "pill") {
|
|
10492
|
+
pdfPage.drawRectangle({
|
|
10493
|
+
x: left,
|
|
10494
|
+
y: pdfY,
|
|
10495
|
+
width,
|
|
10496
|
+
height,
|
|
10497
|
+
borderColor: sealPdfCol,
|
|
10498
|
+
borderWidth: 2,
|
|
10499
|
+
opacity: (el.opacity ?? 0.85) * sealColor.a
|
|
10500
|
+
});
|
|
10501
|
+
} else {
|
|
10502
|
+
pdfPage.drawEllipse({
|
|
10503
|
+
x: left + width / 2,
|
|
10504
|
+
y: pdfY + height / 2,
|
|
10505
|
+
xScale: width / 2,
|
|
10506
|
+
yScale: height / 2,
|
|
10507
|
+
borderColor: sealPdfCol,
|
|
10508
|
+
borderWidth: 2,
|
|
10509
|
+
opacity: (el.opacity ?? 0.85) * sealColor.a
|
|
10510
|
+
});
|
|
10511
|
+
pdfPage.drawEllipse({
|
|
10512
|
+
x: left + width / 2,
|
|
10513
|
+
y: pdfY + height / 2,
|
|
10514
|
+
xScale: Math.max(4, width / 2 - 3.5),
|
|
10515
|
+
yScale: Math.max(4, height / 2 - 3.5),
|
|
10516
|
+
borderColor: sealPdfCol,
|
|
10517
|
+
borderWidth: 0.75,
|
|
10518
|
+
opacity: (el.opacity ?? 0.85) * sealColor.a
|
|
10519
|
+
});
|
|
10520
|
+
}
|
|
10521
|
+
const signerFooter = [el.signer, el.dateText].filter(Boolean).join(" \xB7 ");
|
|
10522
|
+
const baseStampText = el.secondaryText ? `${el.primaryText}
|
|
9306
10523
|
\u2605 ${el.secondaryText}` : el.primaryText;
|
|
9307
|
-
|
|
9308
|
-
|
|
9309
|
-
|
|
9310
|
-
|
|
9311
|
-
|
|
9312
|
-
|
|
9313
|
-
|
|
9314
|
-
|
|
9315
|
-
|
|
9316
|
-
|
|
9317
|
-
|
|
9318
|
-
|
|
9319
|
-
|
|
9320
|
-
|
|
9321
|
-
|
|
9322
|
-
|
|
9323
|
-
|
|
9324
|
-
|
|
9325
|
-
|
|
9326
|
-
|
|
10524
|
+
const stampText = signerFooter ? `${baseStampText}
|
|
10525
|
+
${signerFooter}` : baseStampText;
|
|
10526
|
+
await drawTextBox(
|
|
10527
|
+
pdfDoc,
|
|
10528
|
+
pdfPage,
|
|
10529
|
+
pageHeight,
|
|
10530
|
+
stampText,
|
|
10531
|
+
left + 6,
|
|
10532
|
+
top + 6,
|
|
10533
|
+
width - 12,
|
|
10534
|
+
height - 12,
|
|
10535
|
+
{
|
|
10536
|
+
fontSize: 10,
|
|
10537
|
+
fontWeight: "bold",
|
|
10538
|
+
color: el.color ?? "#dc2626",
|
|
10539
|
+
textAlign: "center",
|
|
10540
|
+
verticalAlign: "middle",
|
|
10541
|
+
lineHeight: 1.35
|
|
10542
|
+
},
|
|
10543
|
+
cjkState,
|
|
10544
|
+
{ colorType: elColorType }
|
|
10545
|
+
);
|
|
10546
|
+
}
|
|
9327
10547
|
} else if (el.type === "ink" /* INK */) {
|
|
9328
10548
|
const stroke2 = parseHexColor(el.strokeColor) ?? { r: 0.94, g: 0.27, b: 0.27, a: 1 };
|
|
9329
10549
|
const strokeCol = toPdfLibColor(stroke2, elColorType);
|
|
@@ -9384,9 +10604,9 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9384
10604
|
if (isMultiline) {
|
|
9385
10605
|
tf.enableMultiline();
|
|
9386
10606
|
}
|
|
9387
|
-
if (textEl.style?.textAlign === "center") tf.setAlignment(
|
|
9388
|
-
else if (textEl.style?.textAlign === "right") tf.setAlignment(
|
|
9389
|
-
else tf.setAlignment(
|
|
10607
|
+
if (textEl.style?.textAlign === "center") tf.setAlignment(import_pdf_lib4.TextAlignment.Center);
|
|
10608
|
+
else if (textEl.style?.textAlign === "right") tf.setAlignment(import_pdf_lib4.TextAlignment.Right);
|
|
10609
|
+
else tf.setAlignment(import_pdf_lib4.TextAlignment.Left);
|
|
9390
10610
|
const bg = parseHexColor(textEl.style?.backgroundColor) ?? { r: 1, g: 1, b: 1, a: 1 };
|
|
9391
10611
|
const border = parseHexColor(textEl.style?.borderColor) ?? { r: 0.75, g: 0.82, b: 0.9, a: 1 };
|
|
9392
10612
|
const txtCol = parseHexColor(textEl.style?.color) ?? { r: 0.12, g: 0.14, b: 0.16, a: 1 };
|
|
@@ -9397,11 +10617,11 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9397
10617
|
y: pdfY,
|
|
9398
10618
|
width,
|
|
9399
10619
|
height,
|
|
9400
|
-
rotate: (0,
|
|
10620
|
+
rotate: (0, import_pdf_lib4.degrees)(rotDeg),
|
|
9401
10621
|
borderWidth: textEl.style?.borderWidth ?? 1,
|
|
9402
|
-
borderColor: (0,
|
|
9403
|
-
backgroundColor: (0,
|
|
9404
|
-
textColor: (0,
|
|
10622
|
+
borderColor: (0, import_pdf_lib4.rgb)(border.r, border.g, border.b),
|
|
10623
|
+
backgroundColor: (0, import_pdf_lib4.rgb)(bg.r, bg.g, bg.b),
|
|
10624
|
+
textColor: (0, import_pdf_lib4.rgb)(txtCol.r, txtCol.g, txtCol.b)
|
|
9405
10625
|
});
|
|
9406
10626
|
if (resolved) {
|
|
9407
10627
|
tf.setText(resolved);
|
|
@@ -9409,9 +10629,9 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9409
10629
|
if (textEl.locked) tf.enableReadOnly();
|
|
9410
10630
|
if (textEl.required) tf.enableRequired();
|
|
9411
10631
|
tf.acroField.dict.set(
|
|
9412
|
-
|
|
10632
|
+
import_pdf_lib4.PDFName.of("YudaoMeta"),
|
|
9413
10633
|
pdfDoc.context.obj({
|
|
9414
|
-
ElementType:
|
|
10634
|
+
ElementType: import_pdf_lib4.PDFName.of("text"),
|
|
9415
10635
|
Left: left,
|
|
9416
10636
|
Top: top,
|
|
9417
10637
|
Width: width,
|
|
@@ -9431,10 +10651,10 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9431
10651
|
txtCol
|
|
9432
10652
|
});
|
|
9433
10653
|
const daStr = `/FCjk ${fmtPdfNum(fsVal)} Tf ${fmtPdfNum(txtCol.r)} ${fmtPdfNum(txtCol.g)} ${fmtPdfNum(txtCol.b)} rg`;
|
|
9434
|
-
tf.acroField.dict.set(
|
|
10654
|
+
tf.acroField.dict.set(import_pdf_lib4.PDFName.of("DA"), import_pdf_lib4.PDFString.of(daStr));
|
|
9435
10655
|
for (const w of tf.acroField.getWidgets()) {
|
|
9436
|
-
w.dict.set(
|
|
9437
|
-
w.dict.set(
|
|
10656
|
+
w.dict.set(import_pdf_lib4.PDFName.of("AP"), pdfDoc.context.obj({ N: apRef }));
|
|
10657
|
+
w.dict.set(import_pdf_lib4.PDFName.of("DA"), import_pdf_lib4.PDFString.of(daStr));
|
|
9438
10658
|
}
|
|
9439
10659
|
} else {
|
|
9440
10660
|
tf.defaultUpdateAppearances(await ensureAcroHelvetica());
|
|
@@ -9565,6 +10785,32 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9565
10785
|
svgEl.opacity ?? 1,
|
|
9566
10786
|
elColorType
|
|
9567
10787
|
);
|
|
10788
|
+
const svgTextNodes = extractSvgTextDescriptors(resolvedSvg, width, height);
|
|
10789
|
+
for (const td of svgTextNodes) {
|
|
10790
|
+
try {
|
|
10791
|
+
await drawTextBox(
|
|
10792
|
+
pdfDoc,
|
|
10793
|
+
pdfPage,
|
|
10794
|
+
pageHeight,
|
|
10795
|
+
td.text,
|
|
10796
|
+
left + td.leftPt,
|
|
10797
|
+
top + td.topPt,
|
|
10798
|
+
td.widthPt,
|
|
10799
|
+
td.heightPt,
|
|
10800
|
+
{
|
|
10801
|
+
fontSize: td.fontSizePt,
|
|
10802
|
+
fontWeight: td.bold ? "bold" : "normal",
|
|
10803
|
+
color: td.color,
|
|
10804
|
+
textAlign: td.align,
|
|
10805
|
+
verticalAlign: "middle",
|
|
10806
|
+
padding: 0
|
|
10807
|
+
},
|
|
10808
|
+
!hasNonAscii(td.text) ? cjkState : null,
|
|
10809
|
+
{ noWrapSingleLine: true, colorType: elColorType }
|
|
10810
|
+
);
|
|
10811
|
+
} catch {
|
|
10812
|
+
}
|
|
10813
|
+
}
|
|
9568
10814
|
} else if (el.type === "circleMark" /* CIRCLE_MARK */) {
|
|
9569
10815
|
const cmEl = el;
|
|
9570
10816
|
const cmKey = cmEl.formFieldName || cmEl.fieldKey || cmEl.bindingField;
|
|
@@ -9578,23 +10824,23 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9578
10824
|
};
|
|
9579
10825
|
const strokeOp = elColorType === "cmyk" ? (() => {
|
|
9580
10826
|
const cm = rgbToCmyk(strokeCol.r, strokeCol.g, strokeCol.b);
|
|
9581
|
-
return (0,
|
|
9582
|
-
})() : (0,
|
|
10827
|
+
return (0, import_pdf_lib4.setStrokingCmykColor)(cm.c, cm.m, cm.y, cm.k);
|
|
10828
|
+
})() : (0, import_pdf_lib4.setStrokingRgbColor)(strokeCol.r, strokeCol.g, strokeCol.b);
|
|
9583
10829
|
const ellipse = computeCircleMarkBezierEllipse(
|
|
9584
10830
|
{ x: left, y: pdfY, width, height },
|
|
9585
10831
|
cmEl.strokeWidthMm ?? 0.6
|
|
9586
10832
|
);
|
|
9587
10833
|
pdfPage.pushOperators(
|
|
9588
|
-
(0,
|
|
10834
|
+
(0, import_pdf_lib4.pushGraphicsState)(),
|
|
9589
10835
|
strokeOp,
|
|
9590
|
-
(0,
|
|
9591
|
-
(0,
|
|
10836
|
+
(0, import_pdf_lib4.setLineWidth)(ellipse.strokeWidthPt),
|
|
10837
|
+
(0, import_pdf_lib4.moveTo)(ellipse.start[0], ellipse.start[1]),
|
|
9592
10838
|
...ellipse.segments.map(
|
|
9593
|
-
([x1, y1, x2, y2, x3, y3]) => (0,
|
|
10839
|
+
([x1, y1, x2, y2, x3, y3]) => (0, import_pdf_lib4.appendBezierCurve)(x1, y1, x2, y2, x3, y3)
|
|
9594
10840
|
),
|
|
9595
|
-
(0,
|
|
9596
|
-
(0,
|
|
9597
|
-
(0,
|
|
10841
|
+
(0, import_pdf_lib4.closePath)(),
|
|
10842
|
+
(0, import_pdf_lib4.stroke)(),
|
|
10843
|
+
(0, import_pdf_lib4.popGraphicsState)()
|
|
9598
10844
|
);
|
|
9599
10845
|
}
|
|
9600
10846
|
} else if (el.type === "dateTime" /* DATE_TIME */) {
|
|
@@ -9615,9 +10861,9 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9615
10861
|
usedAcroFieldNames
|
|
9616
10862
|
);
|
|
9617
10863
|
const tf = form.createTextField(fieldName);
|
|
9618
|
-
if (dtAlign === "center") tf.setAlignment(
|
|
9619
|
-
else if (dtAlign === "right") tf.setAlignment(
|
|
9620
|
-
else tf.setAlignment(
|
|
10864
|
+
if (dtAlign === "center") tf.setAlignment(import_pdf_lib4.TextAlignment.Center);
|
|
10865
|
+
else if (dtAlign === "right") tf.setAlignment(import_pdf_lib4.TextAlignment.Right);
|
|
10866
|
+
else tf.setAlignment(import_pdf_lib4.TextAlignment.Left);
|
|
9621
10867
|
const bg = parseHexColor(dtBgHex) ?? { r: 1, g: 1, b: 1, a: 1 };
|
|
9622
10868
|
const border = parseHexColor(dtEl.style?.borderColor) ?? { r: 0.75, g: 0.82, b: 0.9, a: 1 };
|
|
9623
10869
|
const txtCol = parseHexColor(dtColorHex) ?? { r: 0.11, g: 0.11, b: 0.12, a: 1 };
|
|
@@ -9627,11 +10873,11 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9627
10873
|
y: pdfY,
|
|
9628
10874
|
width,
|
|
9629
10875
|
height,
|
|
9630
|
-
rotate: (0,
|
|
10876
|
+
rotate: (0, import_pdf_lib4.degrees)(rotDeg),
|
|
9631
10877
|
borderWidth: dtEl.style?.borderWidth ?? 1,
|
|
9632
|
-
borderColor: (0,
|
|
9633
|
-
backgroundColor: (0,
|
|
9634
|
-
textColor: (0,
|
|
10878
|
+
borderColor: (0, import_pdf_lib4.rgb)(border.r, border.g, border.b),
|
|
10879
|
+
backgroundColor: (0, import_pdf_lib4.rgb)(bg.r, bg.g, bg.b),
|
|
10880
|
+
textColor: (0, import_pdf_lib4.rgb)(txtCol.r, txtCol.g, txtCol.b)
|
|
9635
10881
|
});
|
|
9636
10882
|
if (formattedDt) {
|
|
9637
10883
|
tf.setText(formattedDt);
|
|
@@ -9639,21 +10885,21 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9639
10885
|
if (dtEl.locked) tf.enableReadOnly();
|
|
9640
10886
|
if (dtEl.required) tf.enableRequired();
|
|
9641
10887
|
tf.acroField.dict.set(
|
|
9642
|
-
|
|
9643
|
-
|
|
10888
|
+
import_pdf_lib4.PDFName.of("TU"),
|
|
10889
|
+
import_pdf_lib4.PDFHexString.fromText(
|
|
9644
10890
|
`dateTime:${dtEl.dateKind || "date"}|${dtEl.format || "YYYY-MM-DD"}|${dtEl.locale || ""}`
|
|
9645
10891
|
)
|
|
9646
10892
|
);
|
|
9647
10893
|
tf.acroField.dict.set(
|
|
9648
|
-
|
|
10894
|
+
import_pdf_lib4.PDFName.of("YudaoMeta"),
|
|
9649
10895
|
pdfDoc.context.obj({
|
|
9650
|
-
ElementType:
|
|
9651
|
-
DateKind:
|
|
9652
|
-
Format:
|
|
9653
|
-
...dtEl.locale ? { Locale:
|
|
9654
|
-
RawValue:
|
|
10896
|
+
ElementType: import_pdf_lib4.PDFName.of("dateTime"),
|
|
10897
|
+
DateKind: import_pdf_lib4.PDFString.of(dtEl.dateKind || "date"),
|
|
10898
|
+
Format: import_pdf_lib4.PDFHexString.fromText(dtEl.format || "YYYY-MM-DD"),
|
|
10899
|
+
...dtEl.locale ? { Locale: import_pdf_lib4.PDFString.of(dtEl.locale) } : {},
|
|
10900
|
+
RawValue: import_pdf_lib4.PDFHexString.fromText(rawDtVal),
|
|
9655
10901
|
FontSize: dtFontSize,
|
|
9656
|
-
Align:
|
|
10902
|
+
Align: import_pdf_lib4.PDFString.of(dtAlign),
|
|
9657
10903
|
...dtEl.characterSpacing !== void 0 ? { CharacterSpacing: dtEl.characterSpacing } : {},
|
|
9658
10904
|
Left: left,
|
|
9659
10905
|
Top: top,
|
|
@@ -9674,10 +10920,10 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9674
10920
|
txtCol
|
|
9675
10921
|
});
|
|
9676
10922
|
const daStr = `/FCjk ${fmtPdfNum(dtFontSize)} Tf ${fmtPdfNum(txtCol.r)} ${fmtPdfNum(txtCol.g)} ${fmtPdfNum(txtCol.b)} rg`;
|
|
9677
|
-
tf.acroField.dict.set(
|
|
10923
|
+
tf.acroField.dict.set(import_pdf_lib4.PDFName.of("DA"), import_pdf_lib4.PDFString.of(daStr));
|
|
9678
10924
|
for (const w of tf.acroField.getWidgets()) {
|
|
9679
|
-
w.dict.set(
|
|
9680
|
-
w.dict.set(
|
|
10925
|
+
w.dict.set(import_pdf_lib4.PDFName.of("AP"), pdfDoc.context.obj({ N: apRef }));
|
|
10926
|
+
w.dict.set(import_pdf_lib4.PDFName.of("DA"), import_pdf_lib4.PDFString.of(daStr));
|
|
9681
10927
|
}
|
|
9682
10928
|
} else {
|
|
9683
10929
|
tf.defaultUpdateAppearances(await ensureAcroHelvetica());
|
|
@@ -9729,10 +10975,10 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9729
10975
|
y: pdfY + (height - boxSz) / 2,
|
|
9730
10976
|
width: boxSz,
|
|
9731
10977
|
height: boxSz,
|
|
9732
|
-
rotate: (0,
|
|
10978
|
+
rotate: (0, import_pdf_lib4.degrees)(rotDeg),
|
|
9733
10979
|
borderWidth: 1,
|
|
9734
|
-
borderColor: (0,
|
|
9735
|
-
backgroundColor: (0,
|
|
10980
|
+
borderColor: (0, import_pdf_lib4.rgb)(0.28, 0.33, 0.41),
|
|
10981
|
+
backgroundColor: (0, import_pdf_lib4.rgb)(1, 1, 1)
|
|
9736
10982
|
});
|
|
9737
10983
|
if (isChecked) cb.check();
|
|
9738
10984
|
else cb.uncheck();
|
|
@@ -9740,14 +10986,14 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9740
10986
|
if (cbEl.required) cb.enableRequired();
|
|
9741
10987
|
cb.defaultUpdateAppearances();
|
|
9742
10988
|
cb.acroField.dict.set(
|
|
9743
|
-
|
|
9744
|
-
|
|
10989
|
+
import_pdf_lib4.PDFName.of("TU"),
|
|
10990
|
+
import_pdf_lib4.PDFHexString.fromText(cbEl.label || fieldName)
|
|
9745
10991
|
);
|
|
9746
10992
|
cb.acroField.dict.set(
|
|
9747
|
-
|
|
10993
|
+
import_pdf_lib4.PDFName.of("YudaoMeta"),
|
|
9748
10994
|
pdfDoc.context.obj({
|
|
9749
|
-
ElementType:
|
|
9750
|
-
Label:
|
|
10995
|
+
ElementType: import_pdf_lib4.PDFName.of("checkbox"),
|
|
10996
|
+
Label: import_pdf_lib4.PDFHexString.fromText(cbEl.label || ""),
|
|
9751
10997
|
BoxWidth: boxSz,
|
|
9752
10998
|
Left: left,
|
|
9753
10999
|
Top: top,
|
|
@@ -9777,8 +11023,8 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9777
11023
|
y: pdfY + (height - boxSz) / 2,
|
|
9778
11024
|
width: boxSz,
|
|
9779
11025
|
height: boxSz,
|
|
9780
|
-
color: isChecked ? (0,
|
|
9781
|
-
borderColor: (0,
|
|
11026
|
+
color: isChecked ? (0, import_pdf_lib4.rgb)(0.15, 0.39, 0.92) : (0, import_pdf_lib4.rgb)(1, 1, 1),
|
|
11027
|
+
borderColor: (0, import_pdf_lib4.rgb)(0.28, 0.33, 0.41),
|
|
9782
11028
|
borderWidth: 1
|
|
9783
11029
|
});
|
|
9784
11030
|
await drawTextBox(
|
|
@@ -9823,10 +11069,10 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9823
11069
|
y: btnY,
|
|
9824
11070
|
width: btnSz,
|
|
9825
11071
|
height: btnSz,
|
|
9826
|
-
rotate: (0,
|
|
11072
|
+
rotate: (0, import_pdf_lib4.degrees)(rotDeg),
|
|
9827
11073
|
borderWidth: 1,
|
|
9828
|
-
borderColor: (0,
|
|
9829
|
-
backgroundColor: (0,
|
|
11074
|
+
borderColor: (0, import_pdf_lib4.rgb)(0.28, 0.33, 0.41),
|
|
11075
|
+
backgroundColor: (0, import_pdf_lib4.rgb)(1, 1, 1)
|
|
9830
11076
|
});
|
|
9831
11077
|
await drawTextBox(
|
|
9832
11078
|
pdfDoc,
|
|
@@ -9851,10 +11097,10 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9851
11097
|
y: btnY,
|
|
9852
11098
|
width: btnSz,
|
|
9853
11099
|
height: btnSz,
|
|
9854
|
-
rotate: (0,
|
|
11100
|
+
rotate: (0, import_pdf_lib4.degrees)(rotDeg),
|
|
9855
11101
|
borderWidth: 1,
|
|
9856
|
-
borderColor: (0,
|
|
9857
|
-
backgroundColor: (0,
|
|
11102
|
+
borderColor: (0, import_pdf_lib4.rgb)(0.28, 0.33, 0.41),
|
|
11103
|
+
backgroundColor: (0, import_pdf_lib4.rgb)(1, 1, 1)
|
|
9858
11104
|
});
|
|
9859
11105
|
await drawTextBox(
|
|
9860
11106
|
pdfDoc,
|
|
@@ -9878,10 +11124,10 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9878
11124
|
if (rgEl.required) rg.enableRequired();
|
|
9879
11125
|
rg.defaultUpdateAppearances();
|
|
9880
11126
|
rg.acroField.dict.set(
|
|
9881
|
-
|
|
11127
|
+
import_pdf_lib4.PDFName.of("YudaoMeta"),
|
|
9882
11128
|
pdfDoc.context.obj({
|
|
9883
|
-
ElementType:
|
|
9884
|
-
Direction:
|
|
11129
|
+
ElementType: import_pdf_lib4.PDFName.of("radioGroup"),
|
|
11130
|
+
Direction: import_pdf_lib4.PDFString.of(rgEl.direction || "horizontal"),
|
|
9885
11131
|
Left: left,
|
|
9886
11132
|
Top: top,
|
|
9887
11133
|
Width: width,
|
|
@@ -9937,11 +11183,11 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9937
11183
|
y: pdfY,
|
|
9938
11184
|
width,
|
|
9939
11185
|
height,
|
|
9940
|
-
rotate: (0,
|
|
11186
|
+
rotate: (0, import_pdf_lib4.degrees)(rotDeg),
|
|
9941
11187
|
borderWidth: selEl.style?.borderWidth ?? 1,
|
|
9942
|
-
borderColor: (0,
|
|
9943
|
-
backgroundColor: (0,
|
|
9944
|
-
textColor: (0,
|
|
11188
|
+
borderColor: (0, import_pdf_lib4.rgb)(border.r, border.g, border.b),
|
|
11189
|
+
backgroundColor: (0, import_pdf_lib4.rgb)(bg.r, bg.g, bg.b),
|
|
11190
|
+
textColor: (0, import_pdf_lib4.rgb)(txtCol.r, txtCol.g, txtCol.b)
|
|
9945
11191
|
});
|
|
9946
11192
|
if (curVal && baseOpts.includes(curVal)) {
|
|
9947
11193
|
dd.select(curVal);
|
|
@@ -9949,10 +11195,10 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9949
11195
|
if (selEl.locked) dd.enableReadOnly();
|
|
9950
11196
|
if (selEl.required) dd.enableRequired();
|
|
9951
11197
|
dd.acroField.dict.set(
|
|
9952
|
-
|
|
11198
|
+
import_pdf_lib4.PDFName.of("YudaoMeta"),
|
|
9953
11199
|
pdfDoc.context.obj({
|
|
9954
|
-
ElementType:
|
|
9955
|
-
...selEl.placeholder ? { Placeholder:
|
|
11200
|
+
ElementType: import_pdf_lib4.PDFName.of("select"),
|
|
11201
|
+
...selEl.placeholder ? { Placeholder: import_pdf_lib4.PDFHexString.fromText(selEl.placeholder) } : {},
|
|
9956
11202
|
Left: left,
|
|
9957
11203
|
Top: top,
|
|
9958
11204
|
Width: width,
|
|
@@ -9972,10 +11218,10 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
9972
11218
|
txtCol
|
|
9973
11219
|
});
|
|
9974
11220
|
const daStr = `/FCjk ${fmtPdfNum(fsVal)} Tf ${fmtPdfNum(txtCol.r)} ${fmtPdfNum(txtCol.g)} ${fmtPdfNum(txtCol.b)} rg`;
|
|
9975
|
-
dd.acroField.dict.set(
|
|
11221
|
+
dd.acroField.dict.set(import_pdf_lib4.PDFName.of("DA"), import_pdf_lib4.PDFString.of(daStr));
|
|
9976
11222
|
for (const w of dd.acroField.getWidgets()) {
|
|
9977
|
-
w.dict.set(
|
|
9978
|
-
w.dict.set(
|
|
11223
|
+
w.dict.set(import_pdf_lib4.PDFName.of("AP"), pdfDoc.context.obj({ N: apRef }));
|
|
11224
|
+
w.dict.set(import_pdf_lib4.PDFName.of("DA"), import_pdf_lib4.PDFString.of(daStr));
|
|
9979
11225
|
}
|
|
9980
11226
|
} else {
|
|
9981
11227
|
dd.defaultUpdateAppearances(await ensureAcroHelvetica());
|
|
@@ -10015,8 +11261,8 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10015
11261
|
Type: "Annot",
|
|
10016
11262
|
Subtype: "Widget",
|
|
10017
11263
|
FT: "Sig",
|
|
10018
|
-
T:
|
|
10019
|
-
TU:
|
|
11264
|
+
T: import_pdf_lib4.PDFHexString.fromText(fieldName),
|
|
11265
|
+
TU: import_pdf_lib4.PDFHexString.fromText(sigEl.label || fieldName),
|
|
10020
11266
|
Rect: [left, pdfY, left + width, pdfY + height],
|
|
10021
11267
|
P: pdfPage.ref,
|
|
10022
11268
|
F: 4,
|
|
@@ -10024,12 +11270,12 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10024
11270
|
AP: { N: sigApRef },
|
|
10025
11271
|
MK: { R: rotDeg },
|
|
10026
11272
|
YudaoMeta: {
|
|
10027
|
-
ElementType:
|
|
10028
|
-
Label:
|
|
10029
|
-
...sigEl.signerName ? { SignerName:
|
|
10030
|
-
...sigEl.signedAt ? { SignedAt:
|
|
10031
|
-
...sigEl.reason ? { Reason:
|
|
10032
|
-
...sigEl.signatureDataUri ? { SignatureDataUri:
|
|
11273
|
+
ElementType: import_pdf_lib4.PDFName.of("signature"),
|
|
11274
|
+
Label: import_pdf_lib4.PDFHexString.fromText(sigEl.label || "Signature"),
|
|
11275
|
+
...sigEl.signerName ? { SignerName: import_pdf_lib4.PDFHexString.fromText(sigEl.signerName) } : {},
|
|
11276
|
+
...sigEl.signedAt ? { SignedAt: import_pdf_lib4.PDFString.of(sigEl.signedAt) } : {},
|
|
11277
|
+
...sigEl.reason ? { Reason: import_pdf_lib4.PDFHexString.fromText(sigEl.reason) } : {},
|
|
11278
|
+
...sigEl.signatureDataUri ? { SignatureDataUri: import_pdf_lib4.PDFString.of(sigEl.signatureDataUri) } : {},
|
|
10033
11279
|
Left: left,
|
|
10034
11280
|
Top: top,
|
|
10035
11281
|
Width: width,
|
|
@@ -10040,11 +11286,11 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10040
11286
|
if (sigEl.signerName || sigEl.signatureDataUri) {
|
|
10041
11287
|
sigWidgetObj.V = pdfDoc.context.obj({
|
|
10042
11288
|
Type: "Sig",
|
|
10043
|
-
Filter:
|
|
10044
|
-
SubFilter:
|
|
10045
|
-
Name:
|
|
10046
|
-
...sigEl.signedAt ? { M:
|
|
10047
|
-
...sigEl.reason ? { Reason:
|
|
11289
|
+
Filter: import_pdf_lib4.PDFName.of("Adobe.PPKLite"),
|
|
11290
|
+
SubFilter: import_pdf_lib4.PDFName.of("adbe.pkcs7.detached"),
|
|
11291
|
+
Name: import_pdf_lib4.PDFHexString.fromText(sigEl.signerName || sigEl.label || "Signed"),
|
|
11292
|
+
...sigEl.signedAt ? { M: import_pdf_lib4.PDFString.of(sigEl.signedAt) } : {},
|
|
11293
|
+
...sigEl.reason ? { Reason: import_pdf_lib4.PDFHexString.fromText(sigEl.reason) } : {}
|
|
10048
11294
|
});
|
|
10049
11295
|
}
|
|
10050
11296
|
const sigRef = pdfDoc.context.register(pdfDoc.context.obj(sigWidgetObj));
|
|
@@ -10056,8 +11302,8 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10056
11302
|
y: pdfY,
|
|
10057
11303
|
width,
|
|
10058
11304
|
height,
|
|
10059
|
-
color: (0,
|
|
10060
|
-
borderColor: (0,
|
|
11305
|
+
color: (0, import_pdf_lib4.rgb)(0.98, 0.985, 0.995),
|
|
11306
|
+
borderColor: (0, import_pdf_lib4.rgb)(0.55, 0.62, 0.72),
|
|
10061
11307
|
borderWidth: 0.9,
|
|
10062
11308
|
borderDashArray: sigEl.signerName || sigEl.signatureDataUri ? void 0 : [3, 2]
|
|
10063
11309
|
});
|
|
@@ -10065,7 +11311,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10065
11311
|
start: { x: left + 6, y: pdfY + 8 },
|
|
10066
11312
|
end: { x: left + Math.max(12, width - 6), y: pdfY + 8 },
|
|
10067
11313
|
thickness: 0.65,
|
|
10068
|
-
color: (0,
|
|
11314
|
+
color: (0, import_pdf_lib4.rgb)(0.45, 0.52, 0.62)
|
|
10069
11315
|
});
|
|
10070
11316
|
if (sigEl.signatureDataUri) {
|
|
10071
11317
|
const sigImgEntry = await embedImageWithMetaFromSrc(pdfDoc, sigEl.signatureDataUri);
|
|
@@ -10137,13 +11383,13 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10137
11383
|
drawH
|
|
10138
11384
|
);
|
|
10139
11385
|
pdfPage.pushOperators(
|
|
10140
|
-
(0,
|
|
10141
|
-
(0,
|
|
10142
|
-
(0,
|
|
10143
|
-
(0,
|
|
10144
|
-
(0,
|
|
10145
|
-
(0,
|
|
10146
|
-
(0,
|
|
11386
|
+
(0, import_pdf_lib4.pushGraphicsState)(),
|
|
11387
|
+
(0, import_pdf_lib4.rectangle)(clipX, clipY, clipW, clipH),
|
|
11388
|
+
(0, import_pdf_lib4.clip)(),
|
|
11389
|
+
(0, import_pdf_lib4.endPath)(),
|
|
11390
|
+
(0, import_pdf_lib4.concatTransformationMatrix)(ma, mb, mc, md, me, mf),
|
|
11391
|
+
(0, import_pdf_lib4.drawObject)(imgKey),
|
|
11392
|
+
(0, import_pdf_lib4.popGraphicsState)()
|
|
10147
11393
|
);
|
|
10148
11394
|
} else if (img && exifOrientation !== 1) {
|
|
10149
11395
|
const fitMode = el.fit ?? "contain";
|
|
@@ -10167,20 +11413,20 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10167
11413
|
);
|
|
10168
11414
|
if (placement2.needsClip) {
|
|
10169
11415
|
pdfPage.pushOperators(
|
|
10170
|
-
(0,
|
|
10171
|
-
(0,
|
|
10172
|
-
(0,
|
|
10173
|
-
(0,
|
|
10174
|
-
(0,
|
|
10175
|
-
(0,
|
|
10176
|
-
(0,
|
|
11416
|
+
(0, import_pdf_lib4.pushGraphicsState)(),
|
|
11417
|
+
(0, import_pdf_lib4.rectangle)(placement2.clipX, placement2.clipY, placement2.clipWidth, placement2.clipHeight),
|
|
11418
|
+
(0, import_pdf_lib4.clip)(),
|
|
11419
|
+
(0, import_pdf_lib4.endPath)(),
|
|
11420
|
+
(0, import_pdf_lib4.concatTransformationMatrix)(ma, mb, mc, md, me, mf),
|
|
11421
|
+
(0, import_pdf_lib4.drawObject)(imgKey),
|
|
11422
|
+
(0, import_pdf_lib4.popGraphicsState)()
|
|
10177
11423
|
);
|
|
10178
11424
|
} else {
|
|
10179
11425
|
pdfPage.pushOperators(
|
|
10180
|
-
(0,
|
|
10181
|
-
(0,
|
|
10182
|
-
(0,
|
|
10183
|
-
(0,
|
|
11426
|
+
(0, import_pdf_lib4.pushGraphicsState)(),
|
|
11427
|
+
(0, import_pdf_lib4.concatTransformationMatrix)(ma, mb, mc, md, me, mf),
|
|
11428
|
+
(0, import_pdf_lib4.drawObject)(imgKey),
|
|
11429
|
+
(0, import_pdf_lib4.popGraphicsState)()
|
|
10184
11430
|
);
|
|
10185
11431
|
}
|
|
10186
11432
|
} else if (img) {
|
|
@@ -10194,10 +11440,10 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10194
11440
|
);
|
|
10195
11441
|
if (placement2.needsClip) {
|
|
10196
11442
|
pdfPage.pushOperators(
|
|
10197
|
-
(0,
|
|
10198
|
-
(0,
|
|
10199
|
-
(0,
|
|
10200
|
-
(0,
|
|
11443
|
+
(0, import_pdf_lib4.pushGraphicsState)(),
|
|
11444
|
+
(0, import_pdf_lib4.rectangle)(placement2.clipX, placement2.clipY, placement2.clipWidth, placement2.clipHeight),
|
|
11445
|
+
(0, import_pdf_lib4.clip)(),
|
|
11446
|
+
(0, import_pdf_lib4.endPath)()
|
|
10201
11447
|
);
|
|
10202
11448
|
pdfPage.drawImage(img, {
|
|
10203
11449
|
x: placement2.drawX,
|
|
@@ -10206,7 +11452,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10206
11452
|
height: placement2.drawHeight,
|
|
10207
11453
|
opacity: el.opacity
|
|
10208
11454
|
});
|
|
10209
|
-
pdfPage.pushOperators((0,
|
|
11455
|
+
pdfPage.pushOperators((0, import_pdf_lib4.popGraphicsState)());
|
|
10210
11456
|
} else {
|
|
10211
11457
|
pdfPage.drawImage(img, {
|
|
10212
11458
|
x: placement2.drawX,
|
|
@@ -10296,7 +11542,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10296
11542
|
y: pdfY,
|
|
10297
11543
|
width,
|
|
10298
11544
|
height,
|
|
10299
|
-
color: (0,
|
|
11545
|
+
color: (0, import_pdf_lib4.rgb)(col.r, col.g, col.b),
|
|
10300
11546
|
opacity: op,
|
|
10301
11547
|
borderWidth: 0
|
|
10302
11548
|
});
|
|
@@ -10305,7 +11551,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10305
11551
|
start: { x: left, y: pdfY + 1.5 },
|
|
10306
11552
|
end: { x: left + width, y: pdfY + 1.5 },
|
|
10307
11553
|
thickness: 1.5,
|
|
10308
|
-
color: (0,
|
|
11554
|
+
color: (0, import_pdf_lib4.rgb)(col.r, col.g, col.b),
|
|
10309
11555
|
opacity: op
|
|
10310
11556
|
});
|
|
10311
11557
|
} else if (annotType === "strikeout") {
|
|
@@ -10313,7 +11559,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10313
11559
|
start: { x: left, y: pdfY + height / 2 },
|
|
10314
11560
|
end: { x: left + width, y: pdfY + height / 2 },
|
|
10315
11561
|
thickness: 1.5,
|
|
10316
|
-
color: (0,
|
|
11562
|
+
color: (0, import_pdf_lib4.rgb)(col.r, col.g, col.b),
|
|
10317
11563
|
opacity: op
|
|
10318
11564
|
});
|
|
10319
11565
|
} else if (annotType === "squiggly") {
|
|
@@ -10331,7 +11577,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10331
11577
|
pdfPage.drawSvgPath(segments.join(" "), {
|
|
10332
11578
|
x: left,
|
|
10333
11579
|
y: pageHeight - top,
|
|
10334
|
-
borderColor: (0,
|
|
11580
|
+
borderColor: (0, import_pdf_lib4.rgb)(col.r, col.g, col.b),
|
|
10335
11581
|
borderWidth: 1.3,
|
|
10336
11582
|
borderOpacity: op
|
|
10337
11583
|
});
|
|
@@ -10342,8 +11588,8 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10342
11588
|
y: pdfY,
|
|
10343
11589
|
width,
|
|
10344
11590
|
height,
|
|
10345
|
-
color: (0,
|
|
10346
|
-
borderColor: (0,
|
|
11591
|
+
color: (0, import_pdf_lib4.rgb)(1, 0.97, 0.86),
|
|
11592
|
+
borderColor: (0, import_pdf_lib4.rgb)(col.r, col.g, col.b),
|
|
10347
11593
|
borderWidth: 1.2,
|
|
10348
11594
|
opacity: op
|
|
10349
11595
|
});
|
|
@@ -10353,29 +11599,43 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10353
11599
|
start: { x: lineLeft, y: pdfY + height * 0.65 },
|
|
10354
11600
|
end: { x: lineRight, y: pdfY + height * 0.65 },
|
|
10355
11601
|
thickness: 1,
|
|
10356
|
-
color: (0,
|
|
11602
|
+
color: (0, import_pdf_lib4.rgb)(col.r, col.g, col.b)
|
|
10357
11603
|
});
|
|
10358
11604
|
pdfPage.drawLine({
|
|
10359
11605
|
start: { x: lineLeft, y: pdfY + height * 0.4 },
|
|
10360
11606
|
end: { x: lineLeft + (lineRight - lineLeft) * 0.72, y: pdfY + height * 0.4 },
|
|
10361
11607
|
thickness: 1,
|
|
10362
|
-
color: (0,
|
|
11608
|
+
color: (0, import_pdf_lib4.rgb)(col.r, col.g, col.b)
|
|
10363
11609
|
});
|
|
10364
11610
|
}
|
|
10365
11611
|
const pdfSubtype = annotType === "highlight" ? "Highlight" : annotType === "underline" ? "Underline" : annotType === "strikeout" ? "StrikeOut" : annotType === "squiggly" ? "Squiggly" : "Text";
|
|
11612
|
+
const effectiveStatus = annotEl.reviewStatus ?? annotEl.status;
|
|
10366
11613
|
const replyLines = (annotEl.replies ?? []).map((r) => `[${r.author}] ${r.content}`);
|
|
10367
11614
|
const threadSummary = [
|
|
10368
|
-
|
|
11615
|
+
effectiveStatus ? `[${effectiveStatus.toUpperCase()}] ${annotEl.comment ?? ""}`.trim() : annotEl.comment ?? "",
|
|
10369
11616
|
...replyLines
|
|
10370
11617
|
].filter(Boolean).join("\n");
|
|
11618
|
+
const pdfReviewStateMap = {
|
|
11619
|
+
accepted: "Accepted",
|
|
11620
|
+
rejected: "Rejected",
|
|
11621
|
+
completed: "Completed",
|
|
11622
|
+
cancelled: "Cancelled",
|
|
11623
|
+
resolved: "Completed",
|
|
11624
|
+
open: "None"
|
|
11625
|
+
};
|
|
11626
|
+
const pdfReviewState = effectiveStatus ? pdfReviewStateMap[effectiveStatus] : void 0;
|
|
10371
11627
|
const annotDictEntries = {
|
|
10372
11628
|
Type: "Annot",
|
|
10373
11629
|
Subtype: pdfSubtype,
|
|
10374
11630
|
Rect: [left, pdfY, left + width, pdfY + height],
|
|
10375
11631
|
C: [col.r, col.g, col.b],
|
|
10376
11632
|
CA: op,
|
|
10377
|
-
T:
|
|
10378
|
-
Contents:
|
|
11633
|
+
T: import_pdf_lib4.PDFHexString.fromText(annotEl.author || "Reviewer"),
|
|
11634
|
+
Contents: import_pdf_lib4.PDFHexString.fromText(threadSummary),
|
|
11635
|
+
...pdfReviewState ? {
|
|
11636
|
+
StateModel: import_pdf_lib4.PDFString.of("Review"),
|
|
11637
|
+
State: import_pdf_lib4.PDFString.of(pdfReviewState)
|
|
11638
|
+
} : {}
|
|
10379
11639
|
};
|
|
10380
11640
|
if (pdfSubtype !== "Text") {
|
|
10381
11641
|
annotDictEntries.QuadPoints = [
|
|
@@ -10398,7 +11658,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10398
11658
|
y: pdfY,
|
|
10399
11659
|
width,
|
|
10400
11660
|
height,
|
|
10401
|
-
color: (0,
|
|
11661
|
+
color: (0, import_pdf_lib4.rgb)(fill.r, fill.g, fill.b),
|
|
10402
11662
|
opacity: 1,
|
|
10403
11663
|
borderWidth: 0
|
|
10404
11664
|
});
|
|
@@ -10431,10 +11691,10 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10431
11691
|
}
|
|
10432
11692
|
} finally {
|
|
10433
11693
|
if (hasOpenCenterTransform) {
|
|
10434
|
-
pdfPage.pushOperators((0,
|
|
11694
|
+
pdfPage.pushOperators((0, import_pdf_lib4.popGraphicsState)());
|
|
10435
11695
|
}
|
|
10436
11696
|
while (openBdcCount > 0) {
|
|
10437
|
-
pdfPage.pushOperators(
|
|
11697
|
+
pdfPage.pushOperators(import_pdf_lib4.PDFOperator.of("EMC"));
|
|
10438
11698
|
openBdcCount--;
|
|
10439
11699
|
}
|
|
10440
11700
|
}
|
|
@@ -10464,7 +11724,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10464
11724
|
const batesFs = 8.5;
|
|
10465
11725
|
const marginX = 18;
|
|
10466
11726
|
if (!hasNonAscii(batesStr) && cjkState?.mode !== "subset-ttf") {
|
|
10467
|
-
const batesFont = await pdfDoc.embedFont(
|
|
11727
|
+
const batesFont = await pdfDoc.embedFont(import_pdf_lib4.StandardFonts.HelveticaBold);
|
|
10468
11728
|
const textW = batesFont.widthOfTextAtSize(batesStr, batesFs);
|
|
10469
11729
|
const bx = pos === "topLeft" || pos === "bottomLeft" ? marginX : Math.max(marginX, pageWidth - marginX - textW);
|
|
10470
11730
|
const by = pos === "bottomLeft" || pos === "bottomRight" ? 14 : Math.max(14, pageHeight - 14 - batesFs);
|
|
@@ -10473,7 +11733,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10473
11733
|
y: by,
|
|
10474
11734
|
size: batesFs,
|
|
10475
11735
|
font: batesFont,
|
|
10476
|
-
color: (0,
|
|
11736
|
+
color: (0, import_pdf_lib4.rgb)(0.18, 0.22, 0.3)
|
|
10477
11737
|
});
|
|
10478
11738
|
} else {
|
|
10479
11739
|
const boxW = Math.min(220, pageWidth - marginX * 2);
|
|
@@ -10509,7 +11769,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10509
11769
|
const wmRgb = parseHexColor(wmCfg?.color) ?? { r: 0.4, g: 0.45, b: 0.55 };
|
|
10510
11770
|
const isTiled = wmCfg?.tiled !== false;
|
|
10511
11771
|
if (!hasNonAscii(watermarkText)) {
|
|
10512
|
-
const wmFont = await pdfDoc.embedFont(
|
|
11772
|
+
const wmFont = await pdfDoc.embedFont(import_pdf_lib4.StandardFonts.HelveticaBold);
|
|
10513
11773
|
if (isTiled) {
|
|
10514
11774
|
for (let wy = 90; wy < pageHeight - 40; wy += 180) {
|
|
10515
11775
|
for (let wx = 50; wx < pageWidth2 - 40; wx += 220) {
|
|
@@ -10518,9 +11778,9 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10518
11778
|
y: wy,
|
|
10519
11779
|
size: wmSize,
|
|
10520
11780
|
font: wmFont,
|
|
10521
|
-
color: (0,
|
|
11781
|
+
color: (0, import_pdf_lib4.rgb)(wmRgb.r, wmRgb.g, wmRgb.b),
|
|
10522
11782
|
opacity: wmOpacity,
|
|
10523
|
-
rotate: (0,
|
|
11783
|
+
rotate: (0, import_pdf_lib4.degrees)(wmAngle)
|
|
10524
11784
|
});
|
|
10525
11785
|
}
|
|
10526
11786
|
}
|
|
@@ -10530,9 +11790,9 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10530
11790
|
y: Math.max(40, pageHeight * 0.45),
|
|
10531
11791
|
size: wmSize * 1.5,
|
|
10532
11792
|
font: wmFont,
|
|
10533
|
-
color: (0,
|
|
11793
|
+
color: (0, import_pdf_lib4.rgb)(wmRgb.r, wmRgb.g, wmRgb.b),
|
|
10534
11794
|
opacity: wmOpacity,
|
|
10535
|
-
rotate: (0,
|
|
11795
|
+
rotate: (0, import_pdf_lib4.degrees)(wmAngle)
|
|
10536
11796
|
});
|
|
10537
11797
|
}
|
|
10538
11798
|
} else if (cjkState) {
|
|
@@ -10541,14 +11801,14 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10541
11801
|
for (let wy = 90; wy < pageHeight - 40; wy += isTiled ? 185 : pageHeight) {
|
|
10542
11802
|
for (let wx = 45; wx < pageWidth2 - 50; wx += isTiled ? 225 : pageWidth2) {
|
|
10543
11803
|
pdfPage.pushOperators(
|
|
10544
|
-
(0,
|
|
10545
|
-
(0,
|
|
10546
|
-
(0,
|
|
10547
|
-
(0,
|
|
10548
|
-
(0,
|
|
10549
|
-
(0,
|
|
10550
|
-
(0,
|
|
10551
|
-
(0,
|
|
11804
|
+
(0, import_pdf_lib4.pushGraphicsState)(),
|
|
11805
|
+
(0, import_pdf_lib4.beginText)(),
|
|
11806
|
+
(0, import_pdf_lib4.setFillingRgbColor)(0.78, 0.81, 0.86),
|
|
11807
|
+
(0, import_pdf_lib4.setFontAndSize)(fontKey, wmSize),
|
|
11808
|
+
(0, import_pdf_lib4.rotateAndSkewTextDegreesAndTranslate)(wmAngle, 0, 0, wx, wy),
|
|
11809
|
+
(0, import_pdf_lib4.showText)(import_pdf_lib4.PDFHexString.of(hexWm)),
|
|
11810
|
+
(0, import_pdf_lib4.endText)(),
|
|
11811
|
+
(0, import_pdf_lib4.popGraphicsState)()
|
|
10552
11812
|
);
|
|
10553
11813
|
}
|
|
10554
11814
|
}
|
|
@@ -10570,7 +11830,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10570
11830
|
width: 170,
|
|
10571
11831
|
height: 30,
|
|
10572
11832
|
opacity: 0.15,
|
|
10573
|
-
rotate: (0,
|
|
11833
|
+
rotate: (0, import_pdf_lib4.degrees)(26)
|
|
10574
11834
|
});
|
|
10575
11835
|
}
|
|
10576
11836
|
}
|
|
@@ -10594,7 +11854,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10594
11854
|
}
|
|
10595
11855
|
return void 0;
|
|
10596
11856
|
};
|
|
10597
|
-
return walk(snapshot.metadata?.bookmarks);
|
|
11857
|
+
return walk(snapshot.bookmarks ?? snapshot.metadata?.bookmarks);
|
|
10598
11858
|
};
|
|
10599
11859
|
for (const linkItem of deferredLinkAnnotations) {
|
|
10600
11860
|
const hostPage = allPages[linkItem.hostPageIndex];
|
|
@@ -10609,9 +11869,9 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10609
11869
|
const targetPage = allPages[clampedIdx];
|
|
10610
11870
|
const destArr = pdfDoc.context.obj([
|
|
10611
11871
|
targetPage.ref,
|
|
10612
|
-
|
|
10613
|
-
|
|
10614
|
-
|
|
11872
|
+
import_pdf_lib4.PDFName.of("XYZ"),
|
|
11873
|
+
import_pdf_lib4.PDFNumber.of(elDest.x),
|
|
11874
|
+
import_pdf_lib4.PDFNumber.of(elDest.topY),
|
|
10615
11875
|
null
|
|
10616
11876
|
]);
|
|
10617
11877
|
const actionDict = pdfDoc.context.obj({
|
|
@@ -10640,9 +11900,9 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10640
11900
|
const targetPage = allPages[clampedIdx];
|
|
10641
11901
|
const destArr = pdfDoc.context.obj([
|
|
10642
11902
|
targetPage.ref,
|
|
10643
|
-
|
|
10644
|
-
|
|
10645
|
-
|
|
11903
|
+
import_pdf_lib4.PDFName.of("XYZ"),
|
|
11904
|
+
import_pdf_lib4.PDFNumber.of(0),
|
|
11905
|
+
import_pdf_lib4.PDFNumber.of(targetPage.getHeight()),
|
|
10646
11906
|
null
|
|
10647
11907
|
]);
|
|
10648
11908
|
const actionDict = pdfDoc.context.obj({
|
|
@@ -10670,7 +11930,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10670
11930
|
const actionDict = pdfDoc.context.obj({
|
|
10671
11931
|
Type: "Action",
|
|
10672
11932
|
S: "URI",
|
|
10673
|
-
URI:
|
|
11933
|
+
URI: import_pdf_lib4.PDFString.of(trimmedUrl)
|
|
10674
11934
|
});
|
|
10675
11935
|
const linkAnnot = pdfDoc.context.obj({
|
|
10676
11936
|
Type: "Annot",
|
|
@@ -10683,7 +11943,7 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10683
11943
|
} else if (typeof linkItem.linkPageIndex === "number" && linkItem.linkPageIndex >= 0 && allPages.length > 0) {
|
|
10684
11944
|
const clampedIdx = Math.min(allPages.length - 1, Math.floor(linkItem.linkPageIndex));
|
|
10685
11945
|
const targetPage = allPages[clampedIdx];
|
|
10686
|
-
const destArr = pdfDoc.context.obj([targetPage.ref,
|
|
11946
|
+
const destArr = pdfDoc.context.obj([targetPage.ref, import_pdf_lib4.PDFName.of("XYZ"), null, null, null]);
|
|
10687
11947
|
const linkAnnot = pdfDoc.context.obj({
|
|
10688
11948
|
Type: "Annot",
|
|
10689
11949
|
Subtype: "Link",
|
|
@@ -10695,8 +11955,9 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10695
11955
|
}
|
|
10696
11956
|
}
|
|
10697
11957
|
}
|
|
10698
|
-
|
|
10699
|
-
|
|
11958
|
+
const effectiveBookmarks = snapshot.bookmarks ?? snapshot.metadata?.bookmarks;
|
|
11959
|
+
if (effectiveBookmarks && effectiveBookmarks.length > 0) {
|
|
11960
|
+
attachPdfOutlinesTree(pdfDoc, effectiveBookmarks);
|
|
10700
11961
|
}
|
|
10701
11962
|
const attachments = snapshot.attachments ?? snapshot.metadata?.attachments ?? [];
|
|
10702
11963
|
if (attachments.length > 0) {
|
|
@@ -10708,28 +11969,28 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10708
11969
|
const pieceDict = {};
|
|
10709
11970
|
if (sigEnv) {
|
|
10710
11971
|
pieceDict.UniverSignature = {
|
|
10711
|
-
LastModified:
|
|
11972
|
+
LastModified: import_pdf_lib4.PDFString.of(sigEnv.signedAt || (/* @__PURE__ */ new Date()).toISOString()),
|
|
10712
11973
|
Private: pdfDoc.context.obj({
|
|
10713
|
-
Algorithm:
|
|
10714
|
-
Signer:
|
|
10715
|
-
CertificateSubject:
|
|
10716
|
-
ContentDigest:
|
|
10717
|
-
EnvelopeDigest:
|
|
11974
|
+
Algorithm: import_pdf_lib4.PDFName.of("SHA-256"),
|
|
11975
|
+
Signer: import_pdf_lib4.PDFHexString.fromText(sigEnv.signerName),
|
|
11976
|
+
CertificateSubject: import_pdf_lib4.PDFHexString.fromText(sigEnv.certificateSubject),
|
|
11977
|
+
ContentDigest: import_pdf_lib4.PDFString.of(sigEnv.contentDigestHex),
|
|
11978
|
+
EnvelopeDigest: import_pdf_lib4.PDFString.of(sigEnv.envelopeDigestHex)
|
|
10718
11979
|
})
|
|
10719
11980
|
};
|
|
10720
11981
|
}
|
|
10721
11982
|
if (secPerms) {
|
|
10722
11983
|
pieceDict.UniverSecurity = {
|
|
10723
|
-
LastModified:
|
|
11984
|
+
LastModified: import_pdf_lib4.PDFString.of((/* @__PURE__ */ new Date()).toISOString()),
|
|
10724
11985
|
Private: pdfDoc.context.obj({
|
|
10725
|
-
AllowPrint: secPerms.allowPrint !== false ?
|
|
10726
|
-
AllowCopy: secPerms.allowCopy !== false ?
|
|
10727
|
-
AllowModify: secPerms.allowModify !== false ?
|
|
10728
|
-
...secPerms.openPasswordHash ? { OpenPasswordHash:
|
|
11986
|
+
AllowPrint: secPerms.allowPrint !== false ? import_pdf_lib4.PDFBool.True : import_pdf_lib4.PDFBool.False,
|
|
11987
|
+
AllowCopy: secPerms.allowCopy !== false ? import_pdf_lib4.PDFBool.True : import_pdf_lib4.PDFBool.False,
|
|
11988
|
+
AllowModify: secPerms.allowModify !== false ? import_pdf_lib4.PDFBool.True : import_pdf_lib4.PDFBool.False,
|
|
11989
|
+
...secPerms.openPasswordHash ? { OpenPasswordHash: import_pdf_lib4.PDFString.of(secPerms.openPasswordHash) } : {}
|
|
10729
11990
|
})
|
|
10730
11991
|
};
|
|
10731
11992
|
}
|
|
10732
|
-
pdfDoc.catalog.set(
|
|
11993
|
+
pdfDoc.catalog.set(import_pdf_lib4.PDFName.of("PieceInfo"), pdfDoc.context.obj(pieceDict));
|
|
10733
11994
|
}
|
|
10734
11995
|
if (accessibilityTagged) {
|
|
10735
11996
|
attachTaggedStructTreeRoot(pdfDoc, snapshot, taggedStructItems, totalPhysicalPages);
|
|
@@ -10740,40 +12001,49 @@ async function exportPdfBytes(snapshot, runtimeData, options) {
|
|
|
10740
12001
|
if (interactiveForm && hasAnyAcroWidget) {
|
|
10741
12002
|
const acroDict = pdfDoc.getForm().acroForm.dict;
|
|
10742
12003
|
const helv = await ensureAcroHelvetica();
|
|
10743
|
-
let drDict = pdfDoc.context.lookupMaybe(acroDict.get(
|
|
12004
|
+
let drDict = pdfDoc.context.lookupMaybe(acroDict.get(import_pdf_lib4.PDFName.of("DR")), import_pdf_lib4.PDFDict);
|
|
10744
12005
|
if (!drDict) {
|
|
10745
12006
|
drDict = pdfDoc.context.obj({});
|
|
10746
|
-
acroDict.set(
|
|
12007
|
+
acroDict.set(import_pdf_lib4.PDFName.of("DR"), drDict);
|
|
10747
12008
|
}
|
|
10748
|
-
let fontDict = pdfDoc.context.lookupMaybe(drDict.get(
|
|
12009
|
+
let fontDict = pdfDoc.context.lookupMaybe(drDict.get(import_pdf_lib4.PDFName.of("Font")), import_pdf_lib4.PDFDict);
|
|
10749
12010
|
if (!fontDict) {
|
|
10750
12011
|
fontDict = pdfDoc.context.obj({});
|
|
10751
|
-
drDict.set(
|
|
12012
|
+
drDict.set(import_pdf_lib4.PDFName.of("Font"), fontDict);
|
|
10752
12013
|
}
|
|
10753
|
-
fontDict.set(
|
|
12014
|
+
fontDict.set(import_pdf_lib4.PDFName.of("Helv"), helv.ref);
|
|
10754
12015
|
if (acroCjkState) {
|
|
10755
|
-
fontDict.set(
|
|
12016
|
+
fontDict.set(import_pdf_lib4.PDFName.of("FCjk"), acroCjkState.type0FontRef);
|
|
10756
12017
|
}
|
|
10757
12018
|
acroDict.set(
|
|
10758
|
-
|
|
10759
|
-
|
|
12019
|
+
import_pdf_lib4.PDFName.of("DA"),
|
|
12020
|
+
import_pdf_lib4.PDFString.of(acroCjkState ? "/FCjk 11 Tf 0 0 0 rg" : "/Helv 11 Tf 0 0 0 rg")
|
|
10760
12021
|
);
|
|
10761
|
-
acroDict.delete(
|
|
12022
|
+
acroDict.delete(import_pdf_lib4.PDFName.of("NeedAppearances"));
|
|
10762
12023
|
}
|
|
10763
12024
|
if (optimizeSize) {
|
|
10764
12025
|
pruneUnreferencedObjectsAndCompressStreams(pdfDoc);
|
|
10765
12026
|
}
|
|
12027
|
+
const effectiveSecurity = options?.security ?? snapshot.security ?? snapshot.metadata?.security;
|
|
12028
|
+
const shouldEncrypt = isPdfSecurityActive(effectiveSecurity);
|
|
10766
12029
|
throwIfExportAborted();
|
|
12030
|
+
if (shouldEncrypt && !options?.diagnosticGridOverlay) {
|
|
12031
|
+
await applyPdfSecurityToDocument(pdfDoc, effectiveSecurity);
|
|
12032
|
+
const encryptedSavedBytes = interactiveForm && hasAnyAcroWidget ? await pdfDoc.save({ useObjectStreams: false, updateFieldAppearances: false }) : await pdfDoc.save({ useObjectStreams: false });
|
|
12033
|
+
throwIfExportAborted();
|
|
12034
|
+
return encryptedSavedBytes;
|
|
12035
|
+
}
|
|
10767
12036
|
const savedPdfBytes = interactiveForm && hasAnyAcroWidget ? await pdfDoc.save({ updateFieldAppearances: false }) : await pdfDoc.save();
|
|
10768
12037
|
throwIfExportAborted();
|
|
10769
12038
|
if (options?.diagnosticGridOverlay) {
|
|
10770
12039
|
const gridOpts = typeof options.diagnosticGridOverlay === "object" ? options.diagnosticGridOverlay : void 0;
|
|
10771
|
-
|
|
12040
|
+
const overlaidBytes = await overlayDiagnosticGridOnPdfBytes(savedPdfBytes, snapshot, gridOpts);
|
|
12041
|
+
return shouldEncrypt ? await encryptPdfBytes(overlaidBytes, effectiveSecurity) : overlaidBytes;
|
|
10772
12042
|
}
|
|
10773
12043
|
return savedPdfBytes;
|
|
10774
12044
|
}
|
|
10775
12045
|
async function overlayDiagnosticGridOnPdfBytes(pdfBytes, snapshot, options) {
|
|
10776
|
-
const pdfDoc = await
|
|
12046
|
+
const pdfDoc = await import_pdf_lib4.PDFDocument.load(pdfBytes, { ignoreEncryption: true });
|
|
10777
12047
|
const pages = pdfDoc.getPages();
|
|
10778
12048
|
if (pages.length === 0) {
|
|
10779
12049
|
return await pdfDoc.save({ updateFieldAppearances: false });
|
|
@@ -10785,14 +12055,14 @@ async function overlayDiagnosticGridOnPdfBytes(pdfBytes, snapshot, options) {
|
|
|
10785
12055
|
const showElementBounds = options?.showElementBounds !== false;
|
|
10786
12056
|
const showElementLabels = options?.showElementLabels !== false;
|
|
10787
12057
|
const gridOpacity = Math.max(0.05, Math.min(1, options?.gridOpacity ?? 0.35));
|
|
10788
|
-
const fontRegular = await pdfDoc.embedFont(
|
|
10789
|
-
const fontBold = await pdfDoc.embedFont(
|
|
12058
|
+
const fontRegular = await pdfDoc.embedFont(import_pdf_lib4.StandardFonts.Helvetica);
|
|
12059
|
+
const fontBold = await pdfDoc.embedFont(import_pdf_lib4.StandardFonts.HelveticaBold);
|
|
10790
12060
|
let lazyCjkState = null;
|
|
10791
|
-
const minorCol = (0,
|
|
10792
|
-
const majorCol = (0,
|
|
10793
|
-
const rulerTextCol = (0,
|
|
10794
|
-
const boxCol = (0,
|
|
10795
|
-
const badgeBgCol = (0,
|
|
12061
|
+
const minorCol = (0, import_pdf_lib4.rgb)(0.49, 0.525, 0.596);
|
|
12062
|
+
const majorCol = (0, import_pdf_lib4.rgb)(0.173, 0.325, 0.945);
|
|
12063
|
+
const rulerTextCol = (0, import_pdf_lib4.rgb)(0.192, 0.212, 0.247);
|
|
12064
|
+
const boxCol = (0, import_pdf_lib4.rgb)(0.173, 0.325, 0.945);
|
|
12065
|
+
const badgeBgCol = (0, import_pdf_lib4.rgb)(0.953, 0.961, 1);
|
|
10796
12066
|
const staticEls = unitSnapshot?.document?.metadata?.staticSchemaElements ?? unitSnapshot?.metadata?.staticSchemaElements ?? [];
|
|
10797
12067
|
for (let pIdx = 0; pIdx < pages.length; pIdx++) {
|
|
10798
12068
|
const page = pages[pIdx];
|