openfig-cli 0.3.18 → 0.3.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/bin/cli.mjs CHANGED
@@ -30,6 +30,7 @@ const COMMANDS = {
30
30
  'remove-slide': './commands/remove-slide.mjs',
31
31
  'roundtrip': './commands/roundtrip.mjs',
32
32
  'export': './commands/export.mjs',
33
+ 'pdf': './commands/pdf.mjs',
33
34
  };
34
35
 
35
36
  const arg2 = process.argv[2];
@@ -40,6 +41,7 @@ if (!arg2 || arg2 === '--help' || arg2 === '-h') {
40
41
  console.log('Usage: openfig <command> [args...]\n');
41
42
  console.log('Commands:');
42
43
  console.log(' export Export slides as images (PNG/JPG/WEBP)');
44
+ console.log(' pdf Export slides as a multi-page PDF');
43
45
  console.log(' inspect Show document structure (node hierarchy tree)');
44
46
  console.log(' list-text List all text content in the deck');
45
47
  console.log(' list-overrides List editable override keys per symbol');
@@ -0,0 +1,66 @@
1
+ /**
2
+ * pdf — Export slides from a .deck file to a multi-page PDF.
3
+ *
4
+ * Usage:
5
+ * openfig pdf <file.deck> [options]
6
+ *
7
+ * Options:
8
+ * -o <file.pdf> Output PDF path (default: <deckname>.pdf)
9
+ * --slide <n> Export only slide N (1-based). Omit to export all.
10
+ * --scale <n> Zoom factor: 1 = 1920×1080, 0.5 = 960×540 (default: 1)
11
+ * --width <px> Output width in pixels (height scales proportionally)
12
+ * --fonts <dir> Extra font directory to load (can repeat)
13
+ */
14
+
15
+ import { writeFileSync } from 'fs';
16
+ import { parse, resolve } from 'path';
17
+ import { PDFDocument } from 'pdf-lib';
18
+ import { FigDeck } from '../../lib/core/fig-deck.mjs';
19
+ import { renderDeck, registerFontDir } from '../../lib/rasterizer/deck-rasterizer.mjs';
20
+ import { resolveFonts } from '../../lib/rasterizer/font-resolver.mjs';
21
+
22
+ export async function run(args, flags) {
23
+ const file = args[0];
24
+ if (!file) {
25
+ console.error('Usage: openfig pdf <file.deck> [options]\n');
26
+ console.error('Options:');
27
+ console.error(' -o <file.pdf> Output PDF path (default: <deckname>.pdf)');
28
+ console.error(' --slide <n> Export only slide N (1-based)');
29
+ console.error(' --scale <n> Zoom factor: 1 = 1920×1080 (default: 1)');
30
+ console.error(' --width <px> Output width in pixels');
31
+ console.error(' --fonts <dir> Extra font directory to load');
32
+ process.exit(1);
33
+ }
34
+
35
+ const defaultOut = parse(file).name + '.pdf';
36
+ const outPath = resolve(flags.o ?? flags.output ?? defaultOut);
37
+
38
+ const renderOpts = {};
39
+ if (flags.width) renderOpts.width = parseInt(flags.width);
40
+ else if (flags.scale) renderOpts.scale = parseFloat(flags.scale);
41
+
42
+ const fontDirs = [].concat(flags.fonts ?? []);
43
+ for (const d of fontDirs) registerFontDir(resolve(d));
44
+
45
+ const deck = await FigDeck.fromDeckFile(file);
46
+ await resolveFonts(deck, { quiet: false });
47
+
48
+ const slideFilter = flags.slide ? parseInt(flags.slide) : null;
49
+ const slides = await renderDeck(deck, renderOpts);
50
+
51
+ const doc = await PDFDocument.create();
52
+
53
+ let count = 0;
54
+ for (const { index, png } of slides) {
55
+ if (slideFilter && index + 1 !== slideFilter) continue;
56
+ const image = await doc.embedPng(png);
57
+ const page = doc.addPage([image.width, image.height]);
58
+ page.drawImage(image, { x: 0, y: 0, width: image.width, height: image.height });
59
+ count++;
60
+ console.log(` slide ${index + 1} → page ${count}`);
61
+ }
62
+
63
+ const pdfBytes = await doc.save();
64
+ writeFileSync(outPath, pdfBytes);
65
+ console.log(`\nExported ${count} slide(s) to ${outPath} (${pdfBytes.length} bytes)`);
66
+ }
@@ -278,12 +278,64 @@ export class FigDeck {
278
278
  });
279
279
  }
280
280
 
281
+ /**
282
+ * Validate deck integrity before saving. Warns about issues that would
283
+ * cause Figma to fail silently (blank slides, missing symbols, etc.).
284
+ */
285
+ validate() {
286
+ const warnings = [];
287
+ const symbols = this.getSymbols().filter(s => s.phase !== 'REMOVED');
288
+
289
+ // Check for variant name / variantPropSpecs mismatch
290
+ const byKey = new Map();
291
+ for (const sym of symbols) {
292
+ if (!sym.componentKey) continue;
293
+ if (!byKey.has(sym.componentKey)) byKey.set(sym.componentKey, []);
294
+ byKey.get(sym.componentKey).push(sym);
295
+ }
296
+ for (const [key, variants] of byKey) {
297
+ if (variants.length < 2) continue;
298
+ const specValues = new Set();
299
+ for (const sym of variants) {
300
+ if (sym.variantPropSpecs) {
301
+ for (const spec of sym.variantPropSpecs) specValues.add(spec.value);
302
+ }
303
+ }
304
+ for (const sym of variants) {
305
+ const parts = (sym.name || '').split(', ').map(p => p.split('=')[1]).filter(Boolean);
306
+ for (const val of parts) {
307
+ if (!specValues.has(val)) {
308
+ const id = `${sym.guid.sessionID}:${sym.guid.localID}`;
309
+ warnings.push(`SYMBOL ${id} "${sym.name}": variant value "${val}" not in variantPropSpecs — Figma will show blank slides`);
310
+ }
311
+ }
312
+ }
313
+ }
314
+
315
+ // Check for instances referencing missing symbols
316
+ for (const node of this.message.nodeChanges) {
317
+ if (node.type !== 'INSTANCE' || node.phase === 'REMOVED') continue;
318
+ const sid = node.symbolData?.symbolID;
319
+ if (!sid) continue;
320
+ const symId = `${sid.sessionID}:${sid.localID}`;
321
+ const sym = this.getNode(symId);
322
+ if (!sym || sym.type !== 'SYMBOL') {
323
+ const nid = `${node.guid.sessionID}:${node.guid.localID}`;
324
+ warnings.push(`INSTANCE ${nid} "${node.name}": references missing SYMBOL ${symId}`);
325
+ }
326
+ }
327
+
328
+ for (const w of warnings) console.warn(`⚠️ ${w}`);
329
+ return warnings;
330
+ }
331
+
281
332
  /**
282
333
  * Save as a .deck (ZIP archive).
283
334
  * @param {string} outPath - Output file path
284
335
  * @param {object} opts - { imagesDir, thumbnail, meta }
285
336
  */
286
337
  async saveDeck(outPath, opts = {}) {
338
+ this.validate();
287
339
  const figBuf = await this.encodeFig();
288
340
  const absOut = resolve(outPath);
289
341
 
package/manifest.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "manifest_version": "0.2",
3
3
  "name": "openfig",
4
- "version": "0.3.18",
4
+ "version": "0.3.20",
5
5
  "description": "Open-source tools for Figma file parsing and rendering",
6
6
  "author": {
7
7
  "name": "OpenFig Contributors"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openfig-cli",
3
- "version": "0.3.18",
3
+ "version": "0.3.20",
4
4
  "description": "OpenFig — Open-source tools for Figma file parsing and rendering",
5
5
  "type": "module",
6
6
  "bin": {
@@ -49,6 +49,7 @@
49
49
  "fzstd": "^0.1.1",
50
50
  "kiwi-schema": "^0.5.0",
51
51
  "pako": "^2.1.0",
52
+ "pdf-lib": "^1.17.1",
52
53
  "sharp": "^0.34.5",
53
54
  "ssim.js": "^3.5.0",
54
55
  "yazl": "^3.3.1",