Skip to content

AI-native design editor. Open-source Figma alternative.

fix: load svg2pdf.js's ES build so PDF export works under Node.js (#1696)

Victor Shymko63926e813be7
diff --git a/changes/1696/fixed.md b/changes/1696/fixed.md
new file mode 100644
index 000000000..f8a1d7cf6
--- /dev/null
+++ b/changes/1696/fixed.md
@@ -0,0 +1 @@
+- PDF export works in the installed `openpencil` CLI and MCP server, which run on Node.js, instead of failing with "Cannot read properties of undefined (reading 'jsPDF')".
diff --git a/packages/core/src/global.d.ts b/packages/core/src/global.d.ts
index 239b55450..c1f12f221 100644
--- a/packages/core/src/global.d.ts
+++ b/packages/core/src/global.d.ts
@@ -14,3 +14,8 @@ declare module '*?raw' {
const content: string
export default content
}
+
+/** svg2pdf.js declares types for its package root only; its ES build is the same module. */
+declare module 'svg2pdf.js/dist/svg2pdf.es.min.js' {
+ export * from 'svg2pdf.js'
+}
diff --git a/packages/core/src/io/formats/pdf/export.ts b/packages/core/src/io/formats/pdf/export.ts
index 5f42cf178..3cc5e358f 100644
--- a/packages/core/src/io/formats/pdf/export.ts
+++ b/packages/core/src/io/formats/pdf/export.ts
@@ -97,7 +97,13 @@ async function renderPages(
if (prepared.length === 0) return null
const first = prepared[0]
- const [{ jsPDF }, { svg2pdf }] = await Promise.all([import('jspdf'), import('svg2pdf.js')])
+ // svg2pdf.js's `main` is a UMD build in a `"type": "module"` package with no `exports`, so
+ // Node loads it as ESM and it throws for want of a global jsPDF. Its ES build, the `module`
+ // file bundlers and Bun already pick, loads everywhere.
+ const [{ jsPDF }, { svg2pdf }] = await Promise.all([
+ import('jspdf'),
+ import('svg2pdf.js/dist/svg2pdf.es.min.js')
+ ])
const doc = new jsPDF({
orientation: orientationOf(first),
diff --git a/tools/checks/package-quality/src/smoke/scenarios.ts b/tools/checks/package-quality/src/smoke/scenarios.ts
index 8a5cd8c11..670457832 100644
--- a/tools/checks/package-quality/src/smoke/scenarios.ts
+++ b/tools/checks/package-quality/src/smoke/scenarios.ts
@@ -79,5 +79,9 @@ export const runtimeScenarios: RuntimeScenario[] = [
{
name: 'XPath query',
code: "const { SceneGraph } = await import('@open-pencil/scene-graph'); const { FigmaAPI } = await import('@open-pencil/core/figma-api'); const { matchByXPath, queryByXPath } = await import('@open-pencil/core/xpath'); const graph = new SceneGraph(); new FigmaAPI(graph).createRectangle(); const found = await queryByXPath(graph, '//RECTANGLE'); if (found.length !== 1 || !(await matchByXPath(graph, '@width > 0', found[0]))) throw new Error('XPath query smoke failed')"
+ },
+ {
+ name: 'PDF export',
+ code: "const { SceneGraph } = await import('@open-pencil/scene-graph'); const { FigmaAPI } = await import('@open-pencil/core/figma-api'); const { BUILTIN_IO_FORMATS, IORegistry } = await import('@open-pencil/core/io'); const graph = new SceneGraph(); const rectangle = new FigmaAPI(graph).createRectangle(); const result = await new IORegistry(BUILTIN_IO_FORMATS).exportContent('pdf', { graph, target: { scope: 'node', nodeId: rectangle.id } }); if (!(result.data instanceof Uint8Array) || new TextDecoder().decode(result.data.subarray(0, 5)) !== '%PDF-') throw new Error('PDF export smoke failed')"
}
]