|
| 1 | +#!/usr/bin/env bun |
| 2 | +import { chromium } from "playwright"; |
| 3 | +import { spawn } from "child_process"; |
| 4 | +import { join } from "path"; |
| 5 | + |
| 6 | +const chromeExecutablePath = |
| 7 | + "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; // macOS path |
| 8 | + |
| 9 | +const args = Bun.argv.slice(2); |
| 10 | + |
| 11 | +let url: string | undefined; |
| 12 | +let nodeSelector: string | undefined; |
| 13 | +let outputFile = "output.pdf"; |
| 14 | +let margin = "1in"; // default |
| 15 | + |
| 16 | +for (let i = 0; i < args.length; i++) { |
| 17 | + if (args[i] === "--node" || args[i] === "-n") { |
| 18 | + nodeSelector = args[i + 1]; |
| 19 | + i++; |
| 20 | + } else if (args[i] === "--output" || args[i] === "-o") { |
| 21 | + outputFile = args[i + 1]; |
| 22 | + i++; |
| 23 | + } else if (args[i] === "--margin" || args[i] === "-m") { |
| 24 | + margin = args[i + 1]; |
| 25 | + i++; |
| 26 | + } else if (!url) { |
| 27 | + url = args[i]; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +if (!url || !nodeSelector) { |
| 32 | + console.error( |
| 33 | + "Usage: bun exportPage.ts <url> --node <CSS selector> [--output file.pdf] [--margin 1in]" |
| 34 | + ); |
| 35 | + process.exit(1); |
| 36 | +} |
| 37 | + |
| 38 | +const browser = await chromium.launch({ |
| 39 | + headless: true, |
| 40 | + executablePath: chromeExecutablePath, |
| 41 | + args: [ |
| 42 | + "--remote-debugging-port=9222", |
| 43 | + "--headless=true", |
| 44 | + "--allow-file-access-from-files", |
| 45 | + "--autoplay-policy=user-gesture-required", |
| 46 | + "--disable-background-networking", |
| 47 | + "--disable-background-timer-throttling", |
| 48 | + "--disable-backgrounding-occluded-windows", |
| 49 | + "--disable-breakpad", |
| 50 | + "--disable-client-side-phishing-detection", |
| 51 | + "--disable-component-update", |
| 52 | + "--disable-default-apps", |
| 53 | + "--disable-dev-shm-usage", |
| 54 | + "--disable-domain-reliability", |
| 55 | + "--disable-extensions", |
| 56 | + "--disable-features=AudioServiceOutOfProcess", |
| 57 | + "--disable-hang-monitor", |
| 58 | + "--disable-ipc-flooding-protection", |
| 59 | + "--disable-notifications", |
| 60 | + "--disable-offer-store-unmasked-wallet-cards", |
| 61 | + "--disable-popup-blocking", |
| 62 | + "--disable-print-preview", |
| 63 | + "--disable-prompt-on-repost", |
| 64 | + "--disable-renderer-backgrounding", |
| 65 | + "--disable-setuid-sandbox", |
| 66 | + "--disable-speech-api", |
| 67 | + "--disable-sync", |
| 68 | + "--hide-scrollbars", |
| 69 | + "--ignore-gpu-blacklist", |
| 70 | + "--metrics-recording-only", |
| 71 | + "--mute-audio", |
| 72 | + "--no-default-browser-check", |
| 73 | + "--no-first-run", |
| 74 | + "--no-pings", |
| 75 | + "--no-sandbox", |
| 76 | + "--no-zygote", |
| 77 | + "--password-store=basic", |
| 78 | + "--use-gl=swiftshader", |
| 79 | + "--use-mock-keychain", |
| 80 | + ], |
| 81 | +}); |
| 82 | + |
| 83 | +const page = await browser.newPage(); |
| 84 | + |
| 85 | +await page.goto(url, { waitUntil: "networkidle" }); |
| 86 | + |
| 87 | +const success = await page.evaluate((selector) => { |
| 88 | + const target = document.querySelector(selector); |
| 89 | + if (!target) return false; |
| 90 | + |
| 91 | + const clone = target.cloneNode(true) as HTMLElement; |
| 92 | + |
| 93 | + const styles = [...document.styleSheets] |
| 94 | + .map((sheet) => { |
| 95 | + try { |
| 96 | + return [...(sheet.cssRules ?? [])] |
| 97 | + .map((rule) => rule.cssText) |
| 98 | + .join("\n"); |
| 99 | + } catch { |
| 100 | + return ""; |
| 101 | + } |
| 102 | + }) |
| 103 | + .join("\n"); |
| 104 | + |
| 105 | + document.head.innerHTML = `<style> |
| 106 | + ${styles} |
| 107 | + html, body { |
| 108 | + margin: 0 !important; |
| 109 | + padding: 0 !important; |
| 110 | + border: 0 !important; |
| 111 | + } |
| 112 | + </style>`; |
| 113 | + |
| 114 | + document.body.innerHTML = ""; |
| 115 | + document.body.appendChild(clone); |
| 116 | + |
| 117 | + return true; |
| 118 | +}, nodeSelector); |
| 119 | + |
| 120 | +if (!success) { |
| 121 | + console.error(`Could not find node matching selector: ${nodeSelector}`); |
| 122 | + await browser.close(); |
| 123 | + process.exit(1); |
| 124 | +} |
| 125 | + |
| 126 | +await page.pdf({ |
| 127 | + path: outputFile, |
| 128 | + format: "A4", |
| 129 | + printBackground: false, |
| 130 | + displayHeaderFooter: true, |
| 131 | + footerTemplate: ` |
| 132 | + <div style=" |
| 133 | + font-size: 11px; |
| 134 | + font-family: Arial, Helvetica, sans-serif; |
| 135 | + width: 100%; |
| 136 | + text-align: center; |
| 137 | + color: #ccc; |
| 138 | + margin: 0 auto; |
| 139 | + "> |
| 140 | + <span class="pageNumber"></span>/<span class="totalPages"></span> |
| 141 | + </div> |
| 142 | + `, |
| 143 | + headerTemplate: `<div></div>`, // optional: empty header |
| 144 | + margin: { |
| 145 | + top: margin, |
| 146 | + right: margin, |
| 147 | + bottom: margin, |
| 148 | + left: margin, |
| 149 | + }, |
| 150 | +}); |
| 151 | + |
| 152 | +await browser.close(); |
| 153 | +console.log(`Saved as ${outputFile}`); |
| 154 | + |
| 155 | +// Open PDF in system viewer |
| 156 | +function openPDF(path: string) { |
| 157 | + const platform = process.platform; |
| 158 | + if (platform === "darwin") { |
| 159 | + spawn("open", [path], { stdio: "inherit" }); |
| 160 | + } else if (platform === "win32") { |
| 161 | + spawn("start", [path], { shell: true, stdio: "inherit" }); |
| 162 | + } else { |
| 163 | + spawn("xdg-open", [path], { stdio: "inherit" }); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +openPDF(join(process.cwd(), outputFile)); |
0 commit comments