@pdfme/cli 6.1.6 → 6.1.8-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/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 +17 -53
- package/dist/index.js.map +1 -1
- package/dist/utils.d.ts +1 -1
- package/package.json +1 -1
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 {
|
|
@@ -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({
|
|
@@ -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]);
|
|
@@ -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,7 +2241,7 @@ var pdf2size_default = defineCommand({
|
|
|
2270
2241
|
});
|
|
2271
2242
|
//#endregion
|
|
2272
2243
|
//#region src/version.ts
|
|
2273
|
-
var CLI_VERSION = "6.1.
|
|
2244
|
+
var CLI_VERSION = "6.1.8-dev.1";
|
|
2274
2245
|
//#endregion
|
|
2275
2246
|
//#region src/commands/doctor.ts
|
|
2276
2247
|
var doctorArgs = {
|
|
@@ -2315,11 +2286,6 @@ var doctorArgs = {
|
|
|
2315
2286
|
type: "boolean",
|
|
2316
2287
|
description: "Simulate generate --image when previewing runtime output paths",
|
|
2317
2288
|
default: false
|
|
2318
|
-
},
|
|
2319
|
-
imageFormat: {
|
|
2320
|
-
type: "string",
|
|
2321
|
-
description: "Image format to use when previewing runtime output paths: png | jpeg",
|
|
2322
|
-
default: "png"
|
|
2323
2289
|
}
|
|
2324
2290
|
};
|
|
2325
2291
|
var doctor_default = defineCommand({
|
|
@@ -2332,7 +2298,6 @@ var doctor_default = defineCommand({
|
|
|
2332
2298
|
return runWithContract({ json: Boolean(args.json) }, async () => {
|
|
2333
2299
|
assertNoUnknownFlags(rawArgs, doctorArgs);
|
|
2334
2300
|
const invocation = resolveDoctorInvocation(args);
|
|
2335
|
-
const imageFormat = parseEnumArg("imageFormat", args.imageFormat, ["png", "jpeg"]);
|
|
2336
2301
|
const environment = getEnvironmentReport();
|
|
2337
2302
|
if (invocation.target === "environment") {
|
|
2338
2303
|
const issues = collectEnvironmentIssues(environment);
|
|
@@ -2360,7 +2325,6 @@ var doctor_default = defineCommand({
|
|
|
2360
2325
|
output: args.output,
|
|
2361
2326
|
force: Boolean(args.force),
|
|
2362
2327
|
image: Boolean(args.image),
|
|
2363
|
-
imageFormat,
|
|
2364
2328
|
rawArgs
|
|
2365
2329
|
}
|
|
2366
2330
|
});
|
|
@@ -2586,8 +2550,8 @@ function diagnoseRuntime(source, options) {
|
|
|
2586
2550
|
output,
|
|
2587
2551
|
imageOutputs: {
|
|
2588
2552
|
enabled: options.image,
|
|
2589
|
-
format:
|
|
2590
|
-
paths: options.image ? getImageOutputPaths(options.output, estimatedPages
|
|
2553
|
+
format: "png",
|
|
2554
|
+
paths: options.image ? getImageOutputPaths(options.output, estimatedPages) : [],
|
|
2591
2555
|
directory: dirname(output.resolvedPath)
|
|
2592
2556
|
}
|
|
2593
2557
|
};
|