@yudaocode/univer-pdfs 1.6.210 → 1.6.212
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/lib/cjs/index.js +499 -40
- package/lib/cjs/index.js.map +1 -1
- package/lib/es/index.js +512 -46
- package/lib/es/index.js.map +1 -1
- package/lib/types/index.d.ts +75 -5
- package/package.json +1 -1
package/lib/es/index.js
CHANGED
|
@@ -6869,13 +6869,22 @@ import {
|
|
|
6869
6869
|
ICommandService as ICommandService2,
|
|
6870
6870
|
Inject,
|
|
6871
6871
|
Injector,
|
|
6872
|
-
IUniverInstanceService as
|
|
6872
|
+
IUniverInstanceService as IUniverInstanceService5,
|
|
6873
6873
|
Plugin,
|
|
6874
|
-
UniverInstanceType as
|
|
6874
|
+
UniverInstanceType as UniverInstanceType5
|
|
6875
6875
|
} from "@univerjs/core";
|
|
6876
6876
|
|
|
6877
6877
|
// src/services/pdf-io.service.ts
|
|
6878
|
-
import {
|
|
6878
|
+
import {
|
|
6879
|
+
CanceledError,
|
|
6880
|
+
createIdentifier as createIdentifier2,
|
|
6881
|
+
Disposable as Disposable2,
|
|
6882
|
+
generateRandomId as generateRandomId6,
|
|
6883
|
+
IUniverInstanceService as IUniverInstanceService4,
|
|
6884
|
+
Optional,
|
|
6885
|
+
Tools as Tools5,
|
|
6886
|
+
UniverInstanceType as UniverInstanceType4
|
|
6887
|
+
} from "@univerjs/core";
|
|
6879
6888
|
import { BehaviorSubject as BehaviorSubject2, Subject } from "rxjs";
|
|
6880
6889
|
function uint8ToBase64(bytes) {
|
|
6881
6890
|
let binary = "";
|
|
@@ -7026,6 +7035,271 @@ function hasPdfMagicHeader(bytes) {
|
|
|
7026
7035
|
}
|
|
7027
7036
|
return false;
|
|
7028
7037
|
}
|
|
7038
|
+
function countJsInActionDict(actionDict, doc, PDFArrayCtor, PDFDictCtor, PDFNameCtor, visited = /* @__PURE__ */ new Set()) {
|
|
7039
|
+
if (!actionDict || visited.has(actionDict)) return 0;
|
|
7040
|
+
visited.add(actionDict);
|
|
7041
|
+
let count = 0;
|
|
7042
|
+
const actionType = actionDict.get?.(PDFNameCtor.of("S"))?.toString?.() || "";
|
|
7043
|
+
const hasJsEntry = actionDict.get?.(PDFNameCtor.of("JS")) !== void 0;
|
|
7044
|
+
if (actionType === "/JavaScript" || hasJsEntry) {
|
|
7045
|
+
count += 1;
|
|
7046
|
+
}
|
|
7047
|
+
const nextObj = actionDict.lookup?.(PDFNameCtor.of("Next"));
|
|
7048
|
+
if (nextObj instanceof PDFDictCtor) {
|
|
7049
|
+
count += countJsInActionDict(nextObj, doc, PDFArrayCtor, PDFDictCtor, PDFNameCtor, visited);
|
|
7050
|
+
} else if (nextObj instanceof PDFArrayCtor) {
|
|
7051
|
+
for (let i = 0; i < nextObj.size(); i++) {
|
|
7052
|
+
const sub = nextObj.lookupMaybe(i, PDFDictCtor);
|
|
7053
|
+
if (sub) {
|
|
7054
|
+
count += countJsInActionDict(sub, doc, PDFArrayCtor, PDFDictCtor, PDFNameCtor, visited);
|
|
7055
|
+
}
|
|
7056
|
+
}
|
|
7057
|
+
}
|
|
7058
|
+
return count;
|
|
7059
|
+
}
|
|
7060
|
+
function inspectPdfDocumentImportWarnings(doc, pdfLib) {
|
|
7061
|
+
const { PDFArray: PDFArray4, PDFDict: PDFDict4, PDFName: PDFName4 } = pdfLib;
|
|
7062
|
+
const warnings = [];
|
|
7063
|
+
let acroFieldCount = 0;
|
|
7064
|
+
const acroFormDict = doc.catalog.lookupMaybe(PDFName4.of("AcroForm"), PDFDict4);
|
|
7065
|
+
if (acroFormDict) {
|
|
7066
|
+
try {
|
|
7067
|
+
acroFieldCount = doc.getForm().getFields().length;
|
|
7068
|
+
} catch {
|
|
7069
|
+
const fieldsArr = acroFormDict.lookupMaybe(PDFName4.of("Fields"), PDFArray4);
|
|
7070
|
+
if (fieldsArr) {
|
|
7071
|
+
acroFieldCount = fieldsArr.size();
|
|
7072
|
+
}
|
|
7073
|
+
}
|
|
7074
|
+
}
|
|
7075
|
+
let jsCount = 0;
|
|
7076
|
+
const namesDict = doc.catalog.lookupMaybe(PDFName4.of("Names"), PDFDict4);
|
|
7077
|
+
const jsNameTree = namesDict?.lookupMaybe(PDFName4.of("JavaScript"), PDFDict4);
|
|
7078
|
+
if (jsNameTree) {
|
|
7079
|
+
const namesArr = jsNameTree.lookupMaybe(PDFName4.of("Names"), PDFArray4);
|
|
7080
|
+
jsCount += namesArr ? Math.max(1, Math.floor(namesArr.size() / 2)) : 1;
|
|
7081
|
+
}
|
|
7082
|
+
const openAction = doc.catalog.lookupMaybe(PDFName4.of("OpenAction"), PDFDict4);
|
|
7083
|
+
if (openAction) {
|
|
7084
|
+
jsCount += countJsInActionDict(openAction, doc, PDFArray4, PDFDict4, PDFName4);
|
|
7085
|
+
}
|
|
7086
|
+
const catalogAA = doc.catalog.lookupMaybe(PDFName4.of("AA"), PDFDict4);
|
|
7087
|
+
if (catalogAA) {
|
|
7088
|
+
for (const [, actionRef] of catalogAA.entries()) {
|
|
7089
|
+
const act = doc.context.lookupMaybe(actionRef, PDFDict4);
|
|
7090
|
+
if (act) {
|
|
7091
|
+
jsCount += countJsInActionDict(act, doc, PDFArray4, PDFDict4, PDFName4);
|
|
7092
|
+
}
|
|
7093
|
+
}
|
|
7094
|
+
}
|
|
7095
|
+
let activeAnnotationCount = 0;
|
|
7096
|
+
let orphanWidgetCount = 0;
|
|
7097
|
+
for (const page of doc.getPages()) {
|
|
7098
|
+
const pageAA = page.node.lookupMaybe(PDFName4.of("AA"), PDFDict4);
|
|
7099
|
+
if (pageAA) {
|
|
7100
|
+
for (const [, actionRef] of pageAA.entries()) {
|
|
7101
|
+
const act = doc.context.lookupMaybe(actionRef, PDFDict4);
|
|
7102
|
+
if (act) {
|
|
7103
|
+
jsCount += countJsInActionDict(act, doc, PDFArray4, PDFDict4, PDFName4);
|
|
7104
|
+
}
|
|
7105
|
+
}
|
|
7106
|
+
}
|
|
7107
|
+
const annots = page.node.Annots();
|
|
7108
|
+
if (annots instanceof PDFArray4) {
|
|
7109
|
+
for (let i = 0; i < annots.size(); i++) {
|
|
7110
|
+
const annot = annots.lookupMaybe(i, PDFDict4);
|
|
7111
|
+
if (!annot) continue;
|
|
7112
|
+
const subtypeObj = annot.get(PDFName4.of("Subtype"));
|
|
7113
|
+
const subtype = subtypeObj ? subtypeObj.toString() : "";
|
|
7114
|
+
if (subtype === "/Widget") {
|
|
7115
|
+
orphanWidgetCount += 1;
|
|
7116
|
+
} else if (subtype !== "/Popup") {
|
|
7117
|
+
activeAnnotationCount += 1;
|
|
7118
|
+
}
|
|
7119
|
+
const annotAction = annot.lookupMaybe(PDFName4.of("A"), PDFDict4);
|
|
7120
|
+
if (annotAction) {
|
|
7121
|
+
jsCount += countJsInActionDict(annotAction, doc, PDFArray4, PDFDict4, PDFName4);
|
|
7122
|
+
}
|
|
7123
|
+
const annotAA = annot.lookupMaybe(PDFName4.of("AA"), PDFDict4);
|
|
7124
|
+
if (annotAA) {
|
|
7125
|
+
for (const [, actionRef] of annotAA.entries()) {
|
|
7126
|
+
const act = doc.context.lookupMaybe(actionRef, PDFDict4);
|
|
7127
|
+
if (act) {
|
|
7128
|
+
jsCount += countJsInActionDict(act, doc, PDFArray4, PDFDict4, PDFName4);
|
|
7129
|
+
}
|
|
7130
|
+
}
|
|
7131
|
+
}
|
|
7132
|
+
}
|
|
7133
|
+
}
|
|
7134
|
+
}
|
|
7135
|
+
const totalAcroFields = acroFieldCount > 0 ? acroFieldCount : orphanWidgetCount;
|
|
7136
|
+
if (totalAcroFields > 0) {
|
|
7137
|
+
warnings.push({
|
|
7138
|
+
code: "ACROFORM_FIELDS_DETECTED",
|
|
7139
|
+
message: `\u68C0\u6D4B\u5230 ${totalAcroFields} \u4E2A\u4EA4\u4E92\u5F0F AcroForm \u8868\u5355\u57DF\uFF0C\u5E95\u56FE\u53E0\u52A0\u6A21\u5F0F\u4E0B\u5C06\u6309\u9759\u6001\u9875\u9762\u4FDD\u7559\u3002`,
|
|
7140
|
+
count: totalAcroFields
|
|
7141
|
+
});
|
|
7142
|
+
}
|
|
7143
|
+
if (jsCount > 0) {
|
|
7144
|
+
warnings.push({
|
|
7145
|
+
code: "EMBEDDED_JAVASCRIPT_DETECTED",
|
|
7146
|
+
message: `\u68C0\u6D4B\u5230 ${jsCount} \u5904\u5185\u5D4C PDF JavaScript \u811A\u672C\uFF0C\u5BFC\u5165\u4E0E\u5BFC\u51FA\u65F6\u5DF2\u7981\u7528\u811A\u672C\u6267\u884C\u3002`,
|
|
7147
|
+
count: jsCount
|
|
7148
|
+
});
|
|
7149
|
+
}
|
|
7150
|
+
if (activeAnnotationCount > 0) {
|
|
7151
|
+
warnings.push({
|
|
7152
|
+
code: "ACTIVE_ANNOTATIONS_DETECTED",
|
|
7153
|
+
message: `\u68C0\u6D4B\u5230 ${activeAnnotationCount} \u4E2A\u6D3B\u52A8\u6279\u6CE8\u6216\u94FE\u63A5\u5BF9\u8C61\uFF0C\u5E95\u56FE\u53E0\u52A0\u6A21\u5F0F\u4E0B\u5C06\u6309\u9759\u6001\u5916\u89C2\u4FDD\u7559\u3002`,
|
|
7154
|
+
count: activeAnnotationCount
|
|
7155
|
+
});
|
|
7156
|
+
}
|
|
7157
|
+
return warnings;
|
|
7158
|
+
}
|
|
7159
|
+
function normalizePdfImportWarnings(raw) {
|
|
7160
|
+
if (!Array.isArray(raw)) return [];
|
|
7161
|
+
const normalized = [];
|
|
7162
|
+
for (let i = 0; i < raw.length; i++) {
|
|
7163
|
+
const item = raw[i];
|
|
7164
|
+
if (typeof item === "string") {
|
|
7165
|
+
const trimmed = item.trim();
|
|
7166
|
+
if (trimmed) {
|
|
7167
|
+
normalized.push({
|
|
7168
|
+
code: `IMPORT_WARNING_${i + 1}`,
|
|
7169
|
+
message: trimmed
|
|
7170
|
+
});
|
|
7171
|
+
}
|
|
7172
|
+
} else if (item && typeof item === "object" && typeof item.message === "string") {
|
|
7173
|
+
normalized.push({
|
|
7174
|
+
code: item.code || `IMPORT_WARNING_${i + 1}`,
|
|
7175
|
+
message: item.message,
|
|
7176
|
+
...typeof item.count === "number" ? { count: item.count } : {}
|
|
7177
|
+
});
|
|
7178
|
+
}
|
|
7179
|
+
}
|
|
7180
|
+
return normalized;
|
|
7181
|
+
}
|
|
7182
|
+
function buildOverlayImportCandidateSnapshot(baseSnapshot, outcome) {
|
|
7183
|
+
const prevSnapshot = Tools5.deepClone(baseSnapshot);
|
|
7184
|
+
const existingOrder = prevSnapshot.document?.pageOrder ?? [];
|
|
7185
|
+
const pageSizes = outcome.pageSizes ?? [];
|
|
7186
|
+
const totalPages = Math.max(1, pageSizes.length || outcome.sourceDocumentRef.pageCount || 1);
|
|
7187
|
+
const nextPages = {};
|
|
7188
|
+
const nextPageOrder = [];
|
|
7189
|
+
const nextElementsByPage = Tools5.deepClone(prevSnapshot.editState?.elementsByPage ?? {});
|
|
7190
|
+
const nextElementOrderByPage = Tools5.deepClone(prevSnapshot.editState?.elementOrderByPage ?? {});
|
|
7191
|
+
for (let i = 0; i < totalPages; i++) {
|
|
7192
|
+
const pid = existingOrder[i] || `page-${generateRandomId6(6)}`;
|
|
7193
|
+
nextPageOrder.push(pid);
|
|
7194
|
+
const resolvedSize = pageSizes[i] ?? pageSizes[0] ?? prevSnapshot.document.pageSize;
|
|
7195
|
+
nextPages[pid] = {
|
|
7196
|
+
id: pid,
|
|
7197
|
+
title: `Page ${i + 1}`,
|
|
7198
|
+
sourcePageNumber: i + 1,
|
|
7199
|
+
pageSize: Tools5.deepClone(resolvedSize),
|
|
7200
|
+
backgroundColor: "#ffffff"
|
|
7201
|
+
};
|
|
7202
|
+
if (!nextElementsByPage[pid]) nextElementsByPage[pid] = {};
|
|
7203
|
+
if (!nextElementOrderByPage[pid]) nextElementOrderByPage[pid] = [];
|
|
7204
|
+
}
|
|
7205
|
+
return {
|
|
7206
|
+
...prevSnapshot,
|
|
7207
|
+
sourceDocumentRef: Tools5.deepClone(outcome.sourceDocumentRef),
|
|
7208
|
+
document: {
|
|
7209
|
+
...prevSnapshot.document,
|
|
7210
|
+
pageSize: Tools5.deepClone(pageSizes[0] ?? prevSnapshot.document.pageSize),
|
|
7211
|
+
pages: nextPages,
|
|
7212
|
+
pageOrder: nextPageOrder
|
|
7213
|
+
},
|
|
7214
|
+
editState: {
|
|
7215
|
+
...prevSnapshot.editState,
|
|
7216
|
+
elementsByPage: nextElementsByPage,
|
|
7217
|
+
elementOrderByPage: nextElementOrderByPage
|
|
7218
|
+
}
|
|
7219
|
+
};
|
|
7220
|
+
}
|
|
7221
|
+
function normalizeAdapterImportResult(raw) {
|
|
7222
|
+
if (!raw || typeof raw !== "object") {
|
|
7223
|
+
throw new Error("[UniverPdfs] Invalid importPdf result: expected an IPdfImportOutcome or IPdfUnitData object.");
|
|
7224
|
+
}
|
|
7225
|
+
const record = raw;
|
|
7226
|
+
if (record.mode === "overlay") {
|
|
7227
|
+
const warnings = normalizePdfImportWarnings(record.warnings);
|
|
7228
|
+
return {
|
|
7229
|
+
outcome: {
|
|
7230
|
+
mode: "overlay",
|
|
7231
|
+
sourceDocumentRef: Tools5.deepClone(record.sourceDocumentRef),
|
|
7232
|
+
pageSizes: Tools5.deepClone(record.pageSizes ?? []),
|
|
7233
|
+
...warnings.length > 0 ? { warnings } : {}
|
|
7234
|
+
},
|
|
7235
|
+
warnings
|
|
7236
|
+
};
|
|
7237
|
+
}
|
|
7238
|
+
if (record.mode === "snapshot") {
|
|
7239
|
+
const warnings = normalizePdfImportWarnings(record.warnings);
|
|
7240
|
+
const clonedSnapshot = Tools5.deepClone(record.snapshot);
|
|
7241
|
+
return {
|
|
7242
|
+
outcome: {
|
|
7243
|
+
mode: "snapshot",
|
|
7244
|
+
snapshot: clonedSnapshot,
|
|
7245
|
+
...warnings.length > 0 ? { warnings } : {}
|
|
7246
|
+
},
|
|
7247
|
+
warnings
|
|
7248
|
+
};
|
|
7249
|
+
}
|
|
7250
|
+
if (record.snapshot && typeof record.snapshot === "object") {
|
|
7251
|
+
const warnings = normalizePdfImportWarnings(record.warnings);
|
|
7252
|
+
const clonedSnapshot = Tools5.deepClone(record.snapshot);
|
|
7253
|
+
return {
|
|
7254
|
+
outcome: {
|
|
7255
|
+
mode: "snapshot",
|
|
7256
|
+
snapshot: clonedSnapshot,
|
|
7257
|
+
...warnings.length > 0 ? { warnings } : {}
|
|
7258
|
+
},
|
|
7259
|
+
warnings
|
|
7260
|
+
};
|
|
7261
|
+
}
|
|
7262
|
+
if ("schema" in record || "document" in record) {
|
|
7263
|
+
const warnings = normalizePdfImportWarnings(record.warnings);
|
|
7264
|
+
const clonedSnapshot = Tools5.deepClone(record);
|
|
7265
|
+
delete clonedSnapshot.warnings;
|
|
7266
|
+
return {
|
|
7267
|
+
outcome: {
|
|
7268
|
+
mode: "snapshot",
|
|
7269
|
+
snapshot: clonedSnapshot,
|
|
7270
|
+
...warnings.length > 0 ? { warnings } : {}
|
|
7271
|
+
},
|
|
7272
|
+
warnings
|
|
7273
|
+
};
|
|
7274
|
+
}
|
|
7275
|
+
throw new Error("[UniverPdfs] Unsupported importPdf adapter return payload.");
|
|
7276
|
+
}
|
|
7277
|
+
function normalizeFormatKeys(formats) {
|
|
7278
|
+
if (!Array.isArray(formats)) return [];
|
|
7279
|
+
return formats.map((f) => String(f || "").trim().replace(/^\./, "").toLowerCase()).filter(Boolean);
|
|
7280
|
+
}
|
|
7281
|
+
function detectImportFileFormat(file) {
|
|
7282
|
+
const lowerName = String(file?.name || "").toLowerCase();
|
|
7283
|
+
const mime = String(file?.type || "").toLowerCase();
|
|
7284
|
+
if (lowerName.endsWith(".json") || mime === "application/json") {
|
|
7285
|
+
return "json";
|
|
7286
|
+
}
|
|
7287
|
+
return "pdf";
|
|
7288
|
+
}
|
|
7289
|
+
function attachSuggestedFallbackAndThrow(err, fallback) {
|
|
7290
|
+
if (err && typeof err === "object") {
|
|
7291
|
+
if (!err.suggestedFallback) {
|
|
7292
|
+
try {
|
|
7293
|
+
err.suggestedFallback = fallback;
|
|
7294
|
+
} catch {
|
|
7295
|
+
}
|
|
7296
|
+
}
|
|
7297
|
+
throw err;
|
|
7298
|
+
}
|
|
7299
|
+
const wrapped = new Error(String(err));
|
|
7300
|
+
wrapped.suggestedFallback = fallback;
|
|
7301
|
+
throw wrapped;
|
|
7302
|
+
}
|
|
7029
7303
|
async function importPdfSource(source, options) {
|
|
7030
7304
|
let bytes;
|
|
7031
7305
|
let fileName = options?.fileName || "imported.pdf";
|
|
@@ -7069,8 +7343,9 @@ async function importPdfSource(source, options) {
|
|
|
7069
7343
|
"Invalid PDF binary: missing %PDF- header signature."
|
|
7070
7344
|
);
|
|
7071
7345
|
}
|
|
7072
|
-
const { PDFDocument: PDFDocument4 } = await import("pdf-lib");
|
|
7346
|
+
const { PDFArray: PDFArray4, PDFDict: PDFDict4, PDFDocument: PDFDocument4, PDFName: PDFName4 } = await import("pdf-lib");
|
|
7073
7347
|
let pageSizes = [];
|
|
7348
|
+
let warnings = [];
|
|
7074
7349
|
try {
|
|
7075
7350
|
const doc = await PDFDocument4.load(bytes);
|
|
7076
7351
|
if (doc.isEncrypted && !options?.password) {
|
|
@@ -7087,6 +7362,7 @@ async function importPdfSource(source, options) {
|
|
|
7087
7362
|
const { width, height } = p.getSize();
|
|
7088
7363
|
return { width, height, preset: "CUSTOM" };
|
|
7089
7364
|
});
|
|
7365
|
+
warnings = inspectPdfDocumentImportWarnings(doc, { PDFArray: PDFArray4, PDFDict: PDFDict4, PDFName: PDFName4 });
|
|
7090
7366
|
} catch (err) {
|
|
7091
7367
|
if (err instanceof PdfImportError) {
|
|
7092
7368
|
throw err;
|
|
@@ -7154,6 +7430,7 @@ async function importPdfSource(source, options) {
|
|
|
7154
7430
|
const { width, height } = p.getSize();
|
|
7155
7431
|
return { width, height, preset: "CUSTOM" };
|
|
7156
7432
|
});
|
|
7433
|
+
warnings = inspectPdfDocumentImportWarnings(doc, { PDFArray: PDFArray4, PDFDict: PDFDict4, PDFName: PDFName4 });
|
|
7157
7434
|
} catch (fallbackErr) {
|
|
7158
7435
|
if (fallbackErr instanceof PdfImportError) throw fallbackErr;
|
|
7159
7436
|
throw new PdfImportError(
|
|
@@ -7178,15 +7455,57 @@ async function importPdfSource(source, options) {
|
|
|
7178
7455
|
dataUri,
|
|
7179
7456
|
pageCount: pageSizes.length
|
|
7180
7457
|
},
|
|
7181
|
-
pageSizes
|
|
7458
|
+
pageSizes,
|
|
7459
|
+
warnings
|
|
7182
7460
|
};
|
|
7183
7461
|
}
|
|
7184
|
-
async function
|
|
7185
|
-
|
|
7462
|
+
async function fallbackReadFileAsOutcome(file, baseSnapshot, password) {
|
|
7463
|
+
if (detectImportFileFormat(file) === "json") {
|
|
7464
|
+
const rawText = typeof file.text === "function" ? await file.text() : new TextDecoder().decode(await file.arrayBuffer());
|
|
7465
|
+
let parsed;
|
|
7466
|
+
try {
|
|
7467
|
+
parsed = JSON.parse(rawText);
|
|
7468
|
+
} catch (err) {
|
|
7469
|
+
throw new PdfImportError(
|
|
7470
|
+
"INVALID_PDF_HEADER",
|
|
7471
|
+
`JSON \u6A21\u677F\u89E3\u6790\u5931\u8D25: ${err?.message || String(err)}`
|
|
7472
|
+
);
|
|
7473
|
+
}
|
|
7474
|
+
if (parsed?.schema === UNIVER_PDF_SCHEMA || parsed?.document && parsed?.editState) {
|
|
7475
|
+
return {
|
|
7476
|
+
mode: "snapshot",
|
|
7477
|
+
snapshot: createDefaultPdfUnitData({
|
|
7478
|
+
...parsed,
|
|
7479
|
+
id: parsed.id || baseSnapshot.id
|
|
7480
|
+
})
|
|
7481
|
+
};
|
|
7482
|
+
}
|
|
7483
|
+
if (parsed?.schemas && Array.isArray(parsed.schemas)) {
|
|
7484
|
+
return {
|
|
7485
|
+
mode: "snapshot",
|
|
7486
|
+
snapshot: fromPdfmeTemplate(parsed, {
|
|
7487
|
+
id: baseSnapshot.id,
|
|
7488
|
+
name: (file.name || baseSnapshot.name || "Imported Template").replace(/\.json$/i, "")
|
|
7489
|
+
})
|
|
7490
|
+
};
|
|
7491
|
+
}
|
|
7492
|
+
throw new PdfImportError(
|
|
7493
|
+
"INVALID_PDF_HEADER",
|
|
7494
|
+
"\u672A\u8BC6\u522B\u7684 JSON \u6A21\u677F\u683C\u5F0F\uFF1A\u8BF7\u63D0\u4F9B\u6807\u51C6 IPdfUnitData \u6216 pdfme Template JSON\u3002"
|
|
7495
|
+
);
|
|
7496
|
+
}
|
|
7497
|
+
const imported = await importPdfSource(file, { fileName: file.name || "imported.pdf", password });
|
|
7498
|
+
return {
|
|
7499
|
+
mode: "overlay",
|
|
7500
|
+
sourceDocumentRef: imported.sourceDocumentRef,
|
|
7501
|
+
pageSizes: imported.pageSizes,
|
|
7502
|
+
...imported.warnings?.length ? { warnings: imported.warnings } : {}
|
|
7503
|
+
};
|
|
7186
7504
|
}
|
|
7187
7505
|
var PdfIoService = class extends Disposable2 {
|
|
7188
|
-
constructor() {
|
|
7506
|
+
constructor(_univerInstanceService) {
|
|
7189
7507
|
super();
|
|
7508
|
+
this._univerInstanceService = _univerInstanceService;
|
|
7190
7509
|
this._adapter$ = new BehaviorSubject2({ name: "frontend-default" });
|
|
7191
7510
|
this.adapter$ = this._adapter$.asObservable();
|
|
7192
7511
|
this._exported$ = new Subject();
|
|
@@ -7239,17 +7558,33 @@ var PdfIoService = class extends Disposable2 {
|
|
|
7239
7558
|
return this._adapter$.getValue();
|
|
7240
7559
|
}
|
|
7241
7560
|
setAdapter(adapter) {
|
|
7561
|
+
if (adapter.defaultIoTarget !== void 0) {
|
|
7562
|
+
this._serviceDefaultIoTarget = void 0;
|
|
7563
|
+
}
|
|
7242
7564
|
this._adapter$.next({
|
|
7243
7565
|
name: adapter.name ?? "custom-adapter",
|
|
7244
7566
|
...adapter
|
|
7245
7567
|
});
|
|
7246
7568
|
}
|
|
7247
7569
|
patchAdapter(patch) {
|
|
7570
|
+
if (patch.defaultIoTarget !== void 0) {
|
|
7571
|
+
this._serviceDefaultIoTarget = void 0;
|
|
7572
|
+
}
|
|
7248
7573
|
this._adapter$.next({
|
|
7249
7574
|
...this._adapter$.getValue(),
|
|
7250
7575
|
...patch
|
|
7251
7576
|
});
|
|
7252
7577
|
}
|
|
7578
|
+
getDefaultIoTarget() {
|
|
7579
|
+
return this._serviceDefaultIoTarget ?? this._adapter$.getValue().defaultIoTarget ?? "auto";
|
|
7580
|
+
}
|
|
7581
|
+
setDefaultIoTarget(target) {
|
|
7582
|
+
const normalized = target === "browser" || target === "server" || target === "auto" ? target : "auto";
|
|
7583
|
+
this._serviceDefaultIoTarget = normalized;
|
|
7584
|
+
this._adapter$.next({
|
|
7585
|
+
...this._adapter$.getValue()
|
|
7586
|
+
});
|
|
7587
|
+
}
|
|
7253
7588
|
getAiCopilotAdapter() {
|
|
7254
7589
|
return this._aiCopilotAdapter;
|
|
7255
7590
|
}
|
|
@@ -7286,7 +7621,53 @@ var PdfIoService = class extends Disposable2 {
|
|
|
7286
7621
|
}
|
|
7287
7622
|
getIoCapabilities() {
|
|
7288
7623
|
const adapter = this._adapter$.getValue();
|
|
7289
|
-
const
|
|
7624
|
+
const defaultIoTarget = this.getDefaultIoTarget();
|
|
7625
|
+
const serverImportPdf = Boolean(adapter.importPdf);
|
|
7626
|
+
const serverExportPdf = Boolean(adapter.exportPdf);
|
|
7627
|
+
const hasServerIo = serverImportPdf || serverExportPdf;
|
|
7628
|
+
const recommendedDefaultTarget = defaultIoTarget === "browser" ? "browser" : defaultIoTarget === "server" ? "server" : hasServerIo ? "server" : "browser";
|
|
7629
|
+
const browserFmtCfg = adapter.browserFormats && typeof adapter.browserFormats === "object" ? adapter.browserFormats : void 0;
|
|
7630
|
+
const browserImportList = browserFmtCfg?.import ? normalizeFormatKeys(browserFmtCfg.import) : null;
|
|
7631
|
+
const browserExportList = browserFmtCfg?.export ? normalizeFormatKeys(browserFmtCfg.export) : null;
|
|
7632
|
+
const serverImportList = adapter.serverFormats?.import ? normalizeFormatKeys(adapter.serverFormats.import) : null;
|
|
7633
|
+
const serverExportList = adapter.serverFormats?.export ? normalizeFormatKeys(adapter.serverFormats.export) : null;
|
|
7634
|
+
const importFormatKeys = /* @__PURE__ */ new Set([
|
|
7635
|
+
"pdf",
|
|
7636
|
+
"json",
|
|
7637
|
+
...browserImportList ?? [],
|
|
7638
|
+
...serverImportList ?? []
|
|
7639
|
+
]);
|
|
7640
|
+
const exportFormatKeys = /* @__PURE__ */ new Set([
|
|
7641
|
+
"pdf",
|
|
7642
|
+
"json",
|
|
7643
|
+
...browserExportList ?? [],
|
|
7644
|
+
...serverExportList ?? []
|
|
7645
|
+
]);
|
|
7646
|
+
const resolveTargetForCap = (serverSupported) => {
|
|
7647
|
+
if (defaultIoTarget === "server") return "server";
|
|
7648
|
+
if (defaultIoTarget === "browser") return "browser";
|
|
7649
|
+
return serverSupported ? "server" : "browser";
|
|
7650
|
+
};
|
|
7651
|
+
const importFormats = {};
|
|
7652
|
+
for (const fmt of importFormatKeys) {
|
|
7653
|
+
const browserSupported = adapter.browserFormats === false ? false : browserImportList !== null ? browserImportList.includes(fmt) : fmt === "pdf" || fmt === "json";
|
|
7654
|
+
const serverSupported = !serverImportPdf ? false : serverImportList !== null ? serverImportList.includes(fmt) : fmt === "pdf";
|
|
7655
|
+
importFormats[fmt] = {
|
|
7656
|
+
browser: browserSupported,
|
|
7657
|
+
server: serverSupported,
|
|
7658
|
+
defaultTarget: resolveTargetForCap(serverSupported)
|
|
7659
|
+
};
|
|
7660
|
+
}
|
|
7661
|
+
const exportFormats = {};
|
|
7662
|
+
for (const fmt of exportFormatKeys) {
|
|
7663
|
+
const browserSupported = adapter.browserFormats === false ? false : browserExportList !== null ? browserExportList.includes(fmt) : fmt === "pdf" || fmt === "json";
|
|
7664
|
+
const serverSupported = fmt === "pdf" ? serverExportPdf && (serverExportList !== null ? serverExportList.includes("pdf") : true) : Boolean(adapter.exportPdf || adapter.onSave) && serverExportList !== null && serverExportList.includes(fmt);
|
|
7665
|
+
exportFormats[fmt] = {
|
|
7666
|
+
browser: browserSupported,
|
|
7667
|
+
server: serverSupported,
|
|
7668
|
+
defaultTarget: resolveTargetForCap(serverSupported)
|
|
7669
|
+
};
|
|
7670
|
+
}
|
|
7290
7671
|
return {
|
|
7291
7672
|
browser: {
|
|
7292
7673
|
importOverlayPdf: true,
|
|
@@ -7295,39 +7676,106 @@ var PdfIoService = class extends Disposable2 {
|
|
|
7295
7676
|
},
|
|
7296
7677
|
server: {
|
|
7297
7678
|
adapterName: adapter.name || "frontend-default",
|
|
7298
|
-
importPdf:
|
|
7299
|
-
exportPdf:
|
|
7679
|
+
importPdf: serverImportPdf,
|
|
7680
|
+
exportPdf: serverExportPdf,
|
|
7300
7681
|
onSave: Boolean(adapter.onSave),
|
|
7301
7682
|
silentPrint: Boolean(adapter.onSilentPrint)
|
|
7302
7683
|
},
|
|
7303
|
-
|
|
7684
|
+
defaultIoTarget,
|
|
7685
|
+
recommendedDefaultTarget,
|
|
7686
|
+
formats: {
|
|
7687
|
+
import: importFormats,
|
|
7688
|
+
export: exportFormats
|
|
7689
|
+
}
|
|
7690
|
+
};
|
|
7691
|
+
}
|
|
7692
|
+
_resolveBaseSnapshotForImport(context) {
|
|
7693
|
+
if (context?.snapshot) {
|
|
7694
|
+
const cloned = Tools5.deepClone(context.snapshot);
|
|
7695
|
+
return {
|
|
7696
|
+
unitId: context.unitId || cloned.id,
|
|
7697
|
+
baseSnapshot: cloned
|
|
7698
|
+
};
|
|
7699
|
+
}
|
|
7700
|
+
if (this._univerInstanceService) {
|
|
7701
|
+
const unit = context?.unitId ? this._univerInstanceService.getUnit(
|
|
7702
|
+
context.unitId,
|
|
7703
|
+
UniverInstanceType4.UNIVER_PDF
|
|
7704
|
+
) : this._univerInstanceService.getCurrentUnitOfType(
|
|
7705
|
+
UniverInstanceType4.UNIVER_PDF
|
|
7706
|
+
);
|
|
7707
|
+
if (unit) {
|
|
7708
|
+
return {
|
|
7709
|
+
unitId: unit.getUnitId(),
|
|
7710
|
+
baseSnapshot: unit.save()
|
|
7711
|
+
};
|
|
7712
|
+
}
|
|
7713
|
+
}
|
|
7714
|
+
const fallback = createDefaultPdfUnitData(
|
|
7715
|
+
context?.unitId ? { id: context.unitId } : void 0
|
|
7716
|
+
);
|
|
7717
|
+
return {
|
|
7718
|
+
unitId: fallback.id,
|
|
7719
|
+
baseSnapshot: fallback
|
|
7304
7720
|
};
|
|
7305
7721
|
}
|
|
7306
7722
|
async importPdf(file, context) {
|
|
7307
|
-
const
|
|
7723
|
+
const report = await this.importPdfWithReport(file, context);
|
|
7724
|
+
return report.outcome;
|
|
7725
|
+
}
|
|
7726
|
+
async importPdfWithReport(file, context) {
|
|
7727
|
+
const { unitId, baseSnapshot } = this._resolveBaseSnapshotForImport(context);
|
|
7308
7728
|
const adapter = this._adapter$.getValue();
|
|
7309
|
-
const
|
|
7310
|
-
const
|
|
7729
|
+
const caps = this.getIoCapabilities();
|
|
7730
|
+
const fmt = detectImportFileFormat(file);
|
|
7731
|
+
const fmtCap = caps.formats.import[fmt] ?? caps.formats.import.pdf;
|
|
7732
|
+
const requested = context?.target ?? this.getDefaultIoTarget();
|
|
7733
|
+
const effective = requested === "auto" ? fmtCap.defaultTarget : requested;
|
|
7734
|
+
const defaultImport = async (f) => {
|
|
7735
|
+
if (context?.defaultImport) {
|
|
7736
|
+
const customRaw = await context.defaultImport(f);
|
|
7737
|
+
return normalizeAdapterImportResult(customRaw).outcome;
|
|
7738
|
+
}
|
|
7739
|
+
return fallbackReadFileAsOutcome(f, baseSnapshot, context?.password);
|
|
7740
|
+
};
|
|
7741
|
+
let rawResult;
|
|
7311
7742
|
if (effective === "server") {
|
|
7312
|
-
if (!adapter.importPdf) {
|
|
7743
|
+
if (!adapter.importPdf || !fmtCap.server) {
|
|
7313
7744
|
throw new PdfIoTargetError(
|
|
7314
7745
|
"\u5F53\u524D\u672A\u914D\u7F6E\u670D\u52A1\u7AEF PDF \u5BFC\u5165\u63A5\u53E3\uFF08importPdf\uFF09\uFF0C\u672A\u81EA\u52A8\u964D\u7EA7\u81F3\u6D4F\u89C8\u5668\u7AEF\uFF1B\u8BF7\u5148\u63A5\u5165\u670D\u52A1\u7AEF\u9002\u914D\u5668\u6216\u6539\u9009\u300C\u6D4F\u89C8\u5668\u5904\u7406\u300D"
|
|
7315
7746
|
);
|
|
7316
7747
|
}
|
|
7317
|
-
|
|
7318
|
-
|
|
7319
|
-
|
|
7320
|
-
|
|
7321
|
-
|
|
7322
|
-
|
|
7323
|
-
|
|
7324
|
-
|
|
7325
|
-
|
|
7326
|
-
|
|
7327
|
-
|
|
7748
|
+
try {
|
|
7749
|
+
rawResult = await adapter.importPdf(file, {
|
|
7750
|
+
unitId,
|
|
7751
|
+
snapshot: Tools5.deepClone(baseSnapshot),
|
|
7752
|
+
defaultImport
|
|
7753
|
+
});
|
|
7754
|
+
} catch (err) {
|
|
7755
|
+
attachSuggestedFallbackAndThrow(err, "browser");
|
|
7756
|
+
}
|
|
7757
|
+
} else {
|
|
7758
|
+
if (!context?.defaultImport && !fmtCap.browser) {
|
|
7759
|
+
attachSuggestedFallbackAndThrow(
|
|
7760
|
+
new Error(`\u5F53\u524D\u6D4F\u89C8\u5668\u7AEF\u672A\u542F\u7528 .${fmt} \u683C\u5F0F\u5BFC\u5165\uFF1B\u8BF7\u5207\u6362\u4E3A\u300C\u670D\u52A1\u7AEF\u5904\u7406\u300D\u91CD\u8BD5`),
|
|
7761
|
+
"server"
|
|
7762
|
+
);
|
|
7763
|
+
}
|
|
7764
|
+
try {
|
|
7765
|
+
rawResult = context?.defaultImport ? await context.defaultImport(file) : await fallbackReadFileAsOutcome(file, baseSnapshot, context?.password);
|
|
7766
|
+
} catch (err) {
|
|
7767
|
+
attachSuggestedFallbackAndThrow(err, "server");
|
|
7328
7768
|
}
|
|
7329
|
-
throw err;
|
|
7330
7769
|
}
|
|
7770
|
+
const { outcome, warnings } = normalizeAdapterImportResult(rawResult);
|
|
7771
|
+
const candidateSnapshot = outcome.mode === "snapshot" ? Tools5.deepClone(outcome.snapshot) : buildOverlayImportCandidateSnapshot(baseSnapshot, outcome);
|
|
7772
|
+
return {
|
|
7773
|
+
snapshot: candidateSnapshot,
|
|
7774
|
+
outcome,
|
|
7775
|
+
warnings,
|
|
7776
|
+
executedBy: effective,
|
|
7777
|
+
fileName: file?.name || (outcome.mode === "overlay" ? outcome.sourceDocumentRef.fileName : candidateSnapshot.name) || void 0
|
|
7778
|
+
};
|
|
7331
7779
|
}
|
|
7332
7780
|
async exportPdf(params) {
|
|
7333
7781
|
const beforeEvent = {
|
|
@@ -7342,22 +7790,29 @@ var PdfIoService = class extends Disposable2 {
|
|
|
7342
7790
|
}
|
|
7343
7791
|
const defaultExport = () => exportPdfBytes(params.snapshot, params.data, params.options);
|
|
7344
7792
|
const adapter = this._adapter$.getValue();
|
|
7345
|
-
const
|
|
7346
|
-
const
|
|
7793
|
+
const caps = this.getIoCapabilities();
|
|
7794
|
+
const pdfExportCap = caps.formats.export.pdf;
|
|
7795
|
+
const requested = params.target ?? this.getDefaultIoTarget();
|
|
7796
|
+
const effective = requested === "auto" ? pdfExportCap.defaultTarget : requested;
|
|
7347
7797
|
if (effective === "server") {
|
|
7348
|
-
if (!adapter.exportPdf) {
|
|
7798
|
+
if (!adapter.exportPdf || !pdfExportCap.server) {
|
|
7349
7799
|
throw new PdfIoTargetError(
|
|
7350
7800
|
"\u5F53\u524D\u672A\u914D\u7F6E\u670D\u52A1\u7AEF PDF \u5BFC\u51FA\u63A5\u53E3\uFF08exportPdf\uFF09\uFF0C\u672A\u81EA\u52A8\u964D\u7EA7\u81F3\u6D4F\u89C8\u5668\u7AEF\uFF1B\u8BF7\u5148\u63A5\u5165\u670D\u52A1\u7AEF\u9002\u914D\u5668\u6216\u6539\u9009\u300C\u6D4F\u89C8\u5668\u5904\u7406\u300D"
|
|
7351
7801
|
);
|
|
7352
7802
|
}
|
|
7353
|
-
|
|
7354
|
-
|
|
7355
|
-
|
|
7356
|
-
|
|
7357
|
-
|
|
7358
|
-
|
|
7359
|
-
|
|
7360
|
-
|
|
7803
|
+
let bytes;
|
|
7804
|
+
try {
|
|
7805
|
+
const raw = await adapter.exportPdf({
|
|
7806
|
+
unitId: params.unitId,
|
|
7807
|
+
snapshot: params.snapshot,
|
|
7808
|
+
data: params.data,
|
|
7809
|
+
options: params.options,
|
|
7810
|
+
defaultExport
|
|
7811
|
+
});
|
|
7812
|
+
bytes = await normalizeBinaryToUint8Array(raw);
|
|
7813
|
+
} catch (err) {
|
|
7814
|
+
attachSuggestedFallbackAndThrow(err, "browser");
|
|
7815
|
+
}
|
|
7361
7816
|
this.notifyExported({
|
|
7362
7817
|
unitId: params.unitId,
|
|
7363
7818
|
bytes,
|
|
@@ -7366,6 +7821,12 @@ var PdfIoService = class extends Disposable2 {
|
|
|
7366
7821
|
});
|
|
7367
7822
|
return bytes;
|
|
7368
7823
|
}
|
|
7824
|
+
if (!pdfExportCap.browser) {
|
|
7825
|
+
attachSuggestedFallbackAndThrow(
|
|
7826
|
+
new Error("\u5F53\u524D\u6D4F\u89C8\u5668\u7AEF\u672A\u542F\u7528 PDF \u5BFC\u51FA\uFF1B\u8BF7\u5207\u6362\u4E3A\u300C\u670D\u52A1\u7AEF\u5904\u7406\u300D\u91CD\u8BD5"),
|
|
7827
|
+
"server"
|
|
7828
|
+
);
|
|
7829
|
+
}
|
|
7369
7830
|
try {
|
|
7370
7831
|
const bytes = await defaultExport();
|
|
7371
7832
|
this.notifyExported({
|
|
@@ -7376,10 +7837,7 @@ var PdfIoService = class extends Disposable2 {
|
|
|
7376
7837
|
});
|
|
7377
7838
|
return bytes;
|
|
7378
7839
|
} catch (err) {
|
|
7379
|
-
|
|
7380
|
-
err.suggestedFallback = "server";
|
|
7381
|
-
}
|
|
7382
|
-
throw err;
|
|
7840
|
+
attachSuggestedFallbackAndThrow(err, "server");
|
|
7383
7841
|
}
|
|
7384
7842
|
}
|
|
7385
7843
|
async silentPrint(params) {
|
|
@@ -7432,6 +7890,9 @@ var PdfIoService = class extends Disposable2 {
|
|
|
7432
7890
|
return true;
|
|
7433
7891
|
}
|
|
7434
7892
|
};
|
|
7893
|
+
PdfIoService = __decorateClass([
|
|
7894
|
+
__decorateParam(0, Optional(IUniverInstanceService4))
|
|
7895
|
+
], PdfIoService);
|
|
7435
7896
|
|
|
7436
7897
|
// src/plugin.ts
|
|
7437
7898
|
var UNIVER_PDFS_PLUGIN_NAME = "UNIVER_PDFS_PLUGIN";
|
|
@@ -7451,9 +7912,9 @@ var UniverPdfsPlugin = class extends Plugin {
|
|
|
7451
7912
|
}
|
|
7452
7913
|
onStarting() {
|
|
7453
7914
|
this.disposeWithMe(
|
|
7454
|
-
this._univerInstanceService.registerCtorForType(
|
|
7915
|
+
this._univerInstanceService.registerCtorForType(UniverInstanceType5.UNIVER_PDF, PdfUnitModel)
|
|
7455
7916
|
);
|
|
7456
|
-
const focusSub = this._univerInstanceService.getCurrentTypeOfUnit$(
|
|
7917
|
+
const focusSub = this._univerInstanceService.getCurrentTypeOfUnit$(UniverInstanceType5.UNIVER_PDF).subscribe((unit) => {
|
|
7457
7918
|
if (unit && this._univerInstanceService.getFocusedUnit()?.getUnitId() !== unit.getUnitId()) {
|
|
7458
7919
|
this._univerInstanceService.focusUnit(unit.getUnitId());
|
|
7459
7920
|
}
|
|
@@ -7470,6 +7931,9 @@ var UniverPdfsPlugin = class extends Plugin {
|
|
|
7470
7931
|
if (this._config.io) {
|
|
7471
7932
|
this._injector.get(IPdfIoService).setAdapter(this._config.io);
|
|
7472
7933
|
}
|
|
7934
|
+
if (this._config.defaultIoTarget) {
|
|
7935
|
+
this._injector.get(IPdfIoService).setDefaultIoTarget(this._config.defaultIoTarget);
|
|
7936
|
+
}
|
|
7473
7937
|
if (this._config.aiCopilot) {
|
|
7474
7938
|
this._injector.get(IPdfIoService).setAiCopilotAdapter(this._config.aiCopilot);
|
|
7475
7939
|
}
|
|
@@ -7527,7 +7991,7 @@ var UniverPdfsPlugin = class extends Plugin {
|
|
|
7527
7991
|
};
|
|
7528
7992
|
UniverPdfsPlugin = __decorateClass([
|
|
7529
7993
|
__decorateParam(1, Inject(Injector)),
|
|
7530
|
-
__decorateParam(2,
|
|
7994
|
+
__decorateParam(2, IUniverInstanceService5),
|
|
7531
7995
|
__decorateParam(3, ICommandService2)
|
|
7532
7996
|
], UniverPdfsPlugin);
|
|
7533
7997
|
export {
|
|
@@ -7626,6 +8090,7 @@ export {
|
|
|
7626
8090
|
applyOcrToUnitPage,
|
|
7627
8091
|
autoDetectFormFieldsOnPage,
|
|
7628
8092
|
buildMinimalTestTrueTypeFont,
|
|
8093
|
+
buildOverlayImportCandidateSnapshot,
|
|
7629
8094
|
buildPdfTemplateFromOcrFields,
|
|
7630
8095
|
canonicalJsonStringify,
|
|
7631
8096
|
cmykToRgb,
|
|
@@ -7735,6 +8200,7 @@ export {
|
|
|
7735
8200
|
normalizeBarcodeFormatType,
|
|
7736
8201
|
normalizeBarcodePayloadForFormat,
|
|
7737
8202
|
normalizeImageCrop,
|
|
8203
|
+
normalizePdfImportWarnings,
|
|
7738
8204
|
optimizePdfUnit,
|
|
7739
8205
|
organizePdfBytes,
|
|
7740
8206
|
overlayDiagnosticGridOnPdfBytes,
|