|
1 | 1 | import { renderToStaticMarkup } from 'react-dom/server';
|
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
2 | 4 |
|
3 | 5 | const isProd = process.env.NODE_ENV !== "development";
|
4 | 6 |
|
5 | 7 | export const componentToPDFBuffer = async (component) => {
|
6 |
| - const html = renderToStaticMarkup(component); |
| 8 | + let html = renderToStaticMarkup(component); |
| 9 | + |
| 10 | + const globalCSS = fs.readFileSync(path.resolve(process.cwd(), "styles", "globals.css"), 'utf-8'); |
| 11 | + const latoLight = fs.readFileSync(path.resolve(process.cwd(), "fonts", "Lato", "Lato-Light.ttf")).toString("base64"); |
| 12 | + const latoRegular = fs.readFileSync(path.resolve(process.cwd(), "fonts", "Lato", "Lato-Regular.ttf")).toString("base64"); |
| 13 | + const latoBold = fs.readFileSync(path.resolve(process.cwd(), "fonts", "Lato", "Lato-Bold.ttf")).toString("base64"); |
| 14 | + const mrsSaint = fs.readFileSync(path.resolve(process.cwd(), "fonts", "MrsSaintDelafield", "MrsSaintDelafield-Regular.ttf")).toString("base64"); |
| 15 | + |
| 16 | + const styles = ` |
| 17 | + <style> |
| 18 | + ${globalCSS} |
| 19 | + @font-face { |
| 20 | + font-family: 'Lato'; |
| 21 | + font-style: normal; |
| 22 | + font-weight: 300; |
| 23 | + src: url(data:font/truetype;charset=utf-8;base64,${latoLight}) format('truetype'); |
| 24 | + } |
| 25 | + @font-face { |
| 26 | + font-family: 'Lato'; |
| 27 | + font-style: normal; |
| 28 | + font-weight: 400; |
| 29 | + src: url(data:font/truetype;charset=utf-8;base64,${latoRegular}) format('truetype'); |
| 30 | + } |
| 31 | + @font-face { |
| 32 | + font-family: 'Lato'; |
| 33 | + font-style: normal; |
| 34 | + font-weight: 700; |
| 35 | + src: url(data:font/truetype;charset=utf-8;base64,${latoBold}) format('truetype'); |
| 36 | + } |
| 37 | + @font-face { |
| 38 | + font-family: 'Mrs Saint Delafield'; |
| 39 | + font-style: normal; |
| 40 | + font-weight: 400; |
| 41 | + src: url(data:font/truetype;charset=utf-8;base64,${mrsSaint}) format('truetype'); |
| 42 | + } |
| 43 | + </style> |
| 44 | + `; |
| 45 | + |
| 46 | + html = html.replace('</head>', `${styles}</head>`); |
7 | 47 |
|
8 | 48 | let browser;
|
9 | 49 | if (isProd) {
|
|
0 commit comments