完成了封面的基本布局,还需要完善文档正文

This commit is contained in:
:NoBody: committed 2026-04-22 23:14:14 +08:00
1 parent 1918fc7a3e
commit 578109c8de
6 files changed
+486 -20

No files matched your search

+35 -7
View File
@@ -1,6 +1,6 @@
import { Browser } from "playwright";
import MarkdownIt from "markdown-it";
import { PFiles } from "../file.ts";
import MarkdownIt from "markdown-it";
function resolveChromiumPath(): string {
const chromiumPath = Deno.env.get("CHROMIUM_PATH");
@@ -41,21 +41,34 @@ export class DocuBuilder {
as_html_page() {
this.html_pages = [];
const MD = new MarkdownIt();
const md = new MarkdownIt({
html: true,
linkify: true,
typographer: true,
});
this.html_pages.push(this.pfiles?.cover_html ?? "");
this.html_pages.push(this.pfiles?.style_html ?? "");
if (this.pfiles != null) {
let id = 1;
this.pfiles.pcolumes.forEach((colume) => {
this.html_pages.push(`<h1>${colume.name}</h1>`);
let c_id = 1;
this.html_pages.push(`<h1 class="global">${colume.name}</h1>`);
colume.pfiles.forEach((pfile) => {
const html = MD.render(pfile.content);
const html = md.render(pfile.content);
this.html_pages.push(
`<div style='page-break-after: always;'>${html}</div>`,
`<div style='page-break-after: always;' class="global c${c_id++} passages" id="page-${id++}">${html}</div>`,
);
});
});
}
this.html_pages.push(this.pfiles?.end_html ?? "");
console.debug(structuredClone(this.html_pages).join("\n"));
}
async build_as_pdf(path = "output.pdf") {
async build_as_pdf(name = "output") {
if (this.browser == null) {
throw new Error("Browser is not initialized. Call with_browser() first.");
}
@@ -65,7 +78,22 @@ export class DocuBuilder {
const page = await this.browser.newPage();
try {
await page.setContent(this.html_pages.join("\n"));
await page.pdf({ path, format: "A4" });
await page.pdf({
path: `outputs/${name}-computer.pdf`,
format: "A4",
printBackground: true,
margin: { top: "20mm", bottom: "20mm", left: "10mm", right: "10mm" },
//displayHeaderFooter: true,
});
await page.pdf({
path: `outputs/${name}-smartphone.pdf`,
format: "A4",
printBackground: true,
margin: { top: "20mm", bottom: "20mm", left: "10mm", right: "10mm" },
//displayHeaderFooter: true,
scale: 1.75,
});
} finally {
await page.close();
await this.browser.close();