@pdfme/cli 6.1.5 → 6.1.7-dev.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/README.md +1 -1
- package/dist/commands/doctor.d.ts +0 -5
- package/dist/commands/generate.d.ts +0 -5
- package/dist/commands/pdf2img.d.ts +0 -5
- package/dist/grid.d.ts +2 -3
- package/dist/index.js +24 -361
- package/dist/index.js.map +1 -1
- package/dist/utils.d.ts +1 -1
- package/package.json +2 -2
- package/dist/commands/examples.d.ts +0 -35
- package/dist/example-fonts.d.ts +0 -8
- package/dist/example-templates.d.ts +0 -42
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
## @pdfme/cli
|
|
2
2
|
|
|
3
|
-
This package provides the `pdfme` command-line interface for JSON-first pdfme workflows, including PDF generation, validation, diagnostics,
|
|
3
|
+
This package provides the `pdfme` command-line interface for JSON-first pdfme workflows, including PDF generation, validation, diagnostics, and PDF conversion utilities.
|
|
4
4
|
|
|
5
5
|
Node.js 20 or later is required.
|
|
6
6
|
|
package/dist/grid.d.ts
CHANGED
|
@@ -9,15 +9,14 @@ interface Schema {
|
|
|
9
9
|
height: number;
|
|
10
10
|
[key: string]: unknown;
|
|
11
11
|
}
|
|
12
|
-
type ImageFormat = 'png' | 'jpeg';
|
|
13
12
|
/**
|
|
14
13
|
* Draw grid lines and schema boundary overlays on a generated PDF page image.
|
|
15
14
|
* Used by `pdfme generate --grid`.
|
|
16
15
|
*/
|
|
17
|
-
export declare function drawGridOnImage(imageBuffer: ArrayBuffer, schemas: Schema[], gridSizeMm: number, pageWidthMm: number, pageHeightMm: number
|
|
16
|
+
export declare function drawGridOnImage(imageBuffer: ArrayBuffer, schemas: Schema[], gridSizeMm: number, pageWidthMm: number, pageHeightMm: number): Promise<ArrayBuffer>;
|
|
18
17
|
/**
|
|
19
18
|
* Draw grid lines with mm coordinate labels on a plain PDF image.
|
|
20
19
|
* Used by `pdfme pdf2img --grid`.
|
|
21
20
|
*/
|
|
22
|
-
export declare function drawGridOnPdfImage(imageBuffer: ArrayBuffer, gridSizeMm: number, pageWidthMm: number, pageHeightMm: number
|
|
21
|
+
export declare function drawGridOnPdfImage(imageBuffer: ArrayBuffer, gridSizeMm: number, pageWidthMm: number, pageHeightMm: number): Promise<ArrayBuffer>;
|
|
23
22
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -103,13 +103,6 @@ function isOptionProvided(rawArgs, name, alias) {
|
|
|
103
103
|
return allowedFlags.has(flag);
|
|
104
104
|
});
|
|
105
105
|
}
|
|
106
|
-
function parseEnumArg(optionName, value, allowedValues) {
|
|
107
|
-
if (typeof value !== "string" || !allowedValues.includes(value)) fail(`Invalid value for --${optionName}: expected one of ${allowedValues.join(", ")}, received ${formatValue(value)}.`, {
|
|
108
|
-
code: "EARG",
|
|
109
|
-
exitCode: 1
|
|
110
|
-
});
|
|
111
|
-
return value;
|
|
112
|
-
}
|
|
113
106
|
function parsePositiveNumberArg(optionName, value) {
|
|
114
107
|
const parsed = typeof value === "number" ? value : Number(value);
|
|
115
108
|
if (!Number.isFinite(parsed) || parsed <= 0) fail(`Invalid value for --${optionName}: expected a positive number, received ${formatValue(value)}.`, {
|
|
@@ -278,11 +271,10 @@ function resolveBasePdf(template, basePdfArg, templateDir) {
|
|
|
278
271
|
}
|
|
279
272
|
return template;
|
|
280
273
|
}
|
|
281
|
-
function getImageOutputPaths(pdfOutputPath, pageCount
|
|
274
|
+
function getImageOutputPaths(pdfOutputPath, pageCount) {
|
|
282
275
|
const dir = dirname(pdfOutputPath);
|
|
283
276
|
const base = basename(pdfOutputPath, extname(pdfOutputPath));
|
|
284
|
-
|
|
285
|
-
return Array.from({ length: pageCount }, (_, i) => join(dir, `${base}-${i + 1}.${ext}`));
|
|
277
|
+
return Array.from({ length: pageCount }, (_, i) => join(dir, `${base}-${i + 1}.png`));
|
|
286
278
|
}
|
|
287
279
|
function writeOutput(filePath, data) {
|
|
288
280
|
try {
|
|
@@ -478,7 +470,7 @@ function validateTemplate(template) {
|
|
|
478
470
|
} catch (error) {
|
|
479
471
|
errors.push(error instanceof Error ? error.message : String(error));
|
|
480
472
|
}
|
|
481
|
-
const schemaPages = normalizeSchemaPages$
|
|
473
|
+
const schemaPages = normalizeSchemaPages$1(template.schemas);
|
|
482
474
|
if (schemaPages.length === 0) return {
|
|
483
475
|
errors,
|
|
484
476
|
warnings,
|
|
@@ -531,7 +523,7 @@ function validateTemplate(template) {
|
|
|
531
523
|
};
|
|
532
524
|
}
|
|
533
525
|
function inspectTemplate(template, templateDir) {
|
|
534
|
-
const flattenedSchemas = normalizeSchemaPages$
|
|
526
|
+
const flattenedSchemas = normalizeSchemaPages$1(template.schemas).flat();
|
|
535
527
|
const collectedSchemaTypes = getUniqueStringValues(flattenedSchemas.map((schema) => schema.type));
|
|
536
528
|
const requiredFonts = getUniqueStringValues(flattenedSchemas.map((schema) => schema.fontName));
|
|
537
529
|
return {
|
|
@@ -543,7 +535,7 @@ function inspectTemplate(template, templateDir) {
|
|
|
543
535
|
}
|
|
544
536
|
function collectInputHints(template) {
|
|
545
537
|
const hintMap = /* @__PURE__ */ new Map();
|
|
546
|
-
const schemaPages = normalizeSchemaPages$
|
|
538
|
+
const schemaPages = normalizeSchemaPages$1(template.schemas);
|
|
547
539
|
const radioGroupMembers = collectRadioGroupMembers(schemaPages);
|
|
548
540
|
for (let pageIdx = 0; pageIdx < schemaPages.length; pageIdx++) for (const schema of schemaPages[pageIdx]) {
|
|
549
541
|
const name = typeof schema.name === "string" ? schema.name : "";
|
|
@@ -645,7 +637,7 @@ function assertRecordObject(value, label) {
|
|
|
645
637
|
});
|
|
646
638
|
return value;
|
|
647
639
|
}
|
|
648
|
-
function normalizeSchemaPages$
|
|
640
|
+
function normalizeSchemaPages$1(rawSchemas) {
|
|
649
641
|
if (!Array.isArray(rawSchemas)) return [];
|
|
650
642
|
return rawSchemas.map((page) => {
|
|
651
643
|
if (Array.isArray(page)) return page.filter((schema) => typeof schema === "object" && schema !== null);
|
|
@@ -1730,23 +1722,21 @@ function bufferToArrayBuffer(buffer) {
|
|
|
1730
1722
|
* Draw grid lines and schema boundary overlays on a generated PDF page image.
|
|
1731
1723
|
* Used by `pdfme generate --grid`.
|
|
1732
1724
|
*/
|
|
1733
|
-
async function drawGridOnImage(imageBuffer, schemas, gridSizeMm, pageWidthMm, pageHeightMm
|
|
1725
|
+
async function drawGridOnImage(imageBuffer, schemas, gridSizeMm, pageWidthMm, pageHeightMm) {
|
|
1734
1726
|
const { canvas, ctx, img } = await loadAndPrepareCanvas(imageBuffer);
|
|
1735
1727
|
const pxPerMm = img.width / pageWidthMm;
|
|
1736
1728
|
drawGridLines(ctx, pxPerMm, pageWidthMm, pageHeightMm, img.width, img.height, gridSizeMm, false);
|
|
1737
1729
|
drawSchemaOverlays(ctx, schemas, pxPerMm);
|
|
1738
|
-
|
|
1739
|
-
return bufferToArrayBuffer(canvas.toBuffer(mimeType));
|
|
1730
|
+
return bufferToArrayBuffer(canvas.toBuffer("image/png"));
|
|
1740
1731
|
}
|
|
1741
1732
|
/**
|
|
1742
1733
|
* Draw grid lines with mm coordinate labels on a plain PDF image.
|
|
1743
1734
|
* Used by `pdfme pdf2img --grid`.
|
|
1744
1735
|
*/
|
|
1745
|
-
async function drawGridOnPdfImage(imageBuffer, gridSizeMm, pageWidthMm, pageHeightMm
|
|
1736
|
+
async function drawGridOnPdfImage(imageBuffer, gridSizeMm, pageWidthMm, pageHeightMm) {
|
|
1746
1737
|
const { canvas, ctx, img } = await loadAndPrepareCanvas(imageBuffer);
|
|
1747
1738
|
drawGridLines(ctx, img.width / pageWidthMm, pageWidthMm, pageHeightMm, img.width, img.height, gridSizeMm, true);
|
|
1748
|
-
|
|
1749
|
-
return bufferToArrayBuffer(canvas.toBuffer(mimeType));
|
|
1739
|
+
return bufferToArrayBuffer(canvas.toBuffer("image/png"));
|
|
1750
1740
|
}
|
|
1751
1741
|
//#endregion
|
|
1752
1742
|
//#region src/commands/generate.ts
|
|
@@ -1782,11 +1772,6 @@ var generateArgs = {
|
|
|
1782
1772
|
description: "Also output PNG images per page",
|
|
1783
1773
|
default: false
|
|
1784
1774
|
},
|
|
1785
|
-
imageFormat: {
|
|
1786
|
-
type: "string",
|
|
1787
|
-
description: "Image format: png | jpeg",
|
|
1788
|
-
default: "png"
|
|
1789
|
-
},
|
|
1790
1775
|
scale: {
|
|
1791
1776
|
type: "string",
|
|
1792
1777
|
description: "Image render scale",
|
|
@@ -1836,7 +1821,6 @@ var generate_default = defineCommand({
|
|
|
1836
1821
|
async run({ args, rawArgs }) {
|
|
1837
1822
|
return runWithContract({ json: Boolean(args.json) }, async () => {
|
|
1838
1823
|
assertNoUnknownFlags(rawArgs, generateArgs);
|
|
1839
|
-
const imageFormat = parseEnumArg("imageFormat", args.imageFormat, ["png", "jpeg"]);
|
|
1840
1824
|
const scale = parsePositiveNumberArg("scale", args.scale);
|
|
1841
1825
|
const gridSize = parsePositiveNumberArg("gridSize", args.gridSize);
|
|
1842
1826
|
ensureSafeDefaultOutputPath({
|
|
@@ -1854,7 +1838,7 @@ var generate_default = defineCommand({
|
|
|
1854
1838
|
});
|
|
1855
1839
|
const template = resolveBasePdf(rawTemplate, args.basePdf, templateDir);
|
|
1856
1840
|
const mode = args.file ? "job" : "template+inputs";
|
|
1857
|
-
const templatePageCount = normalizeSchemaPages
|
|
1841
|
+
const templatePageCount = normalizeSchemaPages(template.schemas).length;
|
|
1858
1842
|
const jobOptions = normalizeJobOptions(rawJobOptions);
|
|
1859
1843
|
assertSupportedSchemaTypes(template);
|
|
1860
1844
|
const resolvedFont = await resolveFont({
|
|
@@ -1889,7 +1873,7 @@ var generate_default = defineCommand({
|
|
|
1889
1873
|
console.error(`Template pages: ${templatePageCount}`);
|
|
1890
1874
|
console.error(`Inputs: ${inputs.length} set(s)`);
|
|
1891
1875
|
console.error(`Output: ${args.output}`);
|
|
1892
|
-
console.error(`Images: ${args.image || args.grid ? `enabled (
|
|
1876
|
+
console.error(`Images: ${args.image || args.grid ? `enabled (png, scale=${scale}, grid=${args.grid ? `${gridSize}mm` : "disabled"})` : "disabled"}`);
|
|
1893
1877
|
console.error(`Fonts: ${Object.keys(font).join(", ")}`);
|
|
1894
1878
|
if (args.basePdf) console.error(`Base PDF override: ${args.basePdf}`);
|
|
1895
1879
|
}
|
|
@@ -1911,18 +1895,15 @@ var generate_default = defineCommand({
|
|
|
1911
1895
|
outputBytes: pdf.byteLength
|
|
1912
1896
|
};
|
|
1913
1897
|
if (args.image || args.grid) {
|
|
1914
|
-
const images = await pdf2img(pdf, {
|
|
1915
|
-
|
|
1916
|
-
imageType: imageFormat
|
|
1917
|
-
});
|
|
1918
|
-
const imagePaths = getImageOutputPaths(args.output, images.length, imageFormat);
|
|
1898
|
+
const images = await pdf2img(pdf, { scale });
|
|
1899
|
+
const imagePaths = getImageOutputPaths(args.output, images.length);
|
|
1919
1900
|
if (args.grid) {
|
|
1920
1901
|
const renderedPageSizes = await pdf2size(pdf);
|
|
1921
1902
|
for (let i = 0; i < images.length; i++) {
|
|
1922
1903
|
const templateSchemas = template.schemas ?? [];
|
|
1923
1904
|
const pageSchemas = templateSchemas[i % templateSchemas.length] ?? [];
|
|
1924
1905
|
const size = renderedPageSizes[i] ?? renderedPageSizes[0] ?? PAGE_SIZE_PRESETS.A4;
|
|
1925
|
-
const gridImage = await drawGridOnImage(images[i], pageSchemas, gridSize, size.width, size.height
|
|
1906
|
+
const gridImage = await drawGridOnImage(images[i], pageSchemas, gridSize, size.width, size.height);
|
|
1926
1907
|
writeOutput(imagePaths[i], gridImage);
|
|
1927
1908
|
}
|
|
1928
1909
|
} else for (let i = 0; i < images.length; i++) writeOutput(imagePaths[i], images[i]);
|
|
@@ -1969,7 +1950,7 @@ function hasExplicitFontEntries(jobFont) {
|
|
|
1969
1950
|
}
|
|
1970
1951
|
function assertSupportedSchemaTypes(template) {
|
|
1971
1952
|
const unsupported = [];
|
|
1972
|
-
for (const page of normalizeSchemaPages
|
|
1953
|
+
for (const page of normalizeSchemaPages(template.schemas)) for (const schema of page) {
|
|
1973
1954
|
const type = schema.type;
|
|
1974
1955
|
if (typeof type === "string" && !schemaTypes.has(type)) {
|
|
1975
1956
|
const name = typeof schema.name === "string" && schema.name.length > 0 ? schema.name : null;
|
|
@@ -1986,7 +1967,7 @@ function describeGenerateInput(file, template, inputs) {
|
|
|
1986
1967
|
if (template || inputs) return `${template ?? "(missing template)"} + ${inputs ?? "(missing inputs)"}`;
|
|
1987
1968
|
return "(unknown)";
|
|
1988
1969
|
}
|
|
1989
|
-
function normalizeSchemaPages
|
|
1970
|
+
function normalizeSchemaPages(rawSchemas) {
|
|
1990
1971
|
if (!Array.isArray(rawSchemas)) return [];
|
|
1991
1972
|
return rawSchemas.map((page) => {
|
|
1992
1973
|
if (Array.isArray(page)) return page.filter((schema) => typeof schema === "object" && schema !== null);
|
|
@@ -2114,11 +2095,6 @@ var pdf2imgArgs = {
|
|
|
2114
2095
|
description: "Render scale",
|
|
2115
2096
|
default: "1"
|
|
2116
2097
|
},
|
|
2117
|
-
imageFormat: {
|
|
2118
|
-
type: "string",
|
|
2119
|
-
description: "Image format: png | jpeg",
|
|
2120
|
-
default: "png"
|
|
2121
|
-
},
|
|
2122
2098
|
pages: {
|
|
2123
2099
|
type: "string",
|
|
2124
2100
|
description: "Page range (e.g., 1-3, 1,3,5)"
|
|
@@ -2150,16 +2126,11 @@ var pdf2img_default = defineCommand({
|
|
|
2150
2126
|
});
|
|
2151
2127
|
const scale = parsePositiveNumberArg("scale", args.scale);
|
|
2152
2128
|
const gridSize = parsePositiveNumberArg("gridSize", args.gridSize);
|
|
2153
|
-
const imageFormat = parseEnumArg("imageFormat", args.imageFormat, ["png", "jpeg"]);
|
|
2154
2129
|
const pdfData = readPdfFile(args.file);
|
|
2155
2130
|
const sizes = await pdf2size(pdfData);
|
|
2156
2131
|
const pageIndices = args.pages ? parsePageRange(args.pages, sizes.length).map((page) => page - 1) : Array.from({ length: sizes.length }, (_, index) => index);
|
|
2157
|
-
const allImages = await pdf2img(pdfData, {
|
|
2158
|
-
scale,
|
|
2159
|
-
imageType: imageFormat
|
|
2160
|
-
});
|
|
2132
|
+
const allImages = await pdf2img(pdfData, { scale });
|
|
2161
2133
|
const inputBase = basename(args.file, extname(args.file));
|
|
2162
|
-
const ext = imageFormat === "jpeg" ? "jpg" : "png";
|
|
2163
2134
|
let outputDir = ".";
|
|
2164
2135
|
if (args.output) {
|
|
2165
2136
|
outputDir = args.output;
|
|
@@ -2175,7 +2146,7 @@ var pdf2img_default = defineCommand({
|
|
|
2175
2146
|
console.error(`Pages: ${sizes.length}`);
|
|
2176
2147
|
console.error(`Selected pages: ${pageIndices.map((pageIdx) => pageIdx + 1).join(", ")}`);
|
|
2177
2148
|
console.error(`Output: ${outputDir}`);
|
|
2178
|
-
console.error(
|
|
2149
|
+
console.error("Image format: png");
|
|
2179
2150
|
console.error(`Scale: ${scale}`);
|
|
2180
2151
|
console.error(`Grid: ${args.grid ? `enabled (${gridSize}mm)` : "disabled"}`);
|
|
2181
2152
|
}
|
|
@@ -2183,8 +2154,8 @@ var pdf2img_default = defineCommand({
|
|
|
2183
2154
|
for (const pageIdx of pageIndices) {
|
|
2184
2155
|
let imageData = allImages[pageIdx];
|
|
2185
2156
|
const size = sizes[pageIdx] ?? PAGE_SIZE_PRESETS.A4;
|
|
2186
|
-
if (args.grid) imageData = await drawGridOnPdfImage(imageData, gridSize, size.width, size.height
|
|
2187
|
-
const outputPath = join(outputDir, `${inputBase}-${pageIdx + 1}
|
|
2157
|
+
if (args.grid) imageData = await drawGridOnPdfImage(imageData, gridSize, size.width, size.height);
|
|
2158
|
+
const outputPath = join(outputDir, `${inputBase}-${pageIdx + 1}.png`);
|
|
2188
2159
|
writeOutput(outputPath, imageData);
|
|
2189
2160
|
const paperSize = detectPaperSize(size.width, size.height);
|
|
2190
2161
|
results.push({
|
|
@@ -2270,307 +2241,7 @@ var pdf2size_default = defineCommand({
|
|
|
2270
2241
|
});
|
|
2271
2242
|
//#endregion
|
|
2272
2243
|
//#region src/version.ts
|
|
2273
|
-
var CLI_VERSION = "6.1.
|
|
2274
|
-
//#endregion
|
|
2275
|
-
//#region src/example-templates.ts
|
|
2276
|
-
function getExamplesBaseUrl() {
|
|
2277
|
-
return process.env.PDFME_EXAMPLES_BASE_URL ?? "https://playground.pdfme.com/template-assets";
|
|
2278
|
-
}
|
|
2279
|
-
async function fetchJson(url) {
|
|
2280
|
-
const response = await fetch(url, { signal: AbortSignal.timeout(15e3) });
|
|
2281
|
-
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
2282
|
-
return await response.json();
|
|
2283
|
-
}
|
|
2284
|
-
async function getExampleManifest() {
|
|
2285
|
-
let lastError;
|
|
2286
|
-
for (const url of getManifestUrls()) try {
|
|
2287
|
-
return {
|
|
2288
|
-
manifest: normalizeManifest(await fetchJson(url)),
|
|
2289
|
-
source: "remote",
|
|
2290
|
-
url
|
|
2291
|
-
};
|
|
2292
|
-
} catch (error) {
|
|
2293
|
-
lastError = error;
|
|
2294
|
-
}
|
|
2295
|
-
throw new Error(`Could not load examples manifest. ${formatError(lastError)}`);
|
|
2296
|
-
}
|
|
2297
|
-
async function fetchExampleTemplateWithSource(name, options = {}) {
|
|
2298
|
-
const entry = (options.manifest ?? (await getExampleManifest()).manifest).templates.find((template) => template.name === name);
|
|
2299
|
-
if (!entry) throw new Error(`Template "${name}" is not present in the examples manifest.`);
|
|
2300
|
-
const relativePath = entry.path;
|
|
2301
|
-
const templateUrl = `${getExamplesBaseUrl().replace(/\/$/, "")}/${relativePath}`;
|
|
2302
|
-
return {
|
|
2303
|
-
template: await fetchJson(templateUrl),
|
|
2304
|
-
source: "remote",
|
|
2305
|
-
url: templateUrl
|
|
2306
|
-
};
|
|
2307
|
-
}
|
|
2308
|
-
function getManifestUrls() {
|
|
2309
|
-
const baseUrl = getExamplesBaseUrl().replace(/\/$/, "");
|
|
2310
|
-
return [`${baseUrl}/manifest.json`, `${baseUrl}/index.json`];
|
|
2311
|
-
}
|
|
2312
|
-
function normalizeManifest(raw) {
|
|
2313
|
-
if (Array.isArray(raw)) return {
|
|
2314
|
-
schemaVersion: 1,
|
|
2315
|
-
cliVersion: CLI_VERSION,
|
|
2316
|
-
templates: normalizeEntries(raw)
|
|
2317
|
-
};
|
|
2318
|
-
if (typeof raw !== "object" || raw === null) throw new Error("Examples manifest must be a JSON object or array.");
|
|
2319
|
-
const record = raw;
|
|
2320
|
-
const rawTemplates = Array.isArray(record.templates) ? record.templates : Array.isArray(record.entries) ? record.entries : void 0;
|
|
2321
|
-
if (!rawTemplates) throw new Error("Examples manifest is missing templates.");
|
|
2322
|
-
return {
|
|
2323
|
-
schemaVersion: typeof record.schemaVersion === "number" && Number.isFinite(record.schemaVersion) ? record.schemaVersion : 1,
|
|
2324
|
-
cliVersion: typeof record.cliVersion === "string" ? record.cliVersion : CLI_VERSION,
|
|
2325
|
-
templates: normalizeEntries(rawTemplates)
|
|
2326
|
-
};
|
|
2327
|
-
}
|
|
2328
|
-
function normalizeEntries(rawTemplates) {
|
|
2329
|
-
return rawTemplates.filter((entry) => typeof entry === "object" && entry !== null).map((entry) => {
|
|
2330
|
-
const name = typeof entry.name === "string" ? entry.name : "";
|
|
2331
|
-
return {
|
|
2332
|
-
name,
|
|
2333
|
-
author: typeof entry.author === "string" && entry.author.length > 0 ? entry.author : "pdfme",
|
|
2334
|
-
path: typeof entry.path === "string" && entry.path.length > 0 ? entry.path : `${name}/template.json`,
|
|
2335
|
-
thumbnailPath: typeof entry.thumbnailPath === "string" && entry.thumbnailPath.length > 0 ? entry.thumbnailPath : `${name}/thumbnail.png`,
|
|
2336
|
-
...typeof entry.sourcePath === "string" && entry.sourcePath.length > 0 ? { sourcePath: entry.sourcePath } : {},
|
|
2337
|
-
...typeof entry.description === "string" ? { description: entry.description } : {},
|
|
2338
|
-
...typeof entry.order === "number" && Number.isFinite(entry.order) ? { order: entry.order } : {},
|
|
2339
|
-
pageCount: typeof entry.pageCount === "number" && Number.isFinite(entry.pageCount) ? entry.pageCount : 0,
|
|
2340
|
-
fieldCount: typeof entry.fieldCount === "number" && Number.isFinite(entry.fieldCount) ? entry.fieldCount : 0,
|
|
2341
|
-
schemaTypes: normalizeStringArray(entry.schemaTypes),
|
|
2342
|
-
fontNames: normalizeStringArray(entry.fontNames),
|
|
2343
|
-
hasCJK: typeof entry.hasCJK === "boolean" ? entry.hasCJK : false,
|
|
2344
|
-
basePdfKind: typeof entry.basePdfKind === "string" && entry.basePdfKind.length > 0 ? entry.basePdfKind : "unknown",
|
|
2345
|
-
...typeof entry.sourceKind === "string" ? { sourceKind: entry.sourceKind } : {},
|
|
2346
|
-
...Array.isArray(entry.tags) ? { tags: normalizeStringArray(entry.tags) } : {},
|
|
2347
|
-
...typeof entry.title === "string" ? { title: entry.title } : {}
|
|
2348
|
-
};
|
|
2349
|
-
}).filter((entry) => entry.name.length > 0);
|
|
2350
|
-
}
|
|
2351
|
-
function normalizeStringArray(value) {
|
|
2352
|
-
if (!Array.isArray(value)) return [];
|
|
2353
|
-
return value.filter((item) => typeof item === "string" && item.length > 0);
|
|
2354
|
-
}
|
|
2355
|
-
function formatError(error) {
|
|
2356
|
-
return error instanceof Error ? error.message : String(error);
|
|
2357
|
-
}
|
|
2358
|
-
//#endregion
|
|
2359
|
-
//#region src/example-fonts.ts
|
|
2360
|
-
var OFFICIAL_EXAMPLE_FONT_URLS = {
|
|
2361
|
-
NotoSansJP: "https://fonts.gstatic.com/s/notosansjp/v53/-F6jfjtqLzI2JPCgQBnw7HFyzSD-AsregP8VFBEj75vY0rw-oME.ttf",
|
|
2362
|
-
NotoSerifJP: "https://fonts.gstatic.com/s/notoserifjp/v30/xn71YHs72GKoTvER4Gn3b5eMRtWGkp6o7MjQ2bwxOubAILO5wBCU.ttf",
|
|
2363
|
-
"PinyonScript-Regular": "https://fonts.gstatic.com/s/pinyonscript/v22/6xKpdSJbL9-e9LuoeQiDRQR8aOLQO4bhiDY.ttf"
|
|
2364
|
-
};
|
|
2365
|
-
function collectTemplateFontNames(template) {
|
|
2366
|
-
const schemas = template.schemas;
|
|
2367
|
-
if (!Array.isArray(schemas)) return [];
|
|
2368
|
-
const fontNames = /* @__PURE__ */ new Set();
|
|
2369
|
-
for (const page of schemas) {
|
|
2370
|
-
const pageSchemas = Array.isArray(page) ? page : typeof page === "object" && page !== null ? Object.values(page) : [];
|
|
2371
|
-
for (const schema of pageSchemas) {
|
|
2372
|
-
if (typeof schema !== "object" || schema === null) continue;
|
|
2373
|
-
const fontName = schema.fontName;
|
|
2374
|
-
if (typeof fontName === "string" && fontName.length > 0) fontNames.add(fontName);
|
|
2375
|
-
}
|
|
2376
|
-
}
|
|
2377
|
-
return [...fontNames].sort();
|
|
2378
|
-
}
|
|
2379
|
-
function getOfficialExampleFonts(template) {
|
|
2380
|
-
const entries = collectTemplateFontNames(template).flatMap((fontName) => {
|
|
2381
|
-
const url = OFFICIAL_EXAMPLE_FONT_URLS[fontName];
|
|
2382
|
-
return url ? [[fontName, {
|
|
2383
|
-
data: url,
|
|
2384
|
-
fallback: false,
|
|
2385
|
-
subset: true
|
|
2386
|
-
}]] : [];
|
|
2387
|
-
});
|
|
2388
|
-
if (entries.length === 0) return;
|
|
2389
|
-
return Object.fromEntries(entries);
|
|
2390
|
-
}
|
|
2391
|
-
//#endregion
|
|
2392
|
-
//#region src/commands/examples.ts
|
|
2393
|
-
var examplesArgs = {
|
|
2394
|
-
name: {
|
|
2395
|
-
type: "positional",
|
|
2396
|
-
description: "Template name to output",
|
|
2397
|
-
required: false
|
|
2398
|
-
},
|
|
2399
|
-
list: {
|
|
2400
|
-
type: "boolean",
|
|
2401
|
-
description: "List available templates",
|
|
2402
|
-
default: false
|
|
2403
|
-
},
|
|
2404
|
-
output: {
|
|
2405
|
-
type: "string",
|
|
2406
|
-
alias: "o",
|
|
2407
|
-
description: "Output file path"
|
|
2408
|
-
},
|
|
2409
|
-
withInputs: {
|
|
2410
|
-
type: "boolean",
|
|
2411
|
-
alias: "w",
|
|
2412
|
-
description: "Output unified format with sample inputs",
|
|
2413
|
-
default: false
|
|
2414
|
-
},
|
|
2415
|
-
verbose: {
|
|
2416
|
-
type: "boolean",
|
|
2417
|
-
alias: "v",
|
|
2418
|
-
description: "Verbose output",
|
|
2419
|
-
default: false
|
|
2420
|
-
},
|
|
2421
|
-
json: {
|
|
2422
|
-
type: "boolean",
|
|
2423
|
-
description: "Machine-readable JSON output",
|
|
2424
|
-
default: false
|
|
2425
|
-
}
|
|
2426
|
-
};
|
|
2427
|
-
function generateSampleInputs(template) {
|
|
2428
|
-
const fields = normalizeSchemaPages(template.schemas).flat();
|
|
2429
|
-
if (fields.length === 0) return [{}];
|
|
2430
|
-
const input = {};
|
|
2431
|
-
for (const schema of fields) {
|
|
2432
|
-
if (typeof schema !== "object" || schema === null) continue;
|
|
2433
|
-
const name = schema.name;
|
|
2434
|
-
const content = schema.content;
|
|
2435
|
-
const readOnly = schema.readOnly;
|
|
2436
|
-
const type = schema.type;
|
|
2437
|
-
if (readOnly) continue;
|
|
2438
|
-
if (name) input[name] = content || (type === "image" || type === "signature" ? "" : `Sample ${name}`);
|
|
2439
|
-
}
|
|
2440
|
-
return [input];
|
|
2441
|
-
}
|
|
2442
|
-
var examples_default = defineCommand({
|
|
2443
|
-
meta: {
|
|
2444
|
-
name: "examples",
|
|
2445
|
-
description: "List and output example pdfme templates"
|
|
2446
|
-
},
|
|
2447
|
-
args: examplesArgs,
|
|
2448
|
-
async run({ args, rawArgs }) {
|
|
2449
|
-
return runWithContract({ json: Boolean(args.json) }, async () => {
|
|
2450
|
-
assertNoUnknownFlags(rawArgs, examplesArgs);
|
|
2451
|
-
let manifestResult;
|
|
2452
|
-
try {
|
|
2453
|
-
manifestResult = await getExampleManifest();
|
|
2454
|
-
} catch (error) {
|
|
2455
|
-
fail(`Failed to load examples manifest. ${error instanceof Error ? error.message : String(error)}`, {
|
|
2456
|
-
code: "EIO",
|
|
2457
|
-
exitCode: 3,
|
|
2458
|
-
cause: error
|
|
2459
|
-
});
|
|
2460
|
-
}
|
|
2461
|
-
const templateEntries = manifestResult.manifest.templates;
|
|
2462
|
-
const templateNames = templateEntries.map((entry) => entry.name).filter((name) => typeof name === "string" && name.length > 0).sort();
|
|
2463
|
-
if (args.verbose) {
|
|
2464
|
-
console.error(`Base URL: ${getExamplesBaseUrl()}`);
|
|
2465
|
-
console.error(`Manifest source: ${manifestResult.source}`);
|
|
2466
|
-
if (manifestResult.url) console.error(`Manifest URL: ${manifestResult.url}`);
|
|
2467
|
-
console.error(`Templates: ${templateNames.length}`);
|
|
2468
|
-
}
|
|
2469
|
-
if (args.list || !args.name) {
|
|
2470
|
-
if (args.json) printJson({
|
|
2471
|
-
ok: true,
|
|
2472
|
-
command: "examples",
|
|
2473
|
-
mode: "list",
|
|
2474
|
-
templateCount: templateNames.length,
|
|
2475
|
-
source: manifestResult.source,
|
|
2476
|
-
baseUrl: getExamplesBaseUrl(),
|
|
2477
|
-
manifest: manifestResult.manifest
|
|
2478
|
-
});
|
|
2479
|
-
else {
|
|
2480
|
-
console.log("Available templates:");
|
|
2481
|
-
for (const name of templateNames) console.log(` ${name}`);
|
|
2482
|
-
}
|
|
2483
|
-
return;
|
|
2484
|
-
}
|
|
2485
|
-
if (!templateEntries.find((template) => template.name === args.name)) fail(`Template "${args.name}" not found. Available templates: ${templateNames.join(", ")}`, {
|
|
2486
|
-
code: "EARG",
|
|
2487
|
-
exitCode: 1
|
|
2488
|
-
});
|
|
2489
|
-
let templateResult;
|
|
2490
|
-
try {
|
|
2491
|
-
templateResult = await fetchExampleTemplateWithSource(args.name, { manifest: manifestResult.manifest });
|
|
2492
|
-
} catch (error) {
|
|
2493
|
-
fail(`Failed to load example template "${args.name}". ${error instanceof Error ? error.message : String(error)}`, {
|
|
2494
|
-
code: "EIO",
|
|
2495
|
-
exitCode: 3,
|
|
2496
|
-
cause: error
|
|
2497
|
-
});
|
|
2498
|
-
}
|
|
2499
|
-
const output = args.withInputs ? buildExampleJob(templateResult.template) : templateResult.template;
|
|
2500
|
-
const mode = args.withInputs ? "job" : "template";
|
|
2501
|
-
const stats = countTemplateStats(templateResult.template);
|
|
2502
|
-
const inputCount = args.withInputs ? output.inputs?.length ?? 0 : void 0;
|
|
2503
|
-
if (args.verbose) {
|
|
2504
|
-
console.error(`Template: ${args.name}`);
|
|
2505
|
-
console.error(`Template source: ${templateResult.source}`);
|
|
2506
|
-
if (templateResult.url) console.error(`Template URL: ${templateResult.url}`);
|
|
2507
|
-
console.error(`Mode: ${mode}`);
|
|
2508
|
-
console.error(`Template pages: ${stats.templatePageCount}`);
|
|
2509
|
-
console.error(`Fields: ${stats.fieldCount}`);
|
|
2510
|
-
if (inputCount !== void 0) console.error(`Inputs: ${inputCount} set(s)`);
|
|
2511
|
-
console.error(`Output: ${args.output ?? "stdout"}`);
|
|
2512
|
-
}
|
|
2513
|
-
if (args.output) {
|
|
2514
|
-
writeOutput(args.output, new TextEncoder().encode(JSON.stringify(output, null, 2)));
|
|
2515
|
-
if (args.json) printJson({
|
|
2516
|
-
ok: true,
|
|
2517
|
-
command: "examples",
|
|
2518
|
-
name: args.name,
|
|
2519
|
-
mode,
|
|
2520
|
-
source: templateResult.source,
|
|
2521
|
-
templatePageCount: stats.templatePageCount,
|
|
2522
|
-
fieldCount: stats.fieldCount,
|
|
2523
|
-
...inputCount !== void 0 ? { inputCount } : {},
|
|
2524
|
-
outputPath: args.output
|
|
2525
|
-
});
|
|
2526
|
-
else {
|
|
2527
|
-
const label = args.withInputs ? "Job file" : "Template";
|
|
2528
|
-
console.log(`\u2713 ${label} written to ${args.output}`);
|
|
2529
|
-
}
|
|
2530
|
-
return;
|
|
2531
|
-
}
|
|
2532
|
-
if (args.json) printJson({
|
|
2533
|
-
ok: true,
|
|
2534
|
-
command: "examples",
|
|
2535
|
-
name: args.name,
|
|
2536
|
-
source: templateResult.source,
|
|
2537
|
-
mode,
|
|
2538
|
-
templatePageCount: stats.templatePageCount,
|
|
2539
|
-
fieldCount: stats.fieldCount,
|
|
2540
|
-
...inputCount !== void 0 ? { inputCount } : {},
|
|
2541
|
-
data: output
|
|
2542
|
-
});
|
|
2543
|
-
else console.log(JSON.stringify(output, null, 2));
|
|
2544
|
-
});
|
|
2545
|
-
}
|
|
2546
|
-
});
|
|
2547
|
-
function buildExampleJob(template) {
|
|
2548
|
-
const job = {
|
|
2549
|
-
template,
|
|
2550
|
-
inputs: generateSampleInputs(template)
|
|
2551
|
-
};
|
|
2552
|
-
const font = getOfficialExampleFonts(template);
|
|
2553
|
-
if (font) job.options = { font };
|
|
2554
|
-
return job;
|
|
2555
|
-
}
|
|
2556
|
-
function countTemplateStats(template) {
|
|
2557
|
-
const schemaPages = normalizeSchemaPages(template.schemas);
|
|
2558
|
-
return {
|
|
2559
|
-
templatePageCount: schemaPages.length,
|
|
2560
|
-
fieldCount: schemaPages.reduce((count, page) => count + page.length, 0)
|
|
2561
|
-
};
|
|
2562
|
-
}
|
|
2563
|
-
function normalizeSchemaPages(rawSchemas) {
|
|
2564
|
-
if (!Array.isArray(rawSchemas)) return [];
|
|
2565
|
-
return rawSchemas.map((page) => {
|
|
2566
|
-
if (Array.isArray(page)) return page.filter((schema) => typeof schema === "object" && schema !== null);
|
|
2567
|
-
if (typeof page === "object" && page !== null) return Object.entries(page).map(([name, schema]) => typeof schema === "object" && schema !== null ? {
|
|
2568
|
-
...schema,
|
|
2569
|
-
name: schema.name ?? name
|
|
2570
|
-
} : null).filter((schema) => schema !== null);
|
|
2571
|
-
return [];
|
|
2572
|
-
});
|
|
2573
|
-
}
|
|
2244
|
+
var CLI_VERSION = "6.1.7-dev.1";
|
|
2574
2245
|
//#endregion
|
|
2575
2246
|
//#region src/commands/doctor.ts
|
|
2576
2247
|
var doctorArgs = {
|
|
@@ -2615,11 +2286,6 @@ var doctorArgs = {
|
|
|
2615
2286
|
type: "boolean",
|
|
2616
2287
|
description: "Simulate generate --image when previewing runtime output paths",
|
|
2617
2288
|
default: false
|
|
2618
|
-
},
|
|
2619
|
-
imageFormat: {
|
|
2620
|
-
type: "string",
|
|
2621
|
-
description: "Image format to use when previewing runtime output paths: png | jpeg",
|
|
2622
|
-
default: "png"
|
|
2623
2289
|
}
|
|
2624
2290
|
};
|
|
2625
2291
|
var doctor_default = defineCommand({
|
|
@@ -2632,7 +2298,6 @@ var doctor_default = defineCommand({
|
|
|
2632
2298
|
return runWithContract({ json: Boolean(args.json) }, async () => {
|
|
2633
2299
|
assertNoUnknownFlags(rawArgs, doctorArgs);
|
|
2634
2300
|
const invocation = resolveDoctorInvocation(args);
|
|
2635
|
-
const imageFormat = parseEnumArg("imageFormat", args.imageFormat, ["png", "jpeg"]);
|
|
2636
2301
|
const environment = getEnvironmentReport();
|
|
2637
2302
|
if (invocation.target === "environment") {
|
|
2638
2303
|
const issues = collectEnvironmentIssues(environment);
|
|
@@ -2660,7 +2325,6 @@ var doctor_default = defineCommand({
|
|
|
2660
2325
|
output: args.output,
|
|
2661
2326
|
force: Boolean(args.force),
|
|
2662
2327
|
image: Boolean(args.image),
|
|
2663
|
-
imageFormat,
|
|
2664
2328
|
rawArgs
|
|
2665
2329
|
}
|
|
2666
2330
|
});
|
|
@@ -2886,8 +2550,8 @@ function diagnoseRuntime(source, options) {
|
|
|
2886
2550
|
output,
|
|
2887
2551
|
imageOutputs: {
|
|
2888
2552
|
enabled: options.image,
|
|
2889
|
-
format:
|
|
2890
|
-
paths: options.image ? getImageOutputPaths(options.output, estimatedPages
|
|
2553
|
+
format: "png",
|
|
2554
|
+
paths: options.image ? getImageOutputPaths(options.output, estimatedPages) : [],
|
|
2891
2555
|
directory: dirname(output.resolvedPath)
|
|
2892
2556
|
}
|
|
2893
2557
|
};
|
|
@@ -3130,7 +2794,6 @@ runMain(defineCommand({
|
|
|
3130
2794
|
validate: validate_default,
|
|
3131
2795
|
pdf2img: pdf2img_default,
|
|
3132
2796
|
pdf2size: pdf2size_default,
|
|
3133
|
-
examples: examples_default,
|
|
3134
2797
|
doctor: doctor_default
|
|
3135
2798
|
}
|
|
3136
2799
|
}));
|