Files
site-portfolio/scripts/generate-resume-pdf.mjs
T

67 lines
2.3 KiB
JavaScript

import puppeteer from 'puppeteer';
import { fileURLToPath } from 'url';
import { dirname, resolve } from 'path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const OUT = resolve(__dirname, '../public/assets/resume/Mamadaev_Nik__CV__Product_Designer_UX-UI.pdf');
const PAGE = process.argv[2] || 'http://localhost:4175/resume/';
const FONT = process.argv[3] || 'medeo';
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
const page = await browser.newPage();
await page.goto(PAGE, { waitUntil: 'networkidle0', timeout: 30000 });
await page.waitForSelector('.resume-doc', { timeout: 5000 });
await page.evaluate((font) => {
localStorage.setItem('siteThemeV1', JSON.stringify({ font }));
document.documentElement.dataset.font = font;
const href = `assets/fonts/${font}.css`;
if (!document.querySelector(`link[data-theme-font="${font}"]`)) {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = href;
link.dataset.themeFont = font;
document.head.appendChild(link);
}
}, FONT);
await page.evaluate(() => document.fonts.ready);
await new Promise(r => setTimeout(r, 1500));
const fontInfo = await page.evaluate(() => ({
dataFont: document.documentElement.dataset.font,
computedFont: getComputedStyle(document.body).fontFamily,
}));
console.log('Font:', fontInfo);
await page.evaluate(() => {
document.querySelectorAll('header.header, footer, nav, .fab-stack, .cookie-notice, .skip-link').forEach(el => el.remove());
const doc = document.querySelector('.resume-doc');
if (doc) {
doc.style.removeProperty('max-width');
doc.style.width = '100%';
doc.style.margin = '0';
doc.style.setProperty('background', 'transparent', 'important');
doc.style.border = 'none';
document.documentElement.style.setProperty('background', 'transparent', 'important');
document.body.style.background = 'transparent';
document.body.innerHTML = '';
document.body.appendChild(doc);
document.body.style.margin = '0';
document.body.style.padding = '0';
}
});
await page.pdf({
path: OUT,
format: 'A4',
printBackground: true,
margin: { top: '10mm', bottom: '10mm', left: '10mm', right: '10mm' }
});
await browser.close();
console.log('PDF saved:', OUT);