@polotno/pdf-export 0.1.18 → 0.1.20
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 +52 -0
- package/lib/compare-render.d.ts +1 -0
- package/lib/compare-render.js +185 -0
- package/lib/figure.d.ts +10 -0
- package/lib/figure.js +53 -48
- package/lib/ghostscript.d.ts +21 -0
- package/lib/ghostscript.js +98 -128
- package/lib/group.d.ts +5 -0
- package/lib/group.js +4 -8
- package/lib/image.d.ts +21 -0
- package/lib/image.js +147 -89
- package/lib/index.d.ts +26 -0
- package/lib/index.js +133 -0
- package/lib/line.d.ts +10 -0
- package/lib/line.js +41 -57
- package/lib/spot-colors.d.ts +38 -0
- package/lib/spot-colors.js +141 -0
- package/lib/svg-render.d.ts +9 -0
- package/lib/svg-render.js +19 -28
- package/lib/svg.d.ts +11 -0
- package/lib/svg.js +212 -233
- package/lib/text.d.ts +39 -0
- package/lib/text.js +558 -165
- package/lib/utils.d.ts +16 -0
- package/lib/utils.js +118 -72
- package/package.json +35 -15
- package/index.js +0 -130
package/README.md
CHANGED
|
@@ -49,9 +49,61 @@ await jsonToPDF(json, './book-cover.pdf', {
|
|
|
49
49
|
});
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
+
## Spot Color / Foil Support
|
|
53
|
+
|
|
54
|
+
For professional printing with special inks like metallic foils, Pantone colors, or other spot colors:
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
await jsonToPDF(json, './output.pdf', {
|
|
58
|
+
pdfx1a: true,
|
|
59
|
+
spotColors: {
|
|
60
|
+
// Map any color to a spot color
|
|
61
|
+
'rgba(255,215,0,1)': {
|
|
62
|
+
name: 'Gold Foil',
|
|
63
|
+
type: 'pantone',
|
|
64
|
+
pantoneCode: 'Pantone 871 C', // Optional reference
|
|
65
|
+
cmyk: [0, 0.15, 0.5, 0], // CMYK fallback (0-1 range)
|
|
66
|
+
},
|
|
67
|
+
'#C0C0C0': {
|
|
68
|
+
name: 'Silver Foil',
|
|
69
|
+
type: 'custom',
|
|
70
|
+
cmyk: [0, 0, 0, 0.25],
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**How it works:**
|
|
77
|
+
|
|
78
|
+
- Any element with a matching fill or stroke color is automatically converted to use the spot color
|
|
79
|
+
- Colors are matched flexibly - `'#FFD700'`, `'#ffd700'`, and `'rgba(255,215,0,1)'` all match
|
|
80
|
+
- Spot colors are preserved as separation color spaces in PDF/X-1a output
|
|
81
|
+
- CMYK fallback values are used when viewers don't support spot colors
|
|
82
|
+
- Works with all element types: text, shapes, lines, and SVG elements
|
|
83
|
+
|
|
84
|
+
**Color Format Support:**
|
|
85
|
+
|
|
86
|
+
- Hex: `'#FFD700'` or `'#ffd700'`
|
|
87
|
+
- RGB: `'rgb(255,215,0)'`
|
|
88
|
+
- RGBA: `'rgba(255,215,0,1)'`
|
|
89
|
+
|
|
90
|
+
**Tips:**
|
|
91
|
+
|
|
92
|
+
- Use professional color references (like Pantone codes) to communicate exact requirements to printers
|
|
93
|
+
- Provide accurate CMYK fallback values for preview and proof prints
|
|
94
|
+
- Spot colors work best with PDF/X-1a export enabled
|
|
95
|
+
- You can verify spot colors in Adobe Acrobat by checking Output Preview > Separations
|
|
96
|
+
|
|
52
97
|
## Requirements
|
|
53
98
|
|
|
54
99
|
- **GhostScript** must be installed for PDF/X-1a conversion
|
|
55
100
|
- macOS: `brew install ghostscript`
|
|
56
101
|
- Ubuntu: `apt-get install ghostscript`
|
|
57
102
|
- Windows: Download from [ghostscript.com](https://www.ghostscript.com/)
|
|
103
|
+
|
|
104
|
+
## Development
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
npm run build # Build the library
|
|
108
|
+
npm test # Run tests
|
|
109
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Compare PDFs produced by polotno-node vs this library for a subset of samples.
|
|
3
|
+
|
|
4
|
+
Usage examples:
|
|
5
|
+
node lib/compare-render.js --limit 10
|
|
6
|
+
node lib/compare-render.js --glob "2021-03-*"
|
|
7
|
+
node lib/compare-render.js --start 0 --end 50
|
|
8
|
+
|
|
9
|
+
Outputs:
|
|
10
|
+
- builds PDFs under `comparisons/<sample>/polotno-node.pdf` and `comparisons/<sample>/current.pdf`
|
|
11
|
+
- writes a simple byte-size diff report to `comparisons/report.json`
|
|
12
|
+
*/
|
|
13
|
+
import fs from 'fs';
|
|
14
|
+
import path from 'path';
|
|
15
|
+
import crypto from 'crypto';
|
|
16
|
+
import { promisify } from 'util';
|
|
17
|
+
import { createInstance } from 'polotno-node';
|
|
18
|
+
import { pdfToPng } from 'pdf-to-png-converter';
|
|
19
|
+
import pixelmatch from 'pixelmatch';
|
|
20
|
+
import { PNG } from 'pngjs';
|
|
21
|
+
import { jsonToPDF } from './index.js';
|
|
22
|
+
const readFile = promisify(fs.readFile);
|
|
23
|
+
const writeFile = promisify(fs.writeFile);
|
|
24
|
+
const mkdir = promisify(fs.mkdir);
|
|
25
|
+
const access = promisify(fs.access);
|
|
26
|
+
const readdir = promisify(fs.readdir);
|
|
27
|
+
const SAMPLES_DIR = path.resolve('./samples');
|
|
28
|
+
const OUTPUT_DIR = path.resolve('./comparisons');
|
|
29
|
+
function parseArgs() {
|
|
30
|
+
const args = process.argv.slice(2);
|
|
31
|
+
const result = {};
|
|
32
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
33
|
+
const a = args[i];
|
|
34
|
+
if (a === '--limit')
|
|
35
|
+
result.limit = Number(args[++i]);
|
|
36
|
+
else if (a === '--start')
|
|
37
|
+
result.start = Number(args[++i]);
|
|
38
|
+
else if (a === '--end')
|
|
39
|
+
result.end = Number(args[++i]);
|
|
40
|
+
else if (a === '--glob')
|
|
41
|
+
result.glob = String(args[++i]);
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
async function tryEnsureDir(dir) {
|
|
46
|
+
try {
|
|
47
|
+
await access(dir, fs.constants.F_OK);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
await mkdir(dir, { recursive: true });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function sha256(buf) {
|
|
54
|
+
return crypto.createHash('sha256').update(buf).digest('hex');
|
|
55
|
+
}
|
|
56
|
+
async function loadPolotnoJson(filePath) {
|
|
57
|
+
const buf = await readFile(filePath, 'utf8');
|
|
58
|
+
return JSON.parse(buf);
|
|
59
|
+
}
|
|
60
|
+
let polotnoInstance = null;
|
|
61
|
+
async function getPolotnoInstance() {
|
|
62
|
+
if (polotnoInstance)
|
|
63
|
+
return polotnoInstance;
|
|
64
|
+
const key = process.env.POLOTNO_API_KEY || process.env.POLOTNO_API_TOKEN || '';
|
|
65
|
+
polotnoInstance = await createInstance({ key });
|
|
66
|
+
return polotnoInstance;
|
|
67
|
+
}
|
|
68
|
+
async function renderWithPolotnoNode(json, outPath) {
|
|
69
|
+
const instance = await getPolotnoInstance();
|
|
70
|
+
const base64 = await instance.jsonToPDFBase64(json, {
|
|
71
|
+
pixelRatio: 4,
|
|
72
|
+
});
|
|
73
|
+
await writeFile(outPath, Buffer.from(base64, 'base64'));
|
|
74
|
+
}
|
|
75
|
+
async function renderWithCurrent(json, outPath) {
|
|
76
|
+
await jsonToPDF(json, outPath, {});
|
|
77
|
+
}
|
|
78
|
+
function pickSampleDirs(allDirs, { start = 0, end, limit, glob }) {
|
|
79
|
+
let list = allDirs.filter((d) => !d.startsWith('.'));
|
|
80
|
+
if (glob) {
|
|
81
|
+
const re = new RegExp('^' + glob.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*') + '$');
|
|
82
|
+
list = list.filter((d) => re.test(d));
|
|
83
|
+
}
|
|
84
|
+
if (typeof start === 'number' && typeof end === 'number' && end > start) {
|
|
85
|
+
list = list.slice(start, end);
|
|
86
|
+
}
|
|
87
|
+
if (typeof limit === 'number' && limit > 0) {
|
|
88
|
+
list = list.slice(0, limit);
|
|
89
|
+
}
|
|
90
|
+
return list;
|
|
91
|
+
}
|
|
92
|
+
async function run() {
|
|
93
|
+
const args = parseArgs();
|
|
94
|
+
await tryEnsureDir(OUTPUT_DIR);
|
|
95
|
+
const all = await readdir(SAMPLES_DIR, { withFileTypes: true });
|
|
96
|
+
const sampleDirs = pickSampleDirs(all.filter((d) => d.isDirectory()).map((d) => d.name), args);
|
|
97
|
+
const report = [];
|
|
98
|
+
for (const dir of sampleDirs) {
|
|
99
|
+
const samplePath = path.join(SAMPLES_DIR, dir);
|
|
100
|
+
const jsonFiles = (await readdir(samplePath)).filter((f) => f.endsWith('.json') && f !== 'meta.json');
|
|
101
|
+
if (jsonFiles.length === 0)
|
|
102
|
+
continue;
|
|
103
|
+
const designPath = path.join(samplePath, jsonFiles[0]);
|
|
104
|
+
const json = await loadPolotnoJson(designPath);
|
|
105
|
+
const outDir = path.join(OUTPUT_DIR, dir);
|
|
106
|
+
await tryEnsureDir(outDir);
|
|
107
|
+
const polotnoOut = path.join(outDir, 'polotno-node.pdf');
|
|
108
|
+
const currentOut = path.join(outDir, 'current.pdf');
|
|
109
|
+
if (!fs.existsSync(polotnoOut)) {
|
|
110
|
+
// Render both
|
|
111
|
+
try {
|
|
112
|
+
await renderWithPolotnoNode(json, polotnoOut);
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
await writeFile(path.join(outDir, 'error-polotno.txt'), String(e?.stack || e?.message || e));
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
await renderWithCurrent(json, currentOut);
|
|
121
|
+
}
|
|
122
|
+
catch (e) {
|
|
123
|
+
await writeFile(path.join(outDir, 'error-current.txt'), String(e?.stack || e?.message || e));
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
// Rasterize first page of each PDF to PNG (via pdf-to-png-converter)
|
|
127
|
+
try {
|
|
128
|
+
const [imgA] = await pdfToPng(polotnoOut, { disableFontFace: true, viewportScale: 2, pagesToProcess: [1] });
|
|
129
|
+
const [imgB] = await pdfToPng(currentOut, { disableFontFace: true, viewportScale: 2, pagesToProcess: [1] });
|
|
130
|
+
const pngA = imgA.content;
|
|
131
|
+
const pngB = imgB.content;
|
|
132
|
+
const pngAPath = path.join(outDir, 'polotno-node.png');
|
|
133
|
+
const pngBPath = path.join(outDir, 'current.png');
|
|
134
|
+
await writeFile(pngAPath, pngA);
|
|
135
|
+
await writeFile(pngBPath, pngB);
|
|
136
|
+
// Visual diff with pixelmatch
|
|
137
|
+
const imgAParsed = PNG.sync.read(pngA);
|
|
138
|
+
const imgBParsed = PNG.sync.read(pngB);
|
|
139
|
+
const width = Math.min(imgAParsed.width, imgBParsed.width);
|
|
140
|
+
const height = Math.min(imgAParsed.height, imgBParsed.height);
|
|
141
|
+
const cropA = new PNG({ width, height });
|
|
142
|
+
const cropB = new PNG({ width, height });
|
|
143
|
+
PNG.bitblt(imgAParsed, cropA, 0, 0, width, height, 0, 0);
|
|
144
|
+
PNG.bitblt(imgBParsed, cropB, 0, 0, width, height, 0, 0);
|
|
145
|
+
const diff = new PNG({ width, height });
|
|
146
|
+
const numDiff = pixelmatch(cropA.data, cropB.data, diff.data, width, height, {
|
|
147
|
+
threshold: 0.1,
|
|
148
|
+
includeAA: true,
|
|
149
|
+
});
|
|
150
|
+
const diffPath = path.join(outDir, 'diff.png');
|
|
151
|
+
await writeFile(diffPath, PNG.sync.write(diff));
|
|
152
|
+
const a = await readFile(polotnoOut);
|
|
153
|
+
const b = await readFile(currentOut);
|
|
154
|
+
const item = {
|
|
155
|
+
sample: dir,
|
|
156
|
+
polotnoBytes: a.length,
|
|
157
|
+
currentBytes: b.length,
|
|
158
|
+
bytesDiff: Math.abs(a.length - b.length),
|
|
159
|
+
polotnoSha256: sha256(a),
|
|
160
|
+
currentSha256: sha256(b),
|
|
161
|
+
identical: numDiff === 0,
|
|
162
|
+
diffPixels: numDiff,
|
|
163
|
+
imageWidth: width,
|
|
164
|
+
imageHeight: height,
|
|
165
|
+
};
|
|
166
|
+
report.push(item);
|
|
167
|
+
await writeFile(path.join(outDir, 'summary.json'), JSON.stringify(item, null, 2));
|
|
168
|
+
console.log(`${dir}: diffPixels=${numDiff} sizeA=${item.polotnoBytes} sizeB=${item.currentBytes}`);
|
|
169
|
+
}
|
|
170
|
+
catch (e) {
|
|
171
|
+
await writeFile(path.join(outDir, 'error-diff.txt'), String(e?.stack || e?.message || e));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
await writeFile(path.join(OUTPUT_DIR, 'report.json'), JSON.stringify(report, null, 2));
|
|
175
|
+
console.log(`Done. Compared ${report.length} samples. Report: ${path.join(OUTPUT_DIR, 'report.json')}`);
|
|
176
|
+
// Close polotno-node instance to allow process to exit
|
|
177
|
+
if (polotnoInstance) {
|
|
178
|
+
await polotnoInstance.close();
|
|
179
|
+
polotnoInstance = null;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
run().catch((err) => {
|
|
183
|
+
console.error(err);
|
|
184
|
+
process.exitCode = 1;
|
|
185
|
+
});
|
package/lib/figure.d.ts
ADDED
package/lib/figure.js
CHANGED
|
@@ -1,49 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
1
|
+
import { parseColor } from './utils.js';
|
|
2
|
+
import { figureToSvg } from 'polotno/utils/figure-to-svg';
|
|
3
|
+
export function renderFigure(doc, element) {
|
|
4
|
+
const svgStr = figureToSvg({
|
|
5
|
+
...element,
|
|
6
|
+
fill: (() => {
|
|
7
|
+
if (!element.fill)
|
|
8
|
+
return 'transparent';
|
|
9
|
+
const color = parseColor(element.fill);
|
|
10
|
+
if (!color || !color.rgba)
|
|
11
|
+
return element.fill;
|
|
12
|
+
let oldOpacity = color.rgba[3] || 1;
|
|
13
|
+
// Normalize opacity to 0-1 range if it's greater than 1
|
|
14
|
+
if (oldOpacity > 1) {
|
|
15
|
+
oldOpacity = oldOpacity / 100;
|
|
16
|
+
}
|
|
17
|
+
const rgba = color.rgba
|
|
18
|
+
.slice(0, 3)
|
|
19
|
+
.concat(oldOpacity * element.opacity)
|
|
20
|
+
.join(',');
|
|
21
|
+
return `rgba(${rgba})`;
|
|
22
|
+
})(),
|
|
23
|
+
stroke: (() => {
|
|
24
|
+
if (!element.stroke || element.strokeWidth === 0)
|
|
25
|
+
return 'none';
|
|
26
|
+
const color = parseColor(element.stroke);
|
|
27
|
+
if (!color || !color.rgba)
|
|
28
|
+
return element.stroke;
|
|
29
|
+
let oldOpacity = color.rgba[3] || 1;
|
|
30
|
+
// Normalize opacity to 0-1 range if it's greater than 1
|
|
31
|
+
if (oldOpacity > 1) {
|
|
32
|
+
oldOpacity = oldOpacity / 100;
|
|
33
|
+
}
|
|
34
|
+
const rgba = color.rgba
|
|
35
|
+
.slice(0, 3)
|
|
36
|
+
.concat(oldOpacity * element.opacity)
|
|
37
|
+
.join(',');
|
|
38
|
+
return `rgba(${rgba})`;
|
|
39
|
+
})(),
|
|
40
|
+
});
|
|
41
|
+
doc.addSVG(svgStr, 0, 0, {
|
|
42
|
+
preserveAspectRatio: 'xMinYMin meet',
|
|
43
|
+
width: element.width,
|
|
44
|
+
height: element.height,
|
|
45
|
+
opacity: element.opacity,
|
|
46
|
+
colorCallback: (colors) => {
|
|
47
|
+
if (!colors) {
|
|
48
|
+
return colors;
|
|
49
|
+
}
|
|
50
|
+
const [color, opacity] = colors;
|
|
51
|
+
return [color, opacity * element.opacity];
|
|
52
|
+
},
|
|
53
|
+
});
|
|
45
54
|
}
|
|
46
|
-
|
|
47
|
-
module.exports = {
|
|
48
|
-
renderFigure,
|
|
49
|
-
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface PDFXMetadata {
|
|
2
|
+
title?: string;
|
|
3
|
+
author?: string;
|
|
4
|
+
application?: string;
|
|
5
|
+
producer?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface ConversionOptions {
|
|
8
|
+
metadata?: PDFXMetadata;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Convert PDF to PDF/X-1a using GhostScript
|
|
12
|
+
* @param inputPath - Path to input PDF
|
|
13
|
+
* @param outputPath - Path to output PDF/X-1a file
|
|
14
|
+
* @param options - Conversion options
|
|
15
|
+
*/
|
|
16
|
+
export declare function convertToPDFX1a(inputPath: string, outputPath: string, options?: ConversionOptions): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Validate if PDF is PDF/X-1a compliant
|
|
19
|
+
* @param pdfPath - Path to PDF file
|
|
20
|
+
*/
|
|
21
|
+
export declare function validatePDFX1a(pdfPath: string): Promise<boolean>;
|
package/lib/ghostscript.js
CHANGED
|
@@ -1,100 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
5
4
|
/**
|
|
6
5
|
* Convert PDF to PDF/X-1a using GhostScript
|
|
7
|
-
* @param
|
|
8
|
-
* @param
|
|
9
|
-
* @param
|
|
10
|
-
* @returns {Promise<void>}
|
|
6
|
+
* @param inputPath - Path to input PDF
|
|
7
|
+
* @param outputPath - Path to output PDF/X-1a file
|
|
8
|
+
* @param options - Conversion options
|
|
11
9
|
*/
|
|
12
|
-
async function convertToPDFX1a(inputPath, outputPath, options = {}) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
args.splice(-1, 0, tempPSFile); // Insert before input file
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
console.log('GhostScript command:', 'gs', args.join(' '));
|
|
53
|
-
|
|
54
|
-
const gs = spawn('gs', args);
|
|
55
|
-
let stderr = '';
|
|
56
|
-
|
|
57
|
-
gs.stderr.on('data', (data) => {
|
|
58
|
-
stderr += data.toString();
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
gs.on('close', (code) => {
|
|
62
|
-
// Clean up temp metadata file
|
|
63
|
-
if (options.metadata) {
|
|
64
|
-
const tempPSFile = path.join(path.dirname(outputPath), 'metadata.ps');
|
|
65
|
-
if (fs.existsSync(tempPSFile)) {
|
|
66
|
-
fs.unlinkSync(tempPSFile);
|
|
10
|
+
export async function convertToPDFX1a(inputPath, outputPath, options = {}) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
// PDF/X-1a conversion parameters with stroke preservation
|
|
13
|
+
const args = [
|
|
14
|
+
'-dNOPAUSE',
|
|
15
|
+
'-dBATCH',
|
|
16
|
+
'-dSAFER',
|
|
17
|
+
'-sDEVICE=pdfwrite',
|
|
18
|
+
'-dCompatibilityLevel=1.3',
|
|
19
|
+
'-dPDFX=true',
|
|
20
|
+
'-dColorConversionStrategy=/CMYK',
|
|
21
|
+
'-dProcessColorModel=/DeviceCMYK',
|
|
22
|
+
'-dUseCIEColor=true',
|
|
23
|
+
'-sColorConversionStrategy=CMYK',
|
|
24
|
+
'-sProcessColorModel=DeviceCMYK',
|
|
25
|
+
'-dOverrideICC=true',
|
|
26
|
+
// Preserve text rendering modes and strokes
|
|
27
|
+
'-dPreserveAnnots=true',
|
|
28
|
+
'-dPreserveCopyPage=true',
|
|
29
|
+
'-dPreserveDeviceN=true', // IMPORTANT: Preserves spot/separation colors
|
|
30
|
+
'-dDoThumbnails=false',
|
|
31
|
+
'-dDetectDuplicateImages=true', // Enable duplicate image detection
|
|
32
|
+
'-dCompressFonts=true', // Enable font compression
|
|
33
|
+
'-dSubsetFonts=true', // Subset fonts to reduce size
|
|
34
|
+
'-dEmbedAllFonts=true',
|
|
35
|
+
// Better handling of text rendering modes
|
|
36
|
+
'-dPDFSETTINGS=/prepress',
|
|
37
|
+
`-sOutputFile=${outputPath}`,
|
|
38
|
+
inputPath,
|
|
39
|
+
];
|
|
40
|
+
// Add custom metadata if provided
|
|
41
|
+
if (options.metadata) {
|
|
42
|
+
// Create temporary PostScript file with metadata
|
|
43
|
+
const psMetadata = createMetadataPS(options.metadata);
|
|
44
|
+
const tempPSFile = path.join(path.dirname(outputPath), 'metadata.ps');
|
|
45
|
+
fs.writeFileSync(tempPSFile, psMetadata);
|
|
46
|
+
args.splice(-1, 0, tempPSFile); // Insert before input file
|
|
67
47
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
48
|
+
const gs = spawn('gs', args);
|
|
49
|
+
let stderr = '';
|
|
50
|
+
gs.stderr.on('data', (data) => {
|
|
51
|
+
stderr += data.toString();
|
|
52
|
+
});
|
|
53
|
+
gs.on('close', (code) => {
|
|
54
|
+
// Clean up temp metadata file
|
|
55
|
+
if (options.metadata) {
|
|
56
|
+
const tempPSFile = path.join(path.dirname(outputPath), 'metadata.ps');
|
|
57
|
+
if (fs.existsSync(tempPSFile)) {
|
|
58
|
+
fs.unlinkSync(tempPSFile);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (code === 0) {
|
|
62
|
+
resolve();
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
reject(new Error(`GhostScript failed with code ${code}: ${stderr}`));
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
gs.on('error', (error) => {
|
|
69
|
+
reject(new Error(`Failed to start GhostScript: ${error.message}`));
|
|
70
|
+
});
|
|
80
71
|
});
|
|
81
|
-
});
|
|
82
72
|
}
|
|
83
|
-
|
|
84
73
|
/**
|
|
85
74
|
* Create PostScript metadata for PDF/X-1a
|
|
86
|
-
* @param
|
|
87
|
-
* @returns
|
|
75
|
+
* @param metadata - Metadata object
|
|
76
|
+
* @returns PostScript code
|
|
88
77
|
*/
|
|
89
78
|
function createMetadataPS(metadata) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
author = 'KDP Cover Creator',
|
|
93
|
-
application = 'KDP CoverCreator',
|
|
94
|
-
producer = 'Polotno PDF Export',
|
|
95
|
-
} = metadata;
|
|
96
|
-
|
|
97
|
-
return `
|
|
79
|
+
const { title = 'Cover Creator Export', author = 'KDP Cover Creator', application = 'KDP CoverCreator', producer = 'Polotno PDF Export', } = metadata;
|
|
80
|
+
return `
|
|
98
81
|
%!PS
|
|
99
82
|
% PDF/X-1a Metadata
|
|
100
83
|
[/Title (${title})
|
|
@@ -114,49 +97,36 @@ function createMetadataPS(metadata) {
|
|
|
114
97
|
/PDFX pdfmark
|
|
115
98
|
`;
|
|
116
99
|
}
|
|
117
|
-
|
|
118
100
|
/**
|
|
119
101
|
* Validate if PDF is PDF/X-1a compliant
|
|
120
|
-
* @param
|
|
121
|
-
* @returns {Promise<boolean>}
|
|
102
|
+
* @param pdfPath - Path to PDF file
|
|
122
103
|
*/
|
|
123
|
-
async function validatePDFX1a(pdfPath) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
104
|
+
export async function validatePDFX1a(pdfPath) {
|
|
105
|
+
return new Promise((resolve, reject) => {
|
|
106
|
+
const args = [
|
|
107
|
+
'-dNOPAUSE',
|
|
108
|
+
'-dBATCH',
|
|
109
|
+
'-sDEVICE=nullpage',
|
|
110
|
+
'-dPDFX=true',
|
|
111
|
+
pdfPath,
|
|
112
|
+
];
|
|
113
|
+
const gs = spawn('gs', args);
|
|
114
|
+
let stderr = '';
|
|
115
|
+
gs.stderr.on('data', (data) => {
|
|
116
|
+
stderr += data.toString();
|
|
117
|
+
});
|
|
118
|
+
gs.on('close', (code) => {
|
|
119
|
+
if (code === 0 &&
|
|
120
|
+
!stderr.includes('Error') &&
|
|
121
|
+
!stderr.includes('Warning')) {
|
|
122
|
+
resolve(true);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
resolve(false);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
gs.on('error', (error) => {
|
|
129
|
+
reject(new Error(`Failed to validate PDF: ${error.message}`));
|
|
130
|
+
});
|
|
138
131
|
});
|
|
139
|
-
|
|
140
|
-
gs.on('close', (code) => {
|
|
141
|
-
if (
|
|
142
|
-
code === 0 &&
|
|
143
|
-
!stderr.includes('Error') &&
|
|
144
|
-
!stderr.includes('Warning')
|
|
145
|
-
) {
|
|
146
|
-
resolve(true);
|
|
147
|
-
} else {
|
|
148
|
-
console.log('PDF/X-1a validation issues:', stderr);
|
|
149
|
-
resolve(false);
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
gs.on('error', (error) => {
|
|
154
|
-
reject(new Error(`Failed to validate PDF: ${error.message}`));
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
132
|
}
|
|
158
|
-
|
|
159
|
-
module.exports = {
|
|
160
|
-
convertToPDFX1a,
|
|
161
|
-
validatePDFX1a,
|
|
162
|
-
};
|
package/lib/group.d.ts
ADDED
package/lib/group.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
async function renderGroup(doc, element, renderElement, fonts, attrs) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export async function renderGroup(doc, element, renderElement, fonts, attrs, cache) {
|
|
2
|
+
for (const child of element.children) {
|
|
3
|
+
await renderElement({ doc, element: child, fonts, attrs, cache });
|
|
4
|
+
}
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
module.exports = {
|
|
8
|
-
renderGroup,
|
|
9
|
-
};
|