Spaces:
Running
Running
File size: 2,572 Bytes
ff36e71 | 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | import puppeteer from 'puppeteer';
import { marked } from 'marked';
export async function generatePdf(markdownContent, title = "SchoolMind Lösung") {
const htmlContent = marked.parse(markdownContent);
const fullHtml = `
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>${title}</title>
<style>
body {
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 11pt;
line-height: 1.6;
color: #333;
margin: 40px;
}
h1, h2, h3 {
color: #1a1a1a;
margin-top: 1.5em;
margin-bottom: 0.5em;
}
h1 { font-size: 24px; border-bottom: 2px solid #eaeaea; padding-bottom: 10px; }
h2 { font-size: 20px; }
h3 { font-size: 16px; }
p { margin-bottom: 15px; }
code {
background-color: #f4f4f4;
padding: 2px 4px;
border-radius: 4px;
font-family: 'Courier New', Courier, monospace;
}
pre {
background-color: #f4f4f4;
padding: 15px;
border-radius: 8px;
overflow-x: auto;
}
pre code {
background-color: transparent;
padding: 0;
}
blockquote {
border-left: 4px solid #8a53e6;
margin: 0;
padding-left: 15px;
color: #555;
font-style: italic;
}
ul, ol { margin-bottom: 15px; }
li { margin-bottom: 5px; }
/* Prevent awkward page breaks */
p, li, h1, h2, h3, pre, blockquote {
page-break-inside: avoid;
break-inside: avoid;
}
h1, h2, h3 {
page-break-after: avoid;
break-after: avoid;
}
</style>
</head>
<body>
${htmlContent}
</body>
</html>
`;
let browser;
try {
browser = await puppeteer.launch({
headless: "new",
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
await page.setContent(fullHtml, { waitUntil: 'networkidle0' });
const pdfBuffer = await page.pdf({
format: 'A4',
margin: {
top: '40px',
right: '40px',
bottom: '40px',
left: '40px'
},
printBackground: true,
displayHeaderFooter: false
});
return pdfBuffer;
} catch (e) {
console.error("Fehler beim PDF Generieren:", e);
throw e;
} finally {
if (browser) {
await browser.close();
}
}
}
|