# Svelte Integration Example
This document shows how to use the CV generation library in a Svelte component.
## Basic Preview Component
```svelte
Resume (JSON5)
{#if error}
{error}
{/if}
Preview
```
## Advanced Component with Download
```svelte
```
## Server Endpoint Example (SvelteKit)
Create `src/routes/api/generate-pdf/+server.js`:
```javascript
import { json } from '@sveltejs/kit';
import { generatePdfFile } from '$lib/generate-cv.js';
export async function POST({ request }) {
try {
const { html } = await request.json();
if (!html) {
return json({ error: 'HTML content required' }, { status: 400 });
}
// Generate PDF to a temporary file or buffer
// For now, we'll use a simple approach with file system
import { tmpdir } from 'os';
import { join } from 'path';
import { randomBytes } from 'crypto';
import fs from 'fs/promises';
const tmpFile = join(tmpdir(), `resume-${randomBytes(8).toString('hex')}.pdf`);
const result = await generatePdfFile(html, tmpFile);
if (!result.success) {
return json({ error: result.error }, { status: 500 });
}
// Read the PDF file
const pdfBuffer = await fs.readFile(tmpFile);
// Clean up
await fs.unlink(tmpFile);
return new Response(pdfBuffer, {
headers: {
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment; filename="resume.pdf"'
}
});
} catch (error) {
console.error('PDF generation error:', error);
return json({ error: error.message }, { status: 500 });
}
}
```
## Configuration
### Vite Config
Add support for loading files as raw text:
```javascript
// vite.config.js
export default {
// ... other config
ssr: {
external: ['playwright'] // Don't bundle Playwright in SSR
}
};
```
### Package Imports
```javascript
// src/lib/index.js (optional barrel export)
export { generatePreview, generateCv } from './generate-cv.js';
export {
loadResumeFromString,
mergeWithDefaults,
renderTemplate
} from './core/index.js';
```
## Tips for Production
1. **Template Caching**: Cache loaded templates in memory or store
2. **Error Boundaries**: Wrap components in error boundaries
3. **Validation**: Validate JSON5 input before parsing
4. **Rate Limiting**: Add rate limiting to PDF generation endpoint
5. **Temporary Files**: Clean up temporary PDF files regularly
6. **Memory Management**: Close browser instances properly
Example with stores:
```javascript
// src/lib/stores.js
import { writable } from 'svelte/store';
export const template = writable('');
export const css = writable('');
export const resume = writable('{}');
// Load templates on app init
import { browser } from '$app/environment';
if (browser) {
Promise.all([
fetch('/templates/default/html.hbs').then(r => r.text()),
fetch('/templates/default/style.css').then(r => r.text())
]).then(([t, c]) => {
template.set(t);
css.set(c);
});
}
```
Then in your component:
```svelte
{#if $template && $css}
{:else}
Loading...
{/if}
```