document-cli 1.11.0 → 1.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,6 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  let node_fs_promises = require("node:fs/promises");
3
3
  let documents_js = require("documents.js");
4
+ let pdf_codec = require("pdf-codec");
4
5
  let node_path = require("node:path");
5
6
  let commander = require("commander");
6
7
  let odf_js = require("odf.js");
@@ -164,166 +165,17 @@ function mapErrorToExit(error, abortReason) {
164
165
  if (abortReason === "timeout") return 124;
165
166
  if (error instanceof documents_js.OdmUnresolvedSectionError || error instanceof documents_js.OdbTableNotSpecifiedError || error instanceof documents_js.OdbTableNotFoundError || error instanceof documents_js.OdbNoEmbeddedDataSourceError || error instanceof documents_js.OdbUnsupportedFormatError || error instanceof documents_js.OdbReportNotSpecifiedError) return 3;
166
167
  if (error instanceof documents_js.HsqldbSqlUnsupportedError || error instanceof documents_js.HsqldbSqlParseError || error instanceof documents_js.HsqldbSqlEvaluationError) return 1;
168
+ if (error instanceof documents_js.UnsupportedFontSourceFormatError) return 2;
167
169
  if (error instanceof documents_js.PdfEncryptedError || error instanceof documents_js.PdfParseError) return 1;
168
170
  return 1;
169
171
  }
170
172
  //#endregion
171
- //#region src/runtime/font-face.ts
172
- var FontFaceError = class extends Error {
173
- constructor(message) {
174
- super(message);
175
- this.name = "FontFaceError";
176
- }
177
- };
178
- const SFNT_VERSION_TRUETYPE = 65536;
179
- const SFNT_VERSION_CFF = 1330926671;
180
- const SFNT_VERSION_APPLE_TRUE = 1953658213;
181
- const SFNT_VERSION_APPLE_TYP1 = 1954115633;
182
- const SFNT_VERSION_COLLECTION = 1953784678;
183
- const SFNT_VERSIONS = /* @__PURE__ */ new Set([
184
- SFNT_VERSION_TRUETYPE,
185
- SFNT_VERSION_CFF,
186
- SFNT_VERSION_APPLE_TRUE,
187
- SFNT_VERSION_APPLE_TYP1
188
- ]);
189
- const TABLE_DIRECTORY_HEADER_SIZE = 12;
190
- const TABLE_RECORD_SIZE = 16;
191
- const TABLE_TAG_SIZE = 4;
192
- const NAME_HEADER_SIZE = 6;
193
- const NAME_RECORD_SIZE = 12;
194
- const NAME_ID_FAMILY = 1;
195
- const NAME_ID_TYPOGRAPHIC_FAMILY = 16;
196
- const PLATFORM_UNICODE = 0;
197
- const PLATFORM_MACINTOSH = 1;
198
- const PLATFORM_WINDOWS = 3;
199
- const MACINTOSH_ENCODING_ROMAN = 0;
200
- const OS2_FS_SELECTION_OFFSET = 62;
201
- const OS2_MINIMUM_SIZE = 64;
202
- const OS2_FS_SELECTION_ITALIC = 1;
203
- const OS2_FS_SELECTION_BOLD = 32;
204
- const HEAD_MAC_STYLE_OFFSET = 44;
205
- const HEAD_MINIMUM_SIZE = 46;
206
- const HEAD_MAC_STYLE_BOLD = 1;
207
- const HEAD_MAC_STYLE_ITALIC = 2;
208
- function viewOf(bytes) {
209
- return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
210
- }
211
- function decodeTag(view, offset) {
212
- let tag = "";
213
- for (let i = 0; i < TABLE_TAG_SIZE; i++) tag += String.fromCharCode(view.getUint8(offset + i));
214
- return tag;
215
- }
216
- function decodeUtf16Be(view, offset, length) {
217
- let text = "";
218
- for (let i = 0; i + 1 < length; i += 2) text += String.fromCharCode(view.getUint16(offset + i));
219
- return text;
220
- }
221
- function decodeMacintoshAscii(view, offset, length) {
222
- let text = "";
223
- for (let i = 0; i < length; i++) {
224
- const byte = view.getUint8(offset + i);
225
- if (byte > 126) return;
226
- text += String.fromCharCode(byte);
227
- }
228
- return text;
229
- }
230
- function readTableDirectory(view, source) {
231
- if (view.byteLength < TABLE_DIRECTORY_HEADER_SIZE) throw new FontFaceError(`${source} is too short to be a font file (${String(view.byteLength)} bytes)`);
232
- const sfntVersion = view.getUint32(0);
233
- if (sfntVersion === SFNT_VERSION_COLLECTION) throw new FontFaceError(`${source} is a TrueType Collection (.ttc), which packs several faces into one file; extract the single face you want and pass that instead`);
234
- if (!SFNT_VERSIONS.has(sfntVersion)) throw new FontFaceError(`${source} is not a TrueType/OpenType font file (no recognised sfnt version); a .woff/.woff2 file must be converted to .ttf/.otf first`);
235
- const numTables = view.getUint16(4);
236
- if (view.byteLength < TABLE_DIRECTORY_HEADER_SIZE + numTables * TABLE_RECORD_SIZE) throw new FontFaceError(`${source} declares ${String(numTables)} tables but is too short to hold that many table records`);
237
- const tables = /* @__PURE__ */ new Map();
238
- for (let i = 0; i < numTables; i++) {
239
- const recordOffset = TABLE_DIRECTORY_HEADER_SIZE + i * TABLE_RECORD_SIZE;
240
- const tag = decodeTag(view, recordOffset);
241
- const offset = view.getUint32(recordOffset + 8);
242
- const length = view.getUint32(recordOffset + 12);
243
- if (offset + length > view.byteLength || tables.has(tag)) continue;
244
- tables.set(tag, {
245
- offset,
246
- length
247
- });
248
- }
249
- return tables;
250
- }
251
- function tableView(bytes, table) {
252
- return new DataView(bytes.buffer, bytes.byteOffset + table.offset, table.length);
253
- }
254
- function readNameRecords(name) {
255
- if (name.byteLength < NAME_HEADER_SIZE) return [];
256
- const count = name.getUint16(2);
257
- const storageOffset = name.getUint16(4);
258
- if (name.byteLength < NAME_HEADER_SIZE + count * NAME_RECORD_SIZE) return [];
259
- const records = [];
260
- for (let i = 0; i < count; i++) {
261
- const recordOffset = NAME_HEADER_SIZE + i * NAME_RECORD_SIZE;
262
- records.push({
263
- platformId: name.getUint16(recordOffset),
264
- encodingId: name.getUint16(recordOffset + 2),
265
- nameId: name.getUint16(recordOffset + 6),
266
- length: name.getUint16(recordOffset + 8),
267
- stringOffset: storageOffset + name.getUint16(recordOffset + 10)
268
- });
269
- }
270
- return records;
271
- }
272
- function platformPreference(record) {
273
- if (record.platformId === PLATFORM_WINDOWS) return 0;
274
- if (record.platformId === PLATFORM_UNICODE) return 1;
275
- if (record.platformId === PLATFORM_MACINTOSH && record.encodingId === MACINTOSH_ENCODING_ROMAN) return 2;
276
- return Number.POSITIVE_INFINITY;
277
- }
278
- function readNameString(name, records, nameId) {
279
- const candidates = records.filter((record) => record.nameId === nameId && Number.isFinite(platformPreference(record))).sort((left, right) => platformPreference(left) - platformPreference(right));
280
- for (const record of candidates) {
281
- if (record.stringOffset + record.length > name.byteLength) continue;
282
- const text = record.platformId === PLATFORM_MACINTOSH ? decodeMacintoshAscii(name, record.stringOffset, record.length) : decodeUtf16Be(name, record.stringOffset, record.length);
283
- if (text !== void 0 && text.length > 0) return text;
284
- }
285
- }
286
- function readStyleBits(bytes, tables, source) {
287
- const os2 = tables.get("OS/2");
288
- if (os2 !== void 0 && os2.length >= OS2_MINIMUM_SIZE) {
289
- const fsSelection = tableView(bytes, os2).getUint16(OS2_FS_SELECTION_OFFSET);
290
- return {
291
- bold: (fsSelection & OS2_FS_SELECTION_BOLD) !== 0,
292
- italic: (fsSelection & OS2_FS_SELECTION_ITALIC) !== 0
293
- };
294
- }
295
- const head = tables.get("head");
296
- if (head !== void 0 && head.length >= HEAD_MINIMUM_SIZE) {
297
- const macStyle = tableView(bytes, head).getUint16(HEAD_MAC_STYLE_OFFSET);
298
- return {
299
- bold: (macStyle & HEAD_MAC_STYLE_BOLD) !== 0,
300
- italic: (macStyle & HEAD_MAC_STYLE_ITALIC) !== 0
301
- };
302
- }
303
- throw new FontFaceError(`${source} declares neither a readable 'OS/2' nor a readable 'head' table, so its weight and slope cannot be determined`);
304
- }
305
- function describeFontFace(bytes, source) {
306
- const tables = readTableDirectory(viewOf(bytes), source);
307
- const nameTable = tables.get("name");
308
- if (nameTable === void 0) throw new FontFaceError(`${source} declares no 'name' table, so the font family it provides cannot be determined`);
309
- const name = tableView(bytes, nameTable);
310
- const records = readNameRecords(name);
311
- const family = readNameString(name, records, NAME_ID_TYPOGRAPHIC_FAMILY) ?? readNameString(name, records, NAME_ID_FAMILY);
312
- if (family === void 0) throw new FontFaceError(`${source} declares no family name in its 'name' table, so the font family it provides cannot be determined`);
313
- const { bold, italic } = readStyleBits(bytes, tables, source);
314
- return {
315
- family,
316
- bold,
317
- italic
318
- };
319
- }
320
- //#endregion
321
173
  //#region src/runtime/fonts.ts
322
174
  async function loadProvidedFonts(paths, options) {
323
175
  const fonts = [];
324
176
  for (const path of paths) {
325
177
  const bytes = new Uint8Array(await (0, node_fs_promises.readFile)(path, { signal: options?.signal }));
326
- const face = describeFontFace(bytes, path);
178
+ const face = (0, pdf_codec.readFontFace)(bytes, path);
327
179
  fonts.push({
328
180
  family: face.family,
329
181
  bold: face.bold,
@@ -617,27 +469,6 @@ function registerDocxExtrasCommand(program) {
617
469
  }
618
470
  //#endregion
619
471
  //#region src/commands/fonts.ts
620
- const FONT_SOURCE_FORMATS = {
621
- docx: true,
622
- pptx: true,
623
- odt: true,
624
- odp: true,
625
- ods: true,
626
- odg: true
627
- };
628
- function isFontSourceFormat(format) {
629
- return format in FONT_SOURCE_FORMATS;
630
- }
631
- function resolveFontSourcePackage(format, bytes) {
632
- if (format === "docx" || format === "pptx") return {
633
- kind: format,
634
- package: (0, documents_js.decodePackage)(bytes)
635
- };
636
- return {
637
- kind: "odf",
638
- package: (0, odf_js.decodePackage)(bytes)
639
- };
640
- }
641
472
  async function runFonts(input, options) {
642
473
  const command = "fonts";
643
474
  const { signal, getAbortReason } = createRuntimeSignal({});
@@ -647,13 +478,8 @@ async function runFonts(input, options) {
647
478
  process.stderr.write(`[${command}] cannot infer a document format from '${input}'; expected one of docx, pptx, odt, odp, ods, odg\n`);
648
479
  return 2;
649
480
  }
650
- if (!isFontSourceFormat(format)) {
651
- process.stderr.write(`[${command}] '${format}' documents carry no source-embedded font faces this command can extract; expected one of docx, pptx, odt, odp, ods, odg\n`);
652
- return 2;
653
- }
654
481
  const inputBytes = await readInput(input, { signal });
655
- const source = resolveFontSourcePackage(format, new Uint8Array(inputBytes));
656
- const summaries = (0, documents_js.extractSourceFonts)(source).map((face) => ({
482
+ const summaries = (0, documents_js.extractSourceFontsForFormat)(format, new Uint8Array(inputBytes)).map((face) => ({
657
483
  family: face.family,
658
484
  bold: face.bold,
659
485
  italic: face.italic,
@@ -698,23 +524,6 @@ function registerFormatsCommand(program) {
698
524
  }
699
525
  //#endregion
700
526
  //#region src/commands/from-package.ts
701
- function buildBytesForTarget(pkg, target) {
702
- if (target === "pdf") {
703
- if (pkg.layout === void 0) throw new Error("this DocumentPackage has no layout -- only a package dumped from a <format>-to-pdf or pdf-to-<format> conversion carries one; a bridge conversion's own dump (e.g. odt-to-docx) never does, so 'pdf' is not a reachable target from it");
704
- return (0, documents_js.writePdf)(pkg.layout);
705
- }
706
- switch (target) {
707
- case "docx": return (0, documents_js.encodePackage)((0, documents_js.buildDocxPackage)(pkg.content));
708
- case "pptx": return (0, documents_js.encodePackage)((0, documents_js.buildPptxPackage)(pkg.content));
709
- case "odt": return (0, odf_js.encodePackage)((0, documents_js.buildOdtPackage)(pkg.content));
710
- case "odp": return (0, odf_js.encodePackage)((0, documents_js.buildOdpPackage)(pkg.content));
711
- case "ods": return (0, odf_js.encodePackage)((0, documents_js.buildOdsPackage)(pkg.content));
712
- case "odg": return (0, odf_js.encodePackage)((0, documents_js.buildOdgPackage)(pkg.content));
713
- case "markdown": return (0, documents_js.encodeMarkdownText)((0, documents_js.buildMarkdownText)(pkg.content));
714
- case "xlsx": throw new Error("'xlsx' cannot be built from a DocumentPackage directly -- documents.js does not re-export a ContentDocument-to-xlsx builder; convert to 'ods' here, then run 'ods-to-xlsx' on the result instead");
715
- case "odf": throw new Error("'odf' (a standalone formula document) cannot be built from a DocumentPackage -- there is no ContentDocument-to-odf builder");
716
- }
717
- }
718
527
  async function runFromPackage(input, output, options) {
719
528
  const command = "from-package";
720
529
  if (output !== void 0 && options.out !== void 0 && output !== options.out) {
@@ -743,7 +552,7 @@ async function runFromPackage(input, output, options) {
743
552
  process.stderr.write(`[${command}] '${input}' is a ${result.kind}, not a DocumentPackage -- only a file written by --dump-package can be read back by this command\n`);
744
553
  return 2;
745
554
  }
746
- const bytes = buildBytesForTarget(result.value, target.format);
555
+ const bytes = (0, documents_js.buildDocumentBytes)(result.value, target.format);
747
556
  await writeOutput(resolvedOutput, bytes);
748
557
  createDiagnosticReporter({
749
558
  json: options.json,
@@ -802,20 +611,6 @@ function formatMetadataLines(metadata) {
802
611
  }
803
612
  //#endregion
804
613
  //#region src/commands/metadata.ts
805
- function readMetadataForFormat(format, bytes, signal) {
806
- switch (format) {
807
- case "docx": return (0, documents_js.readDocxContent)((0, documents_js.decodePackage)(bytes)).metadata;
808
- case "pptx": return (0, documents_js.readPptxContent)((0, documents_js.decodePackage)(bytes)).metadata;
809
- case "odt": return (0, documents_js.readOdtContent)((0, odf_js.decodePackage)(bytes)).metadata;
810
- case "odp": return (0, documents_js.readOdpContent)((0, odf_js.decodePackage)(bytes)).metadata;
811
- case "ods": return (0, documents_js.readOdsContent)((0, odf_js.decodePackage)(bytes)).metadata;
812
- case "odg": return (0, documents_js.readOdgContent)((0, odf_js.decodePackage)(bytes)).metadata;
813
- case "odf": return (0, documents_js.readOdfFormulaContent)((0, odf_js.decodePackage)(bytes)).metadata;
814
- case "markdown": return (0, documents_js.readMarkdownContent)((0, documents_js.decodeMarkdownText)(bytes)).metadata;
815
- case "pdf": return (0, documents_js.readPdf)(bytes, { signal }).metadata;
816
- case "xlsx": return (0, documents_js.readPdf)((0, documents_js.xlsxToPdf)(bytes, { signal }), { signal }).metadata;
817
- }
818
- }
819
614
  async function runMetadata(input, options) {
820
615
  const command = "metadata";
821
616
  const source = inferFormatFromExtension(input);
@@ -826,7 +621,7 @@ async function runMetadata(input, options) {
826
621
  const { signal, getAbortReason } = createRuntimeSignal({});
827
622
  try {
828
623
  const inputBytes = await readInput(input, { signal });
829
- const metadata = readMetadataForFormat(source, new Uint8Array(inputBytes), signal);
624
+ const metadata = (0, documents_js.readDocumentMetadata)(source, new Uint8Array(inputBytes), { signal });
830
625
  if (options.json) {
831
626
  process.stdout.write(`${JSON.stringify(metadata)}\n`);
832
627
  return 0;
@@ -1206,32 +1001,6 @@ const ODB_REPORT_TARGET_FORMATS = {
1206
1001
  function isOdbReportTargetFormat(format) {
1207
1002
  return format in ODB_REPORT_TARGET_FORMATS;
1208
1003
  }
1209
- function renderOdbReportBytes(content, target, options) {
1210
- if (target === "docx") return (0, documents_js.encodePackage)((0, documents_js.buildDocxPackage)(content));
1211
- if (target === "odt") return (0, odf_js.encodePackage)((0, documents_js.buildOdtPackage)(content));
1212
- if (content.kind !== "wordprocessing") throw new Error("readOdbReportContent returned a non-wordprocessing ContentDocument");
1213
- const fonts = (0, documents_js.createFontRegistry)({
1214
- fonts: options.fonts,
1215
- onSubstitution: (substitution) => {
1216
- if (options.reportFontSubstitution !== void 0) {
1217
- options.reportFontSubstitution(substitution);
1218
- return;
1219
- }
1220
- options.onDiagnosticCounted();
1221
- options.reporter.report(fontSubstitutionToDiagnostic(substitution));
1222
- }
1223
- });
1224
- const { document: layout, formulas } = (0, documents_js.convertWordprocessingToLayout)(content, { measurer: (0, documents_js.createFontMeasurer)(fonts) });
1225
- return (0, documents_js.writePdf)(layout, {
1226
- signal: options.signal,
1227
- onSubstitution: (substitution, context) => {
1228
- options.onDiagnosticCounted();
1229
- options.reporter.report(substitutionToDiagnostic(substitution, context.pageIndex));
1230
- },
1231
- formulas,
1232
- fonts
1233
- });
1234
- }
1235
1004
  async function runOdbRenderReport(input, output, options) {
1236
1005
  const command = "odb-render-report";
1237
1006
  if (output !== void 0 && options.out !== void 0 && output !== options.out) {
@@ -1265,13 +1034,21 @@ async function runOdbRenderReport(input, output, options) {
1265
1034
  const inputBytes = await readInput(input, { signal });
1266
1035
  const fonts = await loadProvidedFonts(options.fontFile ?? [], { signal });
1267
1036
  const pkg = (0, odf_js.decodePackage)(new Uint8Array(inputBytes));
1268
- const bytes = renderOdbReportBytes((0, documents_js.readOdbReportContent)(pkg, { report: options.report }), targetFormat, {
1269
- fonts,
1037
+ const content = (0, documents_js.readOdbReportContent)(pkg, { report: options.report });
1038
+ const bytes = targetFormat === "docx" ? (0, documents_js.odbReportToDocx)(content) : targetFormat === "odt" ? (0, documents_js.odbReportToOdt)(content) : (0, documents_js.odbReportToPdf)(content, {
1270
1039
  signal,
1271
- reporter,
1272
- reportFontSubstitution,
1273
- onDiagnosticCounted: () => {
1040
+ fonts,
1041
+ onFontSubstitution: (substitution) => {
1042
+ if (reportFontSubstitution !== void 0) {
1043
+ reportFontSubstitution(substitution);
1044
+ return;
1045
+ }
1046
+ diagnosticCount += 1;
1047
+ reporter.report(fontSubstitutionToDiagnostic(substitution));
1048
+ },
1049
+ onSubstitution: (substitution, context) => {
1274
1050
  diagnosticCount += 1;
1051
+ reporter.report(substitutionToDiagnostic(substitution, context.pageIndex));
1275
1052
  }
1276
1053
  });
1277
1054
  await writeOutput(resolvedOutput, bytes);
@@ -1514,63 +1291,9 @@ function registerPdfInspectCommand(program) {
1514
1291
  }
1515
1292
  //#endregion
1516
1293
  //#region src/commands/set-metadata.ts
1517
- const REBUILD_FORMATS = {
1518
- docx: true,
1519
- pptx: true,
1520
- odt: true,
1521
- odp: true,
1522
- ods: true,
1523
- odg: true,
1524
- markdown: true
1525
- };
1526
- function isRebuildFormat(format) {
1527
- return format in REBUILD_FORMATS;
1528
- }
1529
- function readContentForFormat(format, bytes) {
1530
- switch (format) {
1531
- case "docx": return (0, documents_js.readDocxContent)((0, documents_js.decodePackage)(bytes));
1532
- case "pptx": return (0, documents_js.readPptxContent)((0, documents_js.decodePackage)(bytes));
1533
- case "odt": return (0, documents_js.readOdtContent)((0, odf_js.decodePackage)(bytes));
1534
- case "odp": return (0, documents_js.readOdpContent)((0, odf_js.decodePackage)(bytes));
1535
- case "ods": return (0, documents_js.readOdsContent)((0, odf_js.decodePackage)(bytes));
1536
- case "odg": return (0, documents_js.readOdgContent)((0, odf_js.decodePackage)(bytes));
1537
- case "markdown": return (0, documents_js.readMarkdownContent)((0, documents_js.decodeMarkdownText)(bytes));
1538
- }
1539
- }
1540
- function buildBytesForRebuildFormat(format, content) {
1541
- switch (format) {
1542
- case "docx": return (0, documents_js.encodePackage)((0, documents_js.buildDocxPackage)(content));
1543
- case "pptx": return (0, documents_js.encodePackage)((0, documents_js.buildPptxPackage)(content));
1544
- case "odt": return (0, odf_js.encodePackage)((0, documents_js.buildOdtPackage)(content));
1545
- case "odp": return (0, odf_js.encodePackage)((0, documents_js.buildOdpPackage)(content));
1546
- case "ods": return (0, odf_js.encodePackage)((0, documents_js.buildOdsPackage)(content));
1547
- case "odg": return (0, odf_js.encodePackage)((0, documents_js.buildOdgPackage)(content));
1548
- case "markdown": return (0, documents_js.encodeMarkdownText)((0, documents_js.buildMarkdownText)(content));
1549
- }
1550
- }
1551
- function mergeMetadata(current, overrides) {
1552
- return {
1553
- ...current,
1554
- ...overrides.title !== void 0 ? { title: overrides.title } : {},
1555
- ...overrides.author !== void 0 ? { author: overrides.author } : {},
1556
- ...overrides.subject !== void 0 ? { subject: overrides.subject } : {},
1557
- ...overrides.keywords !== void 0 ? { keywords: overrides.keywords } : {}
1558
- };
1559
- }
1560
1294
  function parseKeywords(csv) {
1561
1295
  return csv.split(",").map((entry) => entry.trim()).filter((entry) => entry.length > 0);
1562
1296
  }
1563
- function classifyWritePath(source, target) {
1564
- if (source === "pdf" && target === "pdf") return { kind: "pdf" };
1565
- if (target === "xlsx" || source === "xlsx") return { errorMessage: "'xlsx' is not a supported set-metadata source or target -- documents.js does not re-export a ContentDocument-to-xlsx builder or a readXlsxContent from its own public surface (see that package's own README, Architecture section); convert with 'xlsx-to-ods'/'ods-to-xlsx' first, then set metadata on the ods" };
1566
- if (target === "odf" || source === "odf") return { errorMessage: "'odf' (a standalone formula document) is not a supported set-metadata source or target -- it has no write path back out at all" };
1567
- if (!isRebuildFormat(source) || !isRebuildFormat(target)) return { errorMessage: `set-metadata only patches metadata in place; it does not convert format -- source ('${source}') and target ('${target}') must be the same format (or both 'pdf'). Run 'convert'/'from-package' first if you need a different target format.` };
1568
- if (source !== target) return { errorMessage: `set-metadata only patches metadata in place; it does not convert format -- source ('${source}') and target ('${target}') must be the same format. Run 'convert'/'from-package' first if you need a different target format.` };
1569
- return {
1570
- kind: "rebuild",
1571
- format: source
1572
- };
1573
- }
1574
1297
  async function runSetMetadata(input, output, options) {
1575
1298
  const command = "set-metadata";
1576
1299
  if (output !== void 0 && options.out !== void 0 && output !== options.out) {
@@ -1587,11 +1310,6 @@ async function runSetMetadata(input, output, options) {
1587
1310
  process.stderr.write(`[${command}] cannot infer a source format from '${input}'; rename the file with a recognised extension (${KNOWN_DOCUMENT_FORMATS})\n`);
1588
1311
  return 2;
1589
1312
  }
1590
- const writePath = classifyWritePath(source, target.format);
1591
- if ("errorMessage" in writePath) {
1592
- process.stderr.write(`[${command}] ${writePath.errorMessage}\n`);
1593
- return 2;
1594
- }
1595
1313
  const overrides = {
1596
1314
  title: options.setTitle,
1597
1315
  author: options.setAuthor,
@@ -1602,21 +1320,7 @@ async function runSetMetadata(input, output, options) {
1602
1320
  const { signal, getAbortReason } = createRuntimeSignal({ timeoutMs: options.timeout });
1603
1321
  try {
1604
1322
  const inputBytes = await readInput(input, { signal });
1605
- const bytes = writePath.kind === "pdf" ? (() => {
1606
- const layout = (0, documents_js.readPdf)(new Uint8Array(inputBytes), { signal });
1607
- const patched = {
1608
- ...layout,
1609
- metadata: mergeMetadata(layout.metadata, overrides)
1610
- };
1611
- return (0, documents_js.writePdf)(patched, { signal });
1612
- })() : (() => {
1613
- const content = readContentForFormat(writePath.format, new Uint8Array(inputBytes));
1614
- const nextContent = {
1615
- ...content,
1616
- metadata: mergeMetadata(content.metadata, overrides)
1617
- };
1618
- return buildBytesForRebuildFormat(writePath.format, nextContent);
1619
- })();
1323
+ const bytes = (0, documents_js.setDocumentMetadata)(source, target.format, new Uint8Array(inputBytes), overrides, { signal });
1620
1324
  await writeOutput(resolvedOutput, bytes);
1621
1325
  createDiagnosticReporter({
1622
1326
  json: options.json,
@@ -1661,7 +1365,7 @@ function registerSetMetadataCommand(program) {
1661
1365
  }
1662
1366
  //#endregion
1663
1367
  //#region package.json
1664
- var version = "1.11.0";
1368
+ var version = "1.11.1";
1665
1369
  //#endregion
1666
1370
  //#region src/program.ts
1667
1371
  function createProgram() {