Skip to content

Commit 5186ac0

Browse files
walmikclaude
andcommitted
Add PDF export via browser print dialog
Opens the patch SVG in a new tab and triggers window.print() so the user can save as PDF from the browser's native print dialog. Button added to the toolbar alongside the existing SVG and PNG exports. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 2015b52 commit 5186ac0

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

components/PatchEditor.jsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,23 @@ export default function PatchEditor() {
630630
}
631631
}
632632

633+
async function handleExportPdf() {
634+
try {
635+
const svg = await buildPatchSvgMarkup();
636+
const html = `<!DOCTYPE html><html><head><style>@page{size:auto;margin:0}body{margin:0;background:#fff}img{display:block;max-width:100%}</style></head><body><img src="data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}" /></body></html>`;
637+
const win = window.open("", "_blank");
638+
if (!win) {
639+
window.alert("Could not open print window. Please allow pop-ups for this site.");
640+
return;
641+
}
642+
win.document.write(html);
643+
win.document.close();
644+
win.onload = () => win.print();
645+
} catch {
646+
window.alert("Could not export PDF.");
647+
}
648+
}
649+
633650
function normalizePatchState(state) {
634651
return {
635652
nodes: Array.isArray(state.nodes) ? state.nodes.map(normalizeNode) : [],
@@ -1981,6 +1998,40 @@ export default function PatchEditor() {
19811998
/>
19821999
</ToolbarIcon>
19832000
</button>
2001+
<button
2002+
className="toolbar-icon-button"
2003+
onClick={handleExportPdf}
2004+
type="button"
2005+
title="Export PDF"
2006+
aria-label="Export PDF"
2007+
>
2008+
<ToolbarIcon>
2009+
<path
2010+
d="M8 4h6l4 4v12H8z"
2011+
fill="none"
2012+
stroke="currentColor"
2013+
strokeWidth="2"
2014+
strokeLinejoin="round"
2015+
/>
2016+
<path
2017+
d="M14 4v4h4"
2018+
fill="none"
2019+
stroke="currentColor"
2020+
strokeWidth="2"
2021+
strokeLinecap="round"
2022+
strokeLinejoin="round"
2023+
/>
2024+
<text
2025+
x="12"
2026+
y="16"
2027+
textAnchor="middle"
2028+
fontSize="5.5"
2029+
fontWeight="700"
2030+
fontFamily="sans-serif"
2031+
fill="currentColor"
2032+
>PDF</text>
2033+
</ToolbarIcon>
2034+
</button>
19842035
<button
19852036
className="toolbar-icon-button"
19862037
onClick={handleExportPng}

0 commit comments

Comments
 (0)