fix: severless font

This commit is contained in:
JOYCEQL
2024-11-25 23:29:10 +08:00
parent 76f3e70d7c
commit 2ebcb6e60a
3 changed files with 18 additions and 13 deletions
Binary file not shown.
+17 -2
View File
@@ -1,7 +1,9 @@
// import puppeteer from "puppeteer";
import puppeteer from "puppeteer";
import { NextResponse } from "next/server";
import puppeteer from "puppeteer-core";
// import puppeteer from "puppeteer-core";
import chrome from "@sparticuz/chromium";
import path from "path";
import fs from "fs";
export async function POST(req: Request) {
try {
@@ -26,6 +28,19 @@ export async function POST(req: Request) {
});
const page = await browser.newPage();
const fontPath = path.join(
process.cwd(),
"public",
"fonts",
"NotoSansSC.ttf"
);
const fontBuffer = fs.readFileSync(fontPath);
await page.evaluate(async (fontBuffer) => {
const font = new FontFace("Noto Sans SC", fontBuffer);
await font.load();
document.fonts.add(font);
}, fontBuffer);
await page.setContent(content);
+1 -11
View File
@@ -37,17 +37,6 @@ export function PdfExport() {
const content = `
<html>
<head>
<style>
@font-face {
font-family: 'Noto Sans SC';
src: url('https://fonts.googleapis.com/css2?family=Noto+Sans+SC&display=swap') format('woff2');
font-weight: normal;
font-style: normal;
}
body {
font-family: 'Noto Sans SC', sans-serif;
}
</style>
<style>${styles}</style>
</head>
<body>
@@ -55,6 +44,7 @@ export function PdfExport() {
</body>
</html>
`;
const response = await fetch("/api/generate-pdf", {
method: "POST",
headers: {