test: export pdf

This commit is contained in:
JOYCEQL
2024-11-26 12:26:07 +08:00
parent 006de4210d
commit 3bc671c4e6
8 changed files with 59 additions and 32 deletions
+1
View File
@@ -33,6 +33,7 @@
"@tiptap/pm": "^2.4.0",
"@tiptap/react": "^2.4.0",
"@tiptap/starter-kit": "^2.4.0",
"chrome-aws-lambda": "^10.1.0",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"cmdk": "1.0.0",
+7 -20
View File
@@ -1,9 +1,7 @@
// import puppeteer from "puppeteer";
import { NextResponse } from "next/server";
import puppeteer from "puppeteer-core";
import chrome from "@sparticuz/chromium";
import path from "path";
import fs from "fs";
import chrome from "chrome-aws-lambda";
export async function POST(req: Request) {
try {
@@ -21,36 +19,25 @@ export async function POST(req: Request) {
"--font-render-hinting=none"
],
// defaultViewport: chrome.defaultViewport,
executablePath: await chrome.executablePath(
`https://github.com/Sparticuz/chromium/releases/download/v126.0.0/chromium-v126.0.0-pack.tar`
),
// executablePath: await chrome.executablePath(
// `https://github.com/Sparticuz/chromium/releases/download/v126.0.0/chromium-v126.0.0-pack.tar`
// ),
executablePath: await chrome.executablePath,
headless: chrome.headless
});
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);
await page.evaluate(() => document.fonts.ready);
await page.waitForFunction("document.fonts.ready");
const marginPx = margin + "px";
const pdf = await page.pdf({
format: "A4",
printBackground: true,
waitForFonts: true,
margin: {
top: marginPx,
right: marginPx,
@@ -37,6 +37,15 @@ 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');
}
body {
font-family: 'Noto Sans SC', sans-serif;
}
</style>
<style>${styles}</style>
</head>
<body>
@@ -30,7 +30,7 @@ import { useMemo } from "react";
import { Switch } from "../ui/switch";
const fontOptions = [
{ value: "Noto Sans SC", label: "无衬线体" },
{ value: "sans", label: "无衬线体" },
{ value: "serif", label: "衬线体" },
{ value: "mono", label: "等宽体" }
];
@@ -264,12 +264,11 @@ export function PreviewPanel() {
"bg-white",
"shadow-lg",
"relative mx-auto",
// fontFamilyClass,
fontFamilyClass,
"text-[#000]"
)}
style={{
minHeight: "297mm",
fontFamily: "Noto Sans SC"
minHeight: "297mm"
}}
>
<div
+1 -1
View File
@@ -225,7 +225,7 @@ const initialState = {
activeSection: "basic",
globalSettings: {
fontFamily: "Noto Sans SC",
fontFamily: "sans",
baseFontSize: 14,
pagePadding: 20,
paragraphSpacing: 20,
+8
View File
@@ -0,0 +1,8 @@
{
"functions": {
"pages/api/generate-pdf.js": {
"memory": 3008,
"maxDuration": 60
}
}
}
+30 -7
View File
@@ -164,6 +164,9 @@ importers:
'@tiptap/starter-kit':
specifier: ^2.4.0
version: 2.9.1
chrome-aws-lambda:
specifier: ^10.1.0
version: 10.1.0(puppeteer-core@23.9.0)
class-variance-authority:
specifier: ^0.7.0
version: 0.7.0
@@ -2241,6 +2244,12 @@ packages:
resolution: {integrity: sha512-n8enUVCED/KVRQlab1hr3MVpcVMvxtZjmEa956u+4YijlmQED223XMSYj2tLuKvr4jcCTzNNMpQDUer72MMmzA==}
engines: {node: '>= 14.16.0'}
chrome-aws-lambda@10.1.0:
resolution: {integrity: sha512-NZQVf+J4kqG4sVhRm3WNmOfzY0OtTSm+S8rg77pwePa9RCYHzhnzRs8YvNI6L9tALIW6RpmefWiPURt3vURXcw==}
engines: {node: '>= 10.16'}
peerDependencies:
puppeteer-core: ^10.1.0
chrome-trace-event@1.0.3:
resolution: {integrity: sha512-p3KULyQg4S7NIHixdwbGX+nFHkoBiA4YQmyWtjb8XngSKV124nJmRysgAeujbUVb15vh+RvFUfCPqU7rXk+hZg==}
engines: {node: '>=6.0'}
@@ -3607,6 +3616,13 @@ packages:
resolution: {integrity: sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==}
engines: {node: '>=6'}
lambdafs@2.1.1:
resolution: {integrity: sha512-x5k8JcoJWkWLvCVBzrl4pzvkEHSgSBqFjg3Dpsc4AcTMq7oUMym4cL/gRTZ6VM4mUMY+M0dIbQ+V1c1tsqqanQ==}
engines: {node: '>= 10.16'}
hasBin: true
bundledDependencies:
- tar-fs
language-subtag-registry@0.3.22:
resolution: {integrity: sha512-tN0MCzyWnoz/4nHS6uxdlFWoUZT7ABptwKPQ52Ea7URk6vll88bWBVhodtnlfEuCcKWNGoc+uGbw1cwa9IKh/w==}
@@ -5428,7 +5444,7 @@ snapshots:
'@eslint/eslintrc@2.1.4':
dependencies:
ajv: 6.12.6
debug: 4.3.4
debug: 4.3.7
espree: 9.6.1
globals: 13.24.0
ignore: 5.3.1
@@ -5461,7 +5477,7 @@ snapshots:
'@humanwhocodes/config-array@0.11.14':
dependencies:
'@humanwhocodes/object-schema': 2.0.3
debug: 4.3.4
debug: 4.3.7
minimatch: 3.1.2
transitivePeerDependencies:
- supports-color
@@ -7439,6 +7455,11 @@ snapshots:
dependencies:
readdirp: 4.0.2
chrome-aws-lambda@10.1.0(puppeteer-core@23.9.0):
dependencies:
lambdafs: 2.1.1
puppeteer-core: 23.9.0
chrome-trace-event@1.0.3: {}
chromium-bidi@0.8.0(devtools-protocol@0.0.1367902):
@@ -7913,10 +7934,10 @@ snapshots:
eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@6.21.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.1(eslint@8.57.0))(eslint@8.57.0):
dependencies:
debug: 4.3.4
debug: 4.3.7
enhanced-resolve: 5.16.1
eslint: 8.57.0
eslint-module-utils: 2.8.1(@typescript-eslint/parser@6.21.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0)
eslint-module-utils: 2.8.1(@typescript-eslint/parser@6.21.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@6.21.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.1(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0)
eslint-plugin-import: 2.29.1(@typescript-eslint/parser@6.21.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0)
fast-glob: 3.3.2
get-tsconfig: 4.7.5
@@ -7928,7 +7949,7 @@ snapshots:
- eslint-import-resolver-webpack
- supports-color
eslint-module-utils@2.8.1(@typescript-eslint/parser@6.21.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0):
eslint-module-utils@2.8.1(@typescript-eslint/parser@6.21.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@6.21.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.1(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0):
dependencies:
debug: 3.2.7
optionalDependencies:
@@ -7949,7 +7970,7 @@ snapshots:
doctrine: 2.1.0
eslint: 8.57.0
eslint-import-resolver-node: 0.3.9
eslint-module-utils: 2.8.1(@typescript-eslint/parser@6.21.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0)
eslint-module-utils: 2.8.1(@typescript-eslint/parser@6.21.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@6.21.0(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.1(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0)
hasown: 2.0.2
is-core-module: 2.13.1
is-glob: 4.0.3
@@ -9154,6 +9175,8 @@ snapshots:
kleur@3.0.3: {}
lambdafs@2.1.1: {}
language-subtag-registry@0.3.22: {}
language-tags@1.0.9:
@@ -10284,7 +10307,7 @@ snapshots:
dependencies:
component-emitter: 1.3.1
cookiejar: 2.1.4
debug: 4.3.4
debug: 4.3.7
fast-safe-stringify: 2.1.1
form-data: 4.0.0
formidable: 2.1.2