Skip to content
RPRichard PruittTech & Cyber
Navigation
repo://poppyup/wiring/render.mjs

wiring/render.mjs

2.5 KB · 39 lines · javascript · Download from the repository page

wiring/render.mjs
// Render wiring SVG sheets to PNG previews and one combined A3-landscape PDF with headless Chrome.// Usage: PUPPETEER_DIR=/tmp/pu3d-tools node wiring/render.mjs [101 102 ...]   (no args = all sheets)import { createRequire } from 'node:module';import fs from 'node:fs';import path from 'node:path';const here = path.dirname(new URL(import.meta.url).pathname);const PUP = process.env.PUPPETEER_DIR || '/tmp/pu3d-tools';const require = createRequire(path.join(PUP, 'package.json'));const puppeteer = require('puppeteer-core');const CHROME = process.env.CHROME || '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';const sheetsDir = path.join(here, 'sheets'), prevDir = path.join(here, 'preview');fs.mkdirSync(prevDir, { recursive: true });let files = fs.readdirSync(sheetsDir).filter(f => f.endsWith('.svg')).sort();const only = process.argv.slice(2);if (only.length) files = files.filter(f => only.some(n => f.includes(`-${n}.svg`)));const browser = await puppeteer.launch({ executablePath: CHROME, headless: true, args: ['--no-sandbox', '--allow-file-access-from-files'] });try {  const page = await browser.newPage();  await page.setViewport({ width: 1587, height: 1123, deviceScaleFactor: 1.4 });  for (const f of files) {    await page.goto('file://' + path.join(sheetsDir, f), { waitUntil: 'load' });    await page.screenshot({ path: path.join(prevDir, f.replace('.svg', '.png')), clip: { x: 0, y: 0, width: 1587, height: 1123 } });    console.log('PNG', f);  }  if (!only.length) {    const all = fs.readdirSync(sheetsDir).filter(f => f.endsWith('.svg')).sort();    const html = `<!doctype html><html><head><style>@page{size:420mm 297mm;margin:0}body{margin:0}      .p{width:420mm;height:297mm;page-break-after:always;display:block}.p:last-child{page-break-after:auto}img{width:420mm;height:297mm;display:block}</style></head><body>` +      all.map(f => `<div class="p"><img src="file://${path.join(sheetsDir, f)}"></div>`).join('') + `</body></html>`;    const tmp = path.join(prevDir, '_set.html'); fs.writeFileSync(tmp, html);    await page.goto('file://' + tmp, { waitUntil: 'load' });    const pdf = path.join(here, `PU_Wiring-Set_Rev-A.pdf`);    await page.pdf({ path: pdf, width: '420mm', height: '297mm', printBackground: true, margin: { top: 0, right: 0, bottom: 0, left: 0 } });    fs.unlinkSync(tmp);    const n = (fs.readFileSync(pdf, 'latin1').match(/\/Type\s*\/Page[^s]/g) || []).length;    console.log('PDF', pdf, 'pages', n, 'sheets', all.length);  }} finally { await browser.close(); }