@pdfme/cli 6.1.0 → 6.1.1-dev.11
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/diagnostics.d.ts +2 -2
- package/dist/index.js +52 -28
- package/dist/index.js.map +1 -1
- package/dist/utils.d.ts +2 -2
- package/package.json +1 -1
package/dist/diagnostics.d.ts
CHANGED
|
@@ -25,10 +25,10 @@ export interface FieldInputHint {
|
|
|
25
25
|
pages: number[];
|
|
26
26
|
required: boolean;
|
|
27
27
|
expectedInput: {
|
|
28
|
-
kind: 'string' | 'jsonStringObject' | 'enumString' | 'stringMatrix';
|
|
28
|
+
kind: 'string' | 'jsonStringObject' | 'enumString' | 'stringMatrix' | 'stringArray';
|
|
29
29
|
variableNames?: string[];
|
|
30
30
|
allowedValues?: string[];
|
|
31
|
-
example?: string | string[][];
|
|
31
|
+
example?: string | string[] | string[][];
|
|
32
32
|
format?: string;
|
|
33
33
|
canonicalFormat?: string;
|
|
34
34
|
contentKind?: string;
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { defineCommand, runMain } from "citty";
|
|
|
3
3
|
import { PDFDocument } from "@pdfme/pdf-lib";
|
|
4
4
|
import { generate } from "@pdfme/generator";
|
|
5
5
|
import { pdf2img, pdf2size } from "@pdfme/converter";
|
|
6
|
-
import { DEFAULT_FONT_NAME, checkGenerateProps, checkTemplate, getDefaultFont, isUrlSafeToFetch } from "@pdfme/common";
|
|
6
|
+
import { DEFAULT_FONT_NAME, PAGE_SIZE_PRESETS, checkGenerateProps, checkTemplate, detectPaperSize, getDefaultFont, isUrlSafeToFetch } from "@pdfme/common";
|
|
7
7
|
import { basename, dirname, extname, join, resolve } from "node:path";
|
|
8
8
|
import * as schemas from "@pdfme/schemas";
|
|
9
9
|
import { accessSync, constants, existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
@@ -313,22 +313,6 @@ function readPdfFile(filePath) {
|
|
|
313
313
|
});
|
|
314
314
|
}
|
|
315
315
|
}
|
|
316
|
-
var PAPER_SIZES = {
|
|
317
|
-
A3: [297, 420],
|
|
318
|
-
A4: [210, 297],
|
|
319
|
-
A5: [148, 210],
|
|
320
|
-
A6: [105, 148],
|
|
321
|
-
B4: [250, 353],
|
|
322
|
-
B5: [176, 250],
|
|
323
|
-
Letter: [216, 279],
|
|
324
|
-
Legal: [216, 356],
|
|
325
|
-
Tabloid: [279, 432]
|
|
326
|
-
};
|
|
327
|
-
function detectPaperSize(width, height) {
|
|
328
|
-
const tolerance = 2;
|
|
329
|
-
for (const [name, [w, h]] of Object.entries(PAPER_SIZES)) if (Math.abs(width - w) <= tolerance && Math.abs(height - h) <= tolerance || Math.abs(width - h) <= tolerance && Math.abs(height - w) <= tolerance) return `${name} ${width < height ? "portrait" : "landscape"}`;
|
|
330
|
-
return null;
|
|
331
|
-
}
|
|
332
316
|
function parsePageRange(rangeStr, totalPages) {
|
|
333
317
|
const pages = /* @__PURE__ */ new Set();
|
|
334
318
|
for (const part of rangeStr.split(",")) {
|
|
@@ -502,8 +486,8 @@ function validateTemplate(template) {
|
|
|
502
486
|
fields: 0
|
|
503
487
|
};
|
|
504
488
|
const totalFields = schemaPages.reduce((sum, page) => sum + page.length, 0);
|
|
505
|
-
let pageWidth =
|
|
506
|
-
let pageHeight =
|
|
489
|
+
let pageWidth = PAGE_SIZE_PRESETS.A4.width;
|
|
490
|
+
let pageHeight = PAGE_SIZE_PRESETS.A4.height;
|
|
507
491
|
if (template.basePdf && typeof template.basePdf === "object" && "width" in template.basePdf) {
|
|
508
492
|
pageWidth = template.basePdf.width;
|
|
509
493
|
pageHeight = template.basePdf.height;
|
|
@@ -767,6 +751,17 @@ function buildFieldInputHint(schema, page, radioGroupMembers) {
|
|
|
767
751
|
}
|
|
768
752
|
};
|
|
769
753
|
}
|
|
754
|
+
if (type === "list") return {
|
|
755
|
+
name: schema.name,
|
|
756
|
+
type,
|
|
757
|
+
pages: [page],
|
|
758
|
+
required: schema.required === true,
|
|
759
|
+
expectedInput: {
|
|
760
|
+
kind: "stringArray",
|
|
761
|
+
example: ["First item", "Second item"],
|
|
762
|
+
acceptsJsonString: true
|
|
763
|
+
}
|
|
764
|
+
};
|
|
770
765
|
if (type === "date" || type === "time" || type === "dateTime") {
|
|
771
766
|
const canonicalFormat = getCanonicalDateStoredFormat(type);
|
|
772
767
|
return {
|
|
@@ -853,6 +848,7 @@ function getInputContractIssue(hint, input, inputIndex) {
|
|
|
853
848
|
if (hint.expectedInput.kind === "jsonStringObject") return getMultiVariableTextInputIssue(hint, input, inputIndex);
|
|
854
849
|
if (hint.expectedInput.kind === "enumString") return getEnumStringInputIssue(hint, input, inputIndex);
|
|
855
850
|
if (hint.expectedInput.kind === "stringMatrix") return getStringMatrixInputIssue(hint, input, inputIndex);
|
|
851
|
+
if (hint.expectedInput.kind === "stringArray") return getStringArrayInputIssue(hint, input, inputIndex);
|
|
856
852
|
if (isCanonicalDateHint(hint)) return getCanonicalDateInputIssue(hint, input, inputIndex);
|
|
857
853
|
return null;
|
|
858
854
|
}
|
|
@@ -957,6 +953,19 @@ function getStringMatrixInputIssue(hint, input, inputIndex) {
|
|
|
957
953
|
example
|
|
958
954
|
});
|
|
959
955
|
}
|
|
956
|
+
function getStringArrayInputIssue(hint, input, inputIndex) {
|
|
957
|
+
const rawValue = input[hint.name];
|
|
958
|
+
const example = hint.expectedInput.example;
|
|
959
|
+
if (rawValue === void 0 || rawValue === "") return null;
|
|
960
|
+
const issue = getStringArrayShapeIssue(typeof rawValue === "string" && hint.expectedInput.acceptsJsonString === true ? parseListJsonInput(rawValue) ?? rawValue : rawValue);
|
|
961
|
+
if (!issue) return null;
|
|
962
|
+
return buildStringArrayErrorMessage({
|
|
963
|
+
hint,
|
|
964
|
+
inputIndex,
|
|
965
|
+
extra: issue,
|
|
966
|
+
example
|
|
967
|
+
});
|
|
968
|
+
}
|
|
960
969
|
function isCanonicalDateHint(hint) {
|
|
961
970
|
return (hint.type === "date" || hint.type === "time" || hint.type === "dateTime") && typeof hint.expectedInput.canonicalFormat === "string" && hint.expectedInput.canonicalFormat.length > 0;
|
|
962
971
|
}
|
|
@@ -992,6 +1001,12 @@ function getStringMatrixShapeIssue(value, expectedColumnCount) {
|
|
|
992
1001
|
}
|
|
993
1002
|
return null;
|
|
994
1003
|
}
|
|
1004
|
+
function getStringArrayShapeIssue(value) {
|
|
1005
|
+
if (typeof value === "string") return null;
|
|
1006
|
+
if (!Array.isArray(value)) return `Received ${describeValue(value)}.`;
|
|
1007
|
+
for (let index = 0; index < value.length; index++) if (typeof value[index] !== "string") return `Item ${index + 1} must be a string. Received ${describeValue(value[index])}.`;
|
|
1008
|
+
return null;
|
|
1009
|
+
}
|
|
995
1010
|
function getFirstArrayLength(rows) {
|
|
996
1011
|
for (const row of rows) if (Array.isArray(row)) return row.length;
|
|
997
1012
|
return 0;
|
|
@@ -1004,6 +1019,16 @@ function parseTableStringMatrix(rawValue) {
|
|
|
1004
1019
|
return null;
|
|
1005
1020
|
}
|
|
1006
1021
|
}
|
|
1022
|
+
function parseListJsonInput(rawValue) {
|
|
1023
|
+
if (typeof rawValue !== "string") return null;
|
|
1024
|
+
const trimmed = rawValue.trim();
|
|
1025
|
+
if (!trimmed.startsWith("[") && !trimmed.startsWith("{")) return null;
|
|
1026
|
+
try {
|
|
1027
|
+
return JSON.parse(trimmed);
|
|
1028
|
+
} catch {
|
|
1029
|
+
return null;
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1007
1032
|
function isValidCanonicalDateValue(value, type) {
|
|
1008
1033
|
switch (type) {
|
|
1009
1034
|
case "date": return isValidCanonicalDate(value);
|
|
@@ -1077,6 +1102,11 @@ function buildStringMatrixErrorMessage(args) {
|
|
|
1077
1102
|
const compatibilityLabel = args.hint.expectedInput.acceptsJsonString === true ? " JSON string input is also accepted for compatibility." : "";
|
|
1078
1103
|
return `Field "${args.hint.name}" (${args.hint.type}) in input ${args.inputIndex + 1} expects a JSON array of string arrays${columnLabel}.${headerLabel}${exampleLabel}${compatibilityLabel} ${args.extra}`.trim();
|
|
1079
1104
|
}
|
|
1105
|
+
function buildStringArrayErrorMessage(args) {
|
|
1106
|
+
const exampleLabel = args.example !== void 0 ? ` Example: ${JSON.stringify(args.example)}.` : "";
|
|
1107
|
+
const compatibilityLabel = args.hint.expectedInput.acceptsJsonString === true ? " A newline string or JSON string array is also accepted." : "";
|
|
1108
|
+
return `Field "${args.hint.name}" (${args.hint.type}) in input ${args.inputIndex + 1} expects an array of strings.${exampleLabel}${compatibilityLabel} ${args.extra}`.trim();
|
|
1109
|
+
}
|
|
1080
1110
|
function buildCanonicalDateErrorMessage(args) {
|
|
1081
1111
|
const displayFormat = args.hint.expectedInput.format;
|
|
1082
1112
|
const displayLabel = typeof displayFormat === "string" && displayFormat.length > 0 && displayFormat !== args.hint.expectedInput.canonicalFormat ? ` Display format hint: ${displayFormat}.` : "";
|
|
@@ -1891,10 +1921,7 @@ var generate_default = defineCommand({
|
|
|
1891
1921
|
for (let i = 0; i < images.length; i++) {
|
|
1892
1922
|
const templateSchemas = template.schemas ?? [];
|
|
1893
1923
|
const pageSchemas = templateSchemas[i % templateSchemas.length] ?? [];
|
|
1894
|
-
const size = renderedPageSizes[i] ?? renderedPageSizes[0] ??
|
|
1895
|
-
width: 210,
|
|
1896
|
-
height: 297
|
|
1897
|
-
};
|
|
1924
|
+
const size = renderedPageSizes[i] ?? renderedPageSizes[0] ?? PAGE_SIZE_PRESETS.A4;
|
|
1898
1925
|
const gridImage = await drawGridOnImage(images[i], pageSchemas, gridSize, size.width, size.height, imageFormat);
|
|
1899
1926
|
writeOutput(imagePaths[i], gridImage);
|
|
1900
1927
|
}
|
|
@@ -2155,10 +2182,7 @@ var pdf2img_default = defineCommand({
|
|
|
2155
2182
|
const results = [];
|
|
2156
2183
|
for (const pageIdx of pageIndices) {
|
|
2157
2184
|
let imageData = allImages[pageIdx];
|
|
2158
|
-
const size = sizes[pageIdx] ??
|
|
2159
|
-
width: 210,
|
|
2160
|
-
height: 297
|
|
2161
|
-
};
|
|
2185
|
+
const size = sizes[pageIdx] ?? PAGE_SIZE_PRESETS.A4;
|
|
2162
2186
|
if (args.grid) imageData = await drawGridOnPdfImage(imageData, gridSize, size.width, size.height, imageFormat);
|
|
2163
2187
|
const outputPath = join(outputDir, `${inputBase}-${pageIdx + 1}.${ext}`);
|
|
2164
2188
|
writeOutput(outputPath, imageData);
|
|
@@ -2246,7 +2270,7 @@ var pdf2size_default = defineCommand({
|
|
|
2246
2270
|
});
|
|
2247
2271
|
//#endregion
|
|
2248
2272
|
//#region src/version.ts
|
|
2249
|
-
var CLI_VERSION = "6.1.
|
|
2273
|
+
var CLI_VERSION = "6.1.1-dev.11";
|
|
2250
2274
|
//#endregion
|
|
2251
2275
|
//#region src/example-templates.ts
|
|
2252
2276
|
function getExamplesBaseUrl() {
|