import express from 'express';
import cors from 'cors';
import puppeteer from 'puppeteer';
import sanitizeHtml from 'sanitize-html';
import { buildPrintableHtml as sharedBuild } from '../shared-printable/buildPrintableHtml.js';
// Minimal escape function to avoid breaking HTML; summary is sanitized client-side.
function escapeText(str) {
if (str == null) return '';
return String(str)
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/\"/g, '"')
.replace(/'/g, ''');
}
function buildPrintableHtml(cv) {
const { personal = {}, summary = '', work = [], education = [], skills = [], languages = [], certifications = [] } = cv || {};
const styles = `
`;
const headerLinks = (personal.links || [])
.map(l => `${escapeText(l.label || l.url)}`)
.join(' · ');
const headerHtml = `
`;
const cleanSummary = typeof summary === 'string' ? sanitizeHtml(summary, {
allowedTags: ['p', 'strong', 'em', 'ul', 'ol', 'li', 'br', 'a'],
allowedAttributes: { a: ['href', 'rel', 'target'] }
}) : '';
const summarySection = cleanSummary ? `
Professional Summary
${cleanSummary}
` : '';
const workSection = (work && work.length) ? `
Work Experience
${work.map(job => `
${escapeText(job.title)}
${escapeText(job.company)}
${escapeText(job.startDate || '')} - ${escapeText(job.endDate || 'Present')}
${job.location ? `
${escapeText(job.location)}
` : ''}
${(job.bullets && job.bullets.length) ? `
${job.bullets.map(b => `- ${escapeText(b)}
`).join('')}
` : ''}
`).join('')}
` : '';
const eduSection = (education && education.length) ? `
Education
${education.map(ed => `
${escapeText(ed.degree)}
${escapeText(ed.school)}
${escapeText(ed.startDate || '')} - ${escapeText(ed.endDate || '')}
${ed.gpa ? `
GPA: ${escapeText(ed.gpa)}
` : ''}
`).join('')}
` : '';
const skillsSection = (skills && skills.length) ? `
Skills
${skills.map(s => `${escapeText(s.name)}${s.level ? ` — ${escapeText(s.level)}` : ''}`).join('')}
` : '';
const languagesSection = (languages && languages.length) ? `
Languages
${languages.map(l => `${escapeText(l.name)}${l.level ? ` — ${escapeText(l.level)}` : ''}`).join('')}
` : '';
const certsSection = (certifications && certifications.length) ? `
Certifications
${certifications.map(c => `- ${escapeText(c.name)}${c.issuer ? ` - ${escapeText(c.issuer)}` : ''}${c.date ? ` (${escapeText(c.date)})` : ''}
`).join('')}
` : '';
return `
Printable CV
${styles}
${headerHtml}
${summarySection}
${workSection}
${eduSection}
${skillsSection}
${languagesSection}
${certsSection}
`;
}
const app = express();
app.use(cors());
app.use(express.json({ limit: '1mb' }));
// Pluggable job store with in-memory default
class InMemoryJobStore {
constructor() {
this.map = new Map();
}
create(job) {
const id = `job_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
this.map.set(id, { ...job, id });
return id;
}
get(id) { return this.map.get(id); }
set(id, data) { this.map.set(id, { ...(this.map.get(id) || {}), ...data }); }
cancel(id) {
const job = this.map.get(id);
if (!job) return false;
this.map.set(id, { ...job, status: 'canceled', error: null, buffer: null });
return true;
}
}
const jobs = new InMemoryJobStore();
app.post('/export/pdf', async (req, res) => {
try {
// Treat presence of the query param as async, and allow common truthy values
const asyncFlag = (
typeof req.query.async !== 'undefined'
? ['1', 'true', 'on', ''].includes(String(req.query.async).toLowerCase())
: req.body?.async === true
);
const templateId = req.query.templateId || req.body?.templateId || null;
const cv = req.body || {};
const sanitizedCv = {
...cv,
summary: typeof cv.summary === 'string' ? sanitizeHtml(cv.summary, {
allowedTags: ['p', 'strong', 'em', 'ul', 'ol', 'li', 'br', 'a'],
allowedAttributes: { a: ['href', 'rel', 'target'] }
}) : ''
};
const firstName = (sanitizedCv.personal && sanitizedCv.personal.firstName) ? sanitizedCv.personal.firstName : 'Candidate';
const lastName = (sanitizedCv.personal && sanitizedCv.personal.lastName) ? sanitizedCv.personal.lastName : 'CV';
const filename = `${firstName}_${lastName}_CV.pdf`.replace(/\s+/g, '_');
if (asyncFlag) {
const jobId = jobs.create({ status: 'queued', filename, error: null, buffer: null, templateId });
setImmediate(async () => {
try {
const current = jobs.get(jobId);
if (current?.status === 'canceled') return; // honor cancel
const html = sharedBuild(sanitizedCv);
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
const page = await browser.newPage();
await page.setContent(html, { waitUntil: 'networkidle0' });
const pdfBuffer = await page.pdf({ format: 'A4', printBackground: true });
await browser.close();
jobs.set(jobId, { status: 'done', filename, error: null, buffer: pdfBuffer, templateId });
} catch (e) {
console.error('Async export error:', e);
jobs.set(jobId, { status: 'error', filename, error: 'Failed to create PDF', buffer: null, templateId });
}
});
return res.json({ jobId });
}
// Synchronous path
const html = sharedBuild(sanitizedCv);
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
const page = await browser.newPage();
await page.setContent(html, { waitUntil: 'networkidle0' });
const pdfBuffer = await page.pdf({ format: 'A4', printBackground: true });
await browser.close();
res.set({
'Content-Type': 'application/pdf',
'Content-Disposition': `attachment; filename="${filename}"`
});
res.send(pdfBuffer);
} catch (err) {
console.error('Export error:', err);
res.status(500).json({ error: 'Failed to create PDF' });
}
});
// Job status endpoint
app.get('/export/status/:id', (req, res) => {
const job = jobs.get(req.params.id);
if (!job) return res.status(404).json({ error: 'Job not found' });
res.json({ status: job.status, filename: job.filename, error: job.error });
});
// Job download endpoint
app.get('/export/download/:id', (req, res) => {
const job = jobs.get(req.params.id);
if (!job) return res.status(404).json({ error: 'Job not found' });
if (job.status !== 'done' || !job.buffer) return res.status(409).json({ error: 'Job not ready' });
res.set({
'Content-Type': 'application/pdf',
'Content-Disposition': `attachment; filename="${job.filename}"`
});
res.send(job.buffer);
});
// Cancel job endpoint
app.post('/export/cancel/:id', (req, res) => {
const id = req.params.id;
const ok = jobs.cancel(id);
if (!ok) return res.status(404).json({ error: 'Job not found' });
return res.json({ status: 'canceled' });
});
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Export server listening on http://localhost:${PORT}`);
});