Files
Lebenslauf-App/src/pages/api/pdf/[hash].ts
betalabor.de aa4057fd9d layout
2026-04-27 19:29:23 +02:00

57 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { APIRoute } from 'astro';
import { getCVByHash, getProfileById } from '../../../lib/db';
// Öffentlicher Endpunkt kein Login nötig, CV muss public=1 sein
export const GET: APIRoute = async ({ params, request }) => {
const cv = getCVByHash(params.hash!);
if (!cv) return new Response('Not Found', { status: 404 });
const profile = getProfileById(cv.profile_id);
if (!profile) return new Response('Profile Not Found', { status: 404 });
const name = [profile.data.personal.firstName, profile.data.personal.lastName]
.filter(Boolean).join(' ') || cv.title;
const origin = new URL(request.url).origin;
const targetUrl = `${origin}/cv/${cv.hash}`;
try {
const { default: puppeteer } = await import('puppeteer');
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
});
const page = await browser.newPage();
await page.setViewport({ width: 794, height: 1123 });
await page.goto(targetUrl, { waitUntil: 'networkidle0', timeout: 20000 });
await page.addStyleTag({
content: `
.cv-public-bar { display: none !important; }
body { background: white !important; padding: 0 !important; }
.cv-public-wrap { padding: 0 !important; background: white !important; }
`
});
const pdf = await page.pdf({
format: 'A4',
margin: { top: '0mm', right: '0mm', bottom: '0mm', left: '0mm' },
printBackground: true,
displayHeaderFooter: false,
});
await browser.close();
return new Response(pdf, {
headers: {
'Content-Type': 'application/pdf',
'Content-Disposition': `attachment; filename="${encodeURIComponent(name)}.pdf"`,
},
});
} catch (err: any) {
console.error('[PDF Hash]', err.message);
return new Response(err.message, { status: 500 });
}
};