Skip to content

Commit

Permalink
Rebuild CV PDFs
Browse files Browse the repository at this point in the history
  • Loading branch information
dmythro committed Nov 26, 2024
1 parent 0ce484d commit 520dea4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 31 deletions.
Binary file modified apps/www/public/cv.en.pdf
Binary file not shown.
Binary file modified apps/www/public/cv.uk.pdf
Binary file not shown.
82 changes: 51 additions & 31 deletions packages/cv-to-pdf/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,49 +62,69 @@ async function start() {

staticServer.stdout?.on('data', async (data: string) => {
if (`${data}`.includes(`://${host}:${port}`)) {
return generatePdfs()
try {
await generatePdfs()
} catch (err) {
done(`${err}`)
}
}
})

staticServer.stderr?.on('data', done)
staticServer.on('error', done)

async function generatePdfs() {
for (const lang of locales) {
console.info(` - open "${lang}" CV page`)
const page = await browser.newPage()
await page.goto(`http://${host}:${port}/${lang}/cv`, { waitUntil: 'networkidle0' })

// Load all non-priority <Image />
await page.evaluate(() => {
window.scrollTo({ left: 0, top: window.document.body.scrollHeight, behavior: 'smooth' })
})
await page.waitForNetworkIdle()

const pdf = await page.pdf({
format: 'A4',
margin: {
top: '0.5cm',
bottom: '0.5cm',
left: '0.5cm',
right: '0.5cm',
},
printBackground: true,
})
const pdfPath = path.resolve(__dirname, `../../apps/www/public/cv.${lang}${pdfExt}`)
console.info(` - save "${pdfPath}"`)
fs.writeFileSync(pdfPath, pdf)
await runGhostScript(pdfPath)
let step = 'starting'
try {
for (const lang of locales) {
console.info(` - open "${lang}" CV page`)

step = 'opening page'
const page = await browser.newPage()
await page.goto(`http://${host}:${port}/${lang}/cv`, { waitUntil: 'networkidle0' })

step = 'loading all non-priority <Image />'
await page.evaluate(() => {
window.scrollTo({ left: 0, top: window.document.body.scrollHeight, behavior: 'smooth' })
})
await page.waitForNetworkIdle()

step = 'saving PDF'
const pdf = await page.pdf({
format: 'A4',
margin: {
top: '0.5cm',
bottom: '0.5cm',
left: '0.5cm',
right: '0.5cm',
},
printBackground: true,
})
const pdfPath = path.resolve(__dirname, `../../apps/www/public/cv.${lang}${pdfExt}`)
console.info(` - save "${pdfPath}"`)
fs.writeFileSync(pdfPath, pdf)
await runGhostScript(pdfPath)
}

staticServer.removeAllListeners()

return done()
} catch (error) {
return done(`Error ${step}: ${error}`)
}

staticServer.removeAllListeners()

return done()
}

async function done(error?: string) {
const message = `${error}`

// Ignore warnings
if (message.includes('Warning') || message.includes('⚠')) {
console.warn(`Warn: ${message}`)
return
}

if (error) {
console.error(error)
console.error(`Fail: ${error}`)
reject()
} else {
console.info('Done.')
Expand Down

0 comments on commit 520dea4

Please sign in to comment.