import MarkdownIt from "markdown-it"; function resolveChromiumPath(): string { const chromiumPath = Deno.env.get("CHROMIUM_PATH"); if (chromiumPath) { return chromiumPath; } throw new Error( [ "CHROMIUM_PATH is not set.", "Use the provided flake shell or set CHROMIUM_PATH manually.", 'Example: export CHROMIUM_PATH="$(command -v chromium)"', ].join("\n"), ); } export async function mdToPdf( markdownContent: string, outputPath = "output.pdf", ): Promise { const { chromium } = await import("playwright"); const md = new MarkdownIt(); const htmlContent = md.render(markdownContent); const browser = await chromium.launch({ executablePath: resolveChromiumPath(), headless: true, args: ["--no-sandbox", "--disable-dev-shm-usage"], }); try { const page = await browser.newPage(); const fullHtml = ` ${htmlContent} `; await page.setContent(fullHtml); await page.pdf({ path: outputPath, format: "A4", printBackground: true, margin: { top: "20mm", bottom: "20mm", left: "10mm", right: "10mm" }, }); } finally { await browser.close(); } } if (import.meta.main) { await mdToPdf("# Test Title\n\nHello **PDF**", "output.pdf"); console.log("PDF generated: output.pdf"); }