resume/index.js
2024-01-08 17:09:06 -06:00

42 lines
1.0 KiB
JavaScript

import { $ } from 'zx'
import * as Fs from 'fs'
import * as Path from 'path'
import * as Stream from 'stream/promises'
import * as Puppeteer from 'puppeteer'
/** @type {import('puppeteer').PDFOptions} */
const PDF_OPTIONS = {
format: 'letter',
printBackground: true,
margin: { bottom: '0.25in', top: '0.25in', left: '0.25in', right: '0.25in' },
}
const DEBUG = !!process.env['DEBUG']
const root = 'file://' + Path.resolve(import.meta.dir, './dist')
console.log(root)
await $`bun bundle --public-url ${root}`
const pup = await Puppeteer.launch({ headless: DEBUG ? false : 'new' })
const page = await pup.newPage()
await page.goto(root + '/resume.html')
await page.waitForNetworkIdle()
const pdf = await page.createPDFStream(PDF_OPTIONS)
const out = Fs.createWriteStream('./resume.pdf', { flags: 'w+' })
await Stream.pipeline(pdf, out)
if (!DEBUG) await pup.close()
console.log('wrote ./resume.pdf')
process.on('SIGINT', () => {
pup.close()
})
process.on('SIGHUP', () => {
pup.close()
})
process.on('exit', () => {
pup.close()
})