|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | + <title>Example: Complex PDF with Embedded WOFF/WOFF2 Fonts</title> |
| 7 | + <link rel="preconnect" href="https://fonts.googleapis.com" /> |
| 8 | + <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /> |
| 9 | + <link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;700&family=IBM+Plex+Mono:wght@400;500&display=swap" rel="stylesheet" /> |
| 10 | + <style> |
| 11 | + :root { |
| 12 | + --bg: #f6f2e8; |
| 13 | + --panel: #fffef9; |
| 14 | + --ink: #132218; |
| 15 | + --muted: #4b5a4f; |
| 16 | + --line: #d8cfba; |
| 17 | + --accent: #9f3f1f; |
| 18 | + } |
| 19 | + * { box-sizing: border-box; } |
| 20 | + body { |
| 21 | + margin: 0; |
| 22 | + font-family: "Space Grotesk", "Segoe UI", sans-serif; |
| 23 | + color: var(--ink); |
| 24 | + background: radial-gradient(circle at 90% -10%, #d9e8de 0, transparent 40%), var(--bg); |
| 25 | + line-height: 1.6; |
| 26 | + } |
| 27 | + .wrap { width: min(980px, 92vw); margin: 0 auto; padding: 34px 0 42px; } |
| 28 | + .crumb { color: var(--muted); text-decoration: none; font-size: 0.95rem; } |
| 29 | + h1 { font-size: clamp(1.8rem, 4vw, 2.8rem); margin: 10px 0 8px; letter-spacing: -0.02em; } |
| 30 | + p { color: var(--muted); } |
| 31 | + .panel { |
| 32 | + background: var(--panel); |
| 33 | + border: 1px solid var(--line); |
| 34 | + border-radius: 14px; |
| 35 | + padding: 18px; |
| 36 | + margin-top: 18px; |
| 37 | + } |
| 38 | + .code { |
| 39 | + font-family: "IBM Plex Mono", Consolas, monospace; |
| 40 | + font-size: 0.9rem; |
| 41 | + white-space: pre; |
| 42 | + overflow-x: auto; |
| 43 | + background: #121916; |
| 44 | + color: #e8efea; |
| 45 | + border-radius: 10px; |
| 46 | + padding: 14px; |
| 47 | + border: 1px solid #25342d; |
| 48 | + } |
| 49 | + .note { |
| 50 | + border-left: 4px solid var(--accent); |
| 51 | + padding-left: 12px; |
| 52 | + margin-top: 14px; |
| 53 | + color: var(--ink); |
| 54 | + } |
| 55 | + ul { margin: 8px 0; } |
| 56 | + </style> |
| 57 | +</head> |
| 58 | +<body> |
| 59 | + <div class="wrap"> |
| 60 | + <a class="crumb" href="../index.html">Back to homepage</a> |
| 61 | + <h1>Complex PDF Export with Embedded WOFF/WOFF2 Fonts</h1> |
| 62 | + <p>This pattern matches the browser addon flow: collect used webfont assets from the live DOM, convert non-TTF font files through fonteditor-core, and embed the result directly into the PDF.</p> |
| 63 | + |
| 64 | + <section class="panel"> |
| 65 | + <h2>Install</h2> |
| 66 | + <div class="code">npm install @node-projects/layout2vector fonteditor-core</div> |
| 67 | + </section> |
| 68 | + |
| 69 | + <section class="panel"> |
| 70 | + <h2>Code</h2> |
| 71 | + <div class="code">import { |
| 72 | + extractIRWithAssets, |
| 73 | + renderIR, |
| 74 | + PDFWriter |
| 75 | +} from "@node-projects/layout2vector"; |
| 76 | + |
| 77 | +async function exportInvoicePdf(root) { |
| 78 | + // includeFonts gathers the @font-face files used by extracted text. |
| 79 | + const { ir, fontAssets } = await extractIRWithAssets(root, { |
| 80 | + includeText: true, |
| 81 | + includeImages: true, |
| 82 | + includeFonts: true, |
| 83 | + convertFormControls: true, |
| 84 | + walkIframes: true |
| 85 | + }); |
| 86 | + |
| 87 | + const writer = new PDFWriter({ |
| 88 | + pageWidth: 210, |
| 89 | + pageHeight: 297, |
| 90 | + fontAssets, |
| 91 | + // Enables WOFF/WOFF2/OTF -> TTF conversion for embedding. |
| 92 | + useFontEditorCore: true |
| 93 | + }); |
| 94 | + |
| 95 | + const pdfDoc = await renderIR(ir, writer); |
| 96 | + await pdfDoc.finalize(); |
| 97 | + const pdfBytes = pdfDoc.toBytes(); |
| 98 | + |
| 99 | + return pdfBytes; |
| 100 | +} |
| 101 | + |
| 102 | +function downloadPdf(bytes, filename = "layout-export.pdf") { |
| 103 | + const blob = new Blob([bytes], { type: "application/pdf" }); |
| 104 | + const url = URL.createObjectURL(blob); |
| 105 | + const a = document.createElement("a"); |
| 106 | + a.href = url; |
| 107 | + a.download = filename; |
| 108 | + a.click(); |
| 109 | + URL.revokeObjectURL(url); |
| 110 | +}</div> |
| 111 | + <p class="note">Important: without <strong>useFontEditorCore: true</strong>, the PDF writer only embeds TTF sources and ignores WOFF/WOFF2/OTF sources.</p> |
| 112 | + </section> |
| 113 | + |
| 114 | + <section class="panel"> |
| 115 | + <h2>Production notes</h2> |
| 116 | + <ul> |
| 117 | + <li>Keep includeFonts enabled only when you need webfont fidelity to reduce network work.</li> |
| 118 | + <li>If the source app uses dynamic @font-face rules, wait for document fonts to load before calling extractIRWithAssets().</li> |
| 119 | + <li>This same approach works in browser extensions where fonts are loaded from page styles.</li> |
| 120 | + </ul> |
| 121 | + </section> |
| 122 | + </div> |
| 123 | +</body> |
| 124 | +</html> |
0 commit comments