Spaces:
Running
Running
Download src/api/pdf.js from Luca448/APP-Backend: direct link, hf CLI and curl.
- Browser
- Download file 2.57 kB
-
https://huggingface.co/spaces/Luca448/APP-Backend/resolve/main/src/api/pdf.js
- Command line
-
hf download hf://spaces/Luca448/APP-Backend/src/api/pdf.js
-
curl -L -o pdf.js https://huggingface.co/spaces/Luca448/APP-Backend/resolve/main/src/api/pdf.js
2.57 kB
| 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(); | |
| } | |
| } | |
| } | |