-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path+server.ts
More file actions
38 lines (33 loc) · 1.13 KB
/
+server.ts
File metadata and controls
38 lines (33 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { launch } from 'puppeteer';
// Explicitly add global styles since +layout.svelte is not applied to server-side components
import globalStyles from '$lib/styles/global.scss?inline';
import ResumePrint from './ResumePrint.svelte';
// SvelteKit doesn't export types for server-side components API, need to define it myself
type ServerSideComponent = {
render(): { html: string; head: string; css: { code: string } };
};
/** @type {import('./$types').RequestHandler} */
export async function GET() {
const resumeRender = (ResumePrint as unknown as ServerSideComponent).render();
const browser = await launch({ headless: true });
const page = await browser.newPage();
await page.setContent(
`<html>
<head>
${resumeRender.head}
<style type='text/css'>
${globalStyles}
${resumeRender.css.code}
</style>
</head>
<body>
${resumeRender.html}
</body>
</html>`,
{ waitUntil: 'networkidle0' }
);
const pdf = await page.pdf({ format: 'A4' });
return new Response(pdf, { status: 200 });
}
// Enable prerendering of server endpoints for deploying with adapter-static
export const prerender = true;