diff --git a/tools/gp/cli/handlers.ts b/tools/gp/cli/handlers.ts index cf7e6b8..7a2deae 100644 --- a/tools/gp/cli/handlers.ts +++ b/tools/gp/cli/handlers.ts @@ -1,8 +1,8 @@ -import { - DocuBuilder, - IndexBuilder, - OpenPGPSigner, -} from "../pdf_builder/builder.ts"; +import { OpenPGPSigner } from "../pdf_builder/utils.ts"; + +import { DocuBuilder } from "../pdf_builder/docu_builder.ts"; +import { IndexBuilder } from "../pdf_builder/index_builder.ts"; +//import TurndownService from "turndown"; export async function generate_pdf() { const cfg = JSON.parse(await Deno.readTextFile("config.json")); diff --git a/tools/gp/cli/match.ts b/tools/gp/cli/match.ts index 8f787ed..4d1f56e 100644 --- a/tools/gp/cli/match.ts +++ b/tools/gp/cli/match.ts @@ -1,4 +1,4 @@ -import { OpenPGPSigner } from "../pdf_builder/builder.ts"; +import { OpenPGPSigner } from "../pdf_builder/utils.ts"; import { add_index_to_pdf, generate_md, diff --git a/tools/gp/computer_template/style.html b/tools/gp/computer_template/style.html index a6e3628..f877d5d 100644 --- a/tools/gp/computer_template/style.html +++ b/tools/gp/computer_template/style.html @@ -137,6 +137,8 @@ word-wrap: normal; line-height: 1; margin-top: 0.5rem; + break-inside: avoid; + page-break-inside: avoid; } index li page_title { color: black; @@ -148,6 +150,8 @@ white-space: normal; word-break: break-word; overflow-wrap: anywhere; + line-height: 1.5; + display: inline-block; } index li page_column { @@ -172,4 +176,8 @@ font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; } + index li a { + break-inside: avoid; + page-break-inside: avoid; + } diff --git a/tools/gp/main_test.ts b/tools/gp/main_test.ts index e8e746d..b12153c 100644 --- a/tools/gp/main_test.ts +++ b/tools/gp/main_test.ts @@ -1,5 +1,6 @@ import { PFile, PFiles } from "./file.ts"; -import { DocuBuilder, IndexBuilder } from "./pdf_builder/builder.ts"; +import { DocuBuilder } from "./pdf_builder/docu_builder.ts"; +import { IndexBuilder } from "./pdf_builder/index_builder.ts"; import { PDFParse } from "pdf-parse"; // Deno.test("PFiles initialization", () => { diff --git a/tools/gp/mobile_template/style.html b/tools/gp/mobile_template/style.html index 225cfab..d128606 100644 --- a/tools/gp/mobile_template/style.html +++ b/tools/gp/mobile_template/style.html @@ -135,6 +135,8 @@ word-wrap: normal; line-height: 1; margin-top: 0.5rem; + break-inside: avoid; + page-break-inside: avoid; } index li page_title { color: black; @@ -146,6 +148,8 @@ white-space: normal; word-break: break-word; overflow-wrap: anywhere; + line-height: 1.5; + display: inline-block; } index li page_column { @@ -170,4 +174,8 @@ font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; } + index li a { + break-inside: avoid; + page-break-inside: avoid; + } diff --git a/tools/gp/pdf_builder/docu_builder.ts b/tools/gp/pdf_builder/docu_builder.ts new file mode 100644 index 0000000..ec9cc08 --- /dev/null +++ b/tools/gp/pdf_builder/docu_builder.ts @@ -0,0 +1,234 @@ +import { Browser } from "playwright"; +import { PFiles } from "../file.ts"; +import MarkdownIt from "markdown-it"; +import TurndownService from "turndown"; + +import "openpgp"; + +import { resolveChromiumPath } from "./utils.ts"; + +//原文->中间体(html)->目标格式(pdf/md/...) +//生成pdf的流程:原文->中间体(html)->playwright渲染->pdf +//生成md的流程:原文->中间体(html)->turndown转换->md +export class DocuBuilder { + pages: Array; + // deno-lint-ignore no-explicit-any + cfg: any; + browser: Browser | null; + pfiles: PFiles | null = null; + // deno-lint-ignore no-explicit-any + constructor(cfg: any) { + this.cfg = cfg; + this.pages = []; + this.browser = null; + } + async with_browser() { + const { chromium } = await import("playwright"); + const browser = await chromium.launch({ + executablePath: resolveChromiumPath(), + headless: true, + args: ["--no-sandbox", "--disable-dev-shm-usage"], + }); + this.browser = browser; + } + + with_files_sync(dir: string) { + this.pfiles = new PFiles( + this.cfg.journal.build_mode ?? "flat", + dir, + this.cfg, + ); + } + + private strip_markdown_symbols(markdown: string): string { + return markdown + .replace(/^\s{0,3}#{1,6}\s*/gm, "") + .replace(/^(.+)\n={3,}\s*$/gm, "$1") + .replace(/^(.+)\n-{3,}\s*$/gm, "$1") + .replace(/^\s{0,3}>\s?/gm, "") + .replace(/^\s{0,3}(?:[-*+]|\d+\.)\s+/gm, "") + .replace(/^\s{0,3}(?:---|\*\*\*|___)\s*$/gm, "") + .replace(/!\[([^\]]*)\]\((.*?)\)/g, "$1") + .replace(/\[([^\]]+)\]\((.*?)\)/g, "$1") + .replace(/\[([^\]]+)\]\[[^\]]*\]/g, "$1") + .replace(/!\[([^\]]*)\]\[[^\]]*\]/g, "$1") + .replace(/(`{1,3})([^`]+?)\1/g, "$2") + .replace(/(\*\*|__)(.*?)\1/g, "$2") + .replace(/(\*|_)(.*?)\1/g, "$2") + .replace(/\\([\\`*_{}\[\]()#+\-.!>])/g, "$1") + .replace(/<[^>]+>/g, "") + .replace(/\n{3,}/g, "\n\n") + .trim(); + } + + async as_markdown() { + const turndownService = new TurndownService(); + const html_page = await this.as_html_page(); + const cleaned_html = html_page.replace( + /<[^>]*class="[^"]*\bc_title\b[^"]*"[^>]*>([\s\S]*?)<\/[^>]+>/gi, + "# $1", + ).replace( + /]*>[\s\S]*?<\/script>|]*>[\s\S]*?<\/style>/gi, + "", + ); + const md_page = turndownService.turndown(cleaned_html); + this.pages = [md_page]; + } + + async as_txt(remove_links = true) { + const turndownService = new TurndownService(); + const html_page = await this.as_html_page(); + const cleaned_html = html_page.replace( + /<[^>]*class="[^"]*\bc_title\b[^"]*"[^>]*>([\s\S]*?)<\/[^>]+>/gi, + "# $1", + ).replace( + /]*>[\s\S]*?<\/script>|]*>[\s\S]*?<\/style>/gi, + "", + ).replaceAll("#", ""); // Remove markdown symbols for plain text + const md_page = turndownService.turndown(cleaned_html); + this.pages = [this.strip_markdown_symbols(md_page)]; + } + + async as_html_page(): Promise { + this.pages = []; + const md = new MarkdownIt({ + html: true, + linkify: true, + typographer: true, + }); + + this.pages.push(this.pfiles?.cover_html ?? ""); + this.pages.push(this.pfiles?.style_html ?? ""); + this.pages.push(this.pfiles?.other_htmls.join("\n") ?? ""); + + if (this.pfiles != null) { + let id = 1; + this.pfiles.pcolumes.forEach((colume) => { + let c_id = 1; + this.pages.push( + `${colume.name}`, + ); + colume.pfiles.forEach((pfile) => { + const html = md.render(pfile.content); + this.pages.push( + `
${html}
`, + ); + }); + }); + } + this.pages.push(this.pfiles?.end_html ?? ""); + if (this.cfg.journal.build_js_script != false) { + const f = Deno.readTextFileSync(`${this.cfg.journal.build_js_script}`); + const html = this.pages.join("\n"); + + // Ensure a DOMParser is available; if not, try to load deno_dom dynamically. + let domParserCtor: { + new (): { + parseFromString: ( + html: string, + mimeType: string, + ) => { documentElement?: { outerHTML: string } } | null; + }; + } | undefined = (globalThis as { DOMParser?: unknown }).DOMParser as + | { + new (): { + parseFromString: ( + html: string, + mimeType: string, + ) => { documentElement?: { outerHTML: string } } | null; + }; + } + | undefined; + + if (domParserCtor == null) { + try { + const denoDom = await import( + // deno-lint-ignore no-import-prefix + "https://deno.land/x/deno_dom/deno-dom-wasm.ts" + ); + if (denoDom && denoDom.DOMParser) { + // deno-lint-ignore no-explicit-any + (globalThis as any).DOMParser = denoDom.DOMParser; + // Cast via unknown to satisfy TypeScript when converting between incompatible constructor types + domParserCtor = denoDom + .DOMParser as unknown as typeof domParserCtor; + } + } catch (e) { + // ignore; will throw below with helpful message + console.error(e); + } + } + + if (domParserCtor == null) { + throw new Error( + "DOMParser is not available in this runtime. Install deno_dom or run in a runtime that provides DOMParser.\n" + + "Suggested: import 'https://deno.land/x/deno_dom/deno-dom-wasm.ts' or run with a JS DOM-enabled environment.", + ); + } + + const document = new domParserCtor().parseFromString(html, "text/html"); + const runBuildScript = new Function( + "html", + "document", + "pages", + "cfg", + `${f} +return document?.documentElement?.outerHTML ?? html;`, + ) as ( + html: string, + document: { documentElement?: { outerHTML: string } } | null, + pages: Array, + cfg: unknown, + ) => string; + const builtHtml = runBuildScript( + html, + document, + [...this.pages], + this.cfg, + ); + this.pages = [builtHtml]; + } + //console.debug(structuredClone(this.pages).join("\n")); + return this.pages.join("\n"); + } + async build_as_raw(file_name: string) { + await Deno.writeFile( + file_name, + new TextEncoder().encode(this.pages.join("\n")), + ); + } + + // deno-lint-ignore no-explicit-any + async build_as_pdf(pw_config: any, pages = this.pages) { + if (this.browser == null) { + throw new Error("Browser is not initialized. Call with_browser() first."); + } + if (this.pfiles != null && pages.length === 0) { + await this.as_html_page(); + } + const page = await this.browser.newPage(); + try { + // const setContentTimeout = pw_config?.setContent_timeout_ms ?? 120000; + // const setContentWaitUntil = pw_config?.setContent_wait_until ?? + // "domcontentloaded"; + await page.setContent(pages.join("\n"), { + timeout: 120000, + // waitUntil: setContentWaitUntil, + }); + + await page.pdf( + pw_config, + ); + } finally { + await page.close(); + } + } + + async close_browser() { + if (this.browser == null) { + return; + } + await this.browser.close(); + this.browser = null; + } +} diff --git a/tools/gp/pdf_builder/builder.ts b/tools/gp/pdf_builder/index_builder.ts similarity index 54% rename from tools/gp/pdf_builder/builder.ts rename to tools/gp/pdf_builder/index_builder.ts index 750362e..d180c8a 100644 --- a/tools/gp/pdf_builder/builder.ts +++ b/tools/gp/pdf_builder/index_builder.ts @@ -1,7 +1,4 @@ -import { Browser } from "playwright"; import { PFiles } from "../file.ts"; -import MarkdownIt from "markdown-it"; -import TurndownService from "turndown"; import { PDFParse } from "pdf-parse"; import { PDFArray, @@ -11,247 +8,20 @@ import { PDFNumber, } from "pdf-lib"; import "openpgp"; -import * as openpgp from "openpgp"; -import { PathLike } from "node:fs"; -function resolveChromiumPath(): string { - const chromiumPath = Deno.env.get("CHROMIUM_PATH"); - if (chromiumPath) { - return chromiumPath; - } +import { resolveChromiumPath } from "./utils.ts"; +import { DocuBuilder } from "./docu_builder.ts"; - throw new Error( - [ - "CHROMIUM_PATH is not set.", - "Use the provided flake shell or set CHROMIUM_PATH manually.", - 'Example: export CHROMIUM_PATH="$(command -v chromium)"', - ].join("\n"), - ); -} -//原文->中间体(html)->目标格式(pdf/md/...) -//生成pdf的流程:原文->中间体(html)->playwright渲染->pdf -//生成md的流程:原文->中间体(html)->turndown转换->md -export class DocuBuilder { - pages: Array; - // deno-lint-ignore no-explicit-any - cfg: any; - browser: Browser | null; - pfiles: PFiles | null = null; - // deno-lint-ignore no-explicit-any - constructor(cfg: any) { - this.cfg = cfg; - this.pages = []; - this.browser = null; - } - async with_browser() { - const { chromium } = await import("playwright"); - const browser = await chromium.launch({ - executablePath: resolveChromiumPath(), - headless: true, - args: ["--no-sandbox", "--disable-dev-shm-usage"], - }); - this.browser = browser; - } +type IndexItemLayout = { + pageIndex: number; + left: number; + top: number; + width: number; + height: number; +}; - with_files_sync(dir: string) { - this.pfiles = new PFiles( - this.cfg.journal.build_mode ?? "flat", - dir, - this.cfg, - ); - } - - private strip_markdown_symbols(markdown: string): string { - return markdown - .replace(/^\s{0,3}#{1,6}\s*/gm, "") - .replace(/^(.+)\n={3,}\s*$/gm, "$1") - .replace(/^(.+)\n-{3,}\s*$/gm, "$1") - .replace(/^\s{0,3}>\s?/gm, "") - .replace(/^\s{0,3}(?:[-*+]|\d+\.)\s+/gm, "") - .replace(/^\s{0,3}(?:---|\*\*\*|___)\s*$/gm, "") - .replace(/!\[([^\]]*)\]\((.*?)\)/g, "$1") - .replace(/\[([^\]]+)\]\((.*?)\)/g, "$1") - .replace(/\[([^\]]+)\]\[[^\]]*\]/g, "$1") - .replace(/!\[([^\]]*)\]\[[^\]]*\]/g, "$1") - .replace(/(`{1,3})([^`]+?)\1/g, "$2") - .replace(/(\*\*|__)(.*?)\1/g, "$2") - .replace(/(\*|_)(.*?)\1/g, "$2") - .replace(/\\([\\`*_{}\[\]()#+\-.!>])/g, "$1") - .replace(/<[^>]+>/g, "") - .replace(/\n{3,}/g, "\n\n") - .trim(); - } - - async as_markdown() { - const turndownService = new TurndownService(); - const html_page = await this.as_html_page(); - const cleaned_html = html_page.replace( - /<[^>]*class="[^"]*\bc_title\b[^"]*"[^>]*>([\s\S]*?)<\/[^>]+>/gi, - "# $1", - ).replace( - /]*>[\s\S]*?<\/script>|]*>[\s\S]*?<\/style>/gi, - "", - ); - const md_page = turndownService.turndown(cleaned_html); - this.pages = [md_page]; - } - - async as_txt(remove_links = true) { - const turndownService = new TurndownService(); - const html_page = await this.as_html_page(); - const cleaned_html = html_page.replace( - /<[^>]*class="[^"]*\bc_title\b[^"]*"[^>]*>([\s\S]*?)<\/[^>]+>/gi, - "# $1", - ).replace( - /]*>[\s\S]*?<\/script>|]*>[\s\S]*?<\/style>/gi, - "", - ).replaceAll("#", ""); // Remove markdown symbols for plain text - const md_page = turndownService.turndown(cleaned_html); - this.pages = [this.strip_markdown_symbols(md_page)]; - } - - async as_html_page(): Promise { - this.pages = []; - const md = new MarkdownIt({ - html: true, - linkify: true, - typographer: true, - }); - - this.pages.push(this.pfiles?.cover_html ?? ""); - this.pages.push(this.pfiles?.style_html ?? ""); - this.pages.push(this.pfiles?.other_htmls.join("\n") ?? ""); - - if (this.pfiles != null) { - let id = 1; - this.pfiles.pcolumes.forEach((colume) => { - let c_id = 1; - this.pages.push( - `${colume.name}`, - ); - colume.pfiles.forEach((pfile) => { - const html = md.render(pfile.content); - this.pages.push( - `
${html}
`, - ); - }); - }); - } - this.pages.push(this.pfiles?.end_html ?? ""); - if (this.cfg.journal.build_js_script != false) { - const f = Deno.readTextFileSync(`${this.cfg.journal.build_js_script}`); - const html = this.pages.join("\n"); - - // Ensure a DOMParser is available; if not, try to load deno_dom dynamically. - let domParserCtor: { - new (): { - parseFromString: ( - html: string, - mimeType: string, - ) => { documentElement?: { outerHTML: string } } | null; - }; - } | undefined = (globalThis as { DOMParser?: unknown }).DOMParser as - | { - new (): { - parseFromString: ( - html: string, - mimeType: string, - ) => { documentElement?: { outerHTML: string } } | null; - }; - } - | undefined; - - if (domParserCtor == null) { - try { - const denoDom = await import( - // deno-lint-ignore no-import-prefix - "https://deno.land/x/deno_dom/deno-dom-wasm.ts" - ); - if (denoDom && denoDom.DOMParser) { - // deno-lint-ignore no-explicit-any - (globalThis as any).DOMParser = denoDom.DOMParser; - // Cast via unknown to satisfy TypeScript when converting between incompatible constructor types - domParserCtor = denoDom - .DOMParser as unknown as typeof domParserCtor; - } - } catch (e) { - // ignore; will throw below with helpful message - console.error(e); - } - } - - if (domParserCtor == null) { - throw new Error( - "DOMParser is not available in this runtime. Install deno_dom or run in a runtime that provides DOMParser.\n" + - "Suggested: import 'https://deno.land/x/deno_dom/deno-dom-wasm.ts' or run with a JS DOM-enabled environment.", - ); - } - - const document = new domParserCtor().parseFromString(html, "text/html"); - const runBuildScript = new Function( - "html", - "document", - "pages", - "cfg", - `${f} -return document?.documentElement?.outerHTML ?? html;`, - ) as ( - html: string, - document: { documentElement?: { outerHTML: string } } | null, - pages: Array, - cfg: unknown, - ) => string; - const builtHtml = runBuildScript( - html, - document, - [...this.pages], - this.cfg, - ); - this.pages = [builtHtml]; - } - //console.debug(structuredClone(this.pages).join("\n")); - return this.pages.join("\n"); - } - async build_as_raw(file_name: string) { - await Deno.writeFile( - file_name, - new TextEncoder().encode(this.pages.join("\n")), - ); - } - - // deno-lint-ignore no-explicit-any - async build_as_pdf(pw_config: any, pages = this.pages) { - if (this.browser == null) { - throw new Error("Browser is not initialized. Call with_browser() first."); - } - if (this.pfiles != null && pages.length === 0) { - await this.as_html_page(); - } - const page = await this.browser.newPage(); - try { - // const setContentTimeout = pw_config?.setContent_timeout_ms ?? 120000; - // const setContentWaitUntil = pw_config?.setContent_wait_until ?? - // "domcontentloaded"; - await page.setContent(pages.join("\n"), { - timeout: 120000, - // waitUntil: setContentWaitUntil, - }); - - await page.pdf( - pw_config, - ); - } finally { - await page.close(); - } - } - - async close_browser() { - if (this.browser == null) { - return; - } - await this.browser.close(); - this.browser = null; - } -} +const PDF_PAGE_HEIGHT_PT = 842; +const PDF_VIEWPORT_WIDTH_PX = 794; +const CSS_PX_TO_PT = 72 / 96; export class IndexBuilder { indexed_map = new Map< @@ -311,8 +81,8 @@ export class IndexBuilder { //修改实现为在每页文本中查找目录标题,支持目录标题跨行的情况。通过维护一个标题游标,逐页扫描PDF文本,尝试匹配当前标题。每页文本被拆分成行,并进行规范化(去除空格和换行)。对于每个标题,尝试将连续的行合并起来进行匹配,如果找到匹配则记录该标题对应的页码和章节索引,并移动到下一个标题。这样可以更准确地处理目录标题在PDF中被拆分成多行的情况。 async build_index() { const pdf_obj = await this.read_pdf(); - const { titles, passages_index, passages_column_name } = - this.get_index_titles_from_file_name( + const { titles, passages_index, passages_column_name } = this + .get_index_titles_from_file_name( this.cfg.journal.build_mode ?? "flat", ); // Normalize titles (remove spaces/newlines) and require exact equality. @@ -330,11 +100,13 @@ export class IndexBuilder { } const pageTextRaw = pdf_obj.getPageText(i); - const lines = pageTextRaw.split(/\r?\n/).map((l) => l.trim()).filter( - (l) => l.length > 0, + const lines = pageTextRaw.split(/\r?\n/).map((l: string) => l.trim()).filter( + (l: string) => l.length > 0, ); - const normalizedLines = lines.map((line) => line.replaceAll(" ", "")); + const normalizedLines = lines.map((line: string) => + line.replaceAll(" ", "") + ); const tt = normalizedTitles[titleCursor]; for (let start = 0; start < normalizedLines.length; start++) { @@ -388,10 +160,34 @@ export class IndexBuilder { as_html_page(): string { const styles = Deno.readTextFileSync(`${this.template_dir}/style.html`); const fonts = Deno.readTextFileSync(`${this.template_dir}/fonts.html`); + const debugIndexLayout = Deno.env.get("INDEX_DEBUG_LAYOUT") === "1"; + const debugStyles = debugIndexLayout + ? `` + : ""; + const pageStyles = ``; const html = - `${styles}${fonts}

目录

    ${ + `${styles}${fonts}${pageStyles}${debugStyles}

    目录

      ${ this.get_sorted_index_entries().map(( - [title, { page, column_name }], + [title, { page, column_name }], ) => `
    • ${column_name} ${title} ${page}
    • ` ).join("\n") @@ -400,6 +196,24 @@ export class IndexBuilder { return html; } + private getPdfMarginTopPt(): number { + const topMargin = this.cfg?.playwright?.margin?.top ?? "20mm"; + const match = /^([0-9.]+)\s*mm$/i.exec(String(topMargin).trim()); + if (match) { + return Number(match[1]) / 25.4 * 72; + } + return (20 / 25.4) * 72; + } + + private getPdfMarginBottomPt(): number { + const bottomMargin = this.cfg?.playwright?.margin?.bottom ?? "20mm"; + const match = /^([0-9.]+)\s*mm$/i.exec(String(bottomMargin).trim()); + if (match) { + return Number(match[1]) / 25.4 * 72; + } + return (20 / 25.4) * 72; + } + // deno-lint-ignore no-explicit-any async html2pdf(cfg: any) { const builder = new DocuBuilder(cfg); @@ -412,31 +226,54 @@ export class IndexBuilder { } } - private async get_index_item_layouts(index_html: string) { + private async get_index_item_layouts(index_html: string): Promise< + Array + > { const { chromium } = await import("playwright"); const browser = await chromium.launch({ executablePath: resolveChromiumPath(), headless: true, args: ["--no-sandbox", "--disable-dev-shm-usage"], }); - const page = await browser.newPage(); + const page = await browser.newPage({ + viewport: { + width: PDF_VIEWPORT_WIDTH_PX, + height: 1123, + }, + }); try { await page.setContent(index_html, { timeout: 120000 }); await page.emulateMedia({ media: "print" }); - return await page.evaluate(() => { - const items = Array.from( - document.querySelectorAll("li.index-item"), - ); - return items.map((item) => { - const rect = item.getBoundingClientRect(); - return { - top: rect.top, - left: rect.left, - width: rect.width, - height: rect.height, - }; - }); - }); + return await page.evaluate( + ({ printableHeightPx }: { printableHeightPx: number }) => { + // Browser-side DOM types are not available in Deno's type checker. + const doc = (globalThis as any).document as any; + const items = Array.from( + doc.querySelectorAll("li.index-item"), + ) as any[]; + return items.map((item) => { + const titleEl = item.querySelector("page_title") as any; + const itemRect = item.getBoundingClientRect(); + const titleRect = titleEl?.getBoundingClientRect() ?? itemRect; + const pageIndex = Math.floor( + (itemRect.bottom - 1) / printableHeightPx, + ); + const pageTopPx = pageIndex * printableHeightPx; + return { + pageIndex, + left: titleRect.left, + width: titleRect.width, + top: itemRect.top - pageTopPx, + height: itemRect.height, + }; + }); + }, + { + printableHeightPx: + (PDF_PAGE_HEIGHT_PT - this.getPdfMarginTopPt() - + this.getPdfMarginBottomPt()) / CSS_PX_TO_PT, + }, + ); } finally { await page.close(); await browser.close(); @@ -465,15 +302,15 @@ export class IndexBuilder { ); //插入index页到指定位置,默认插入到第一页后面 if (mainPages.length > 0) { - mainPages.slice(0, insert_index_after_page).forEach((page) => + mainPages.slice(0, insert_index_after_page).forEach((page: any) => combinedPdf.addPage(page) ); - indexPages.forEach((page) => combinedPdf.addPage(page)); - mainPages.slice(insert_index_after_page).forEach((page) => + indexPages.forEach((page: any) => combinedPdf.addPage(page)); + mainPages.slice(insert_index_after_page).forEach((page: any) => combinedPdf.addPage(page) ); } else { - indexPages.forEach((page) => combinedPdf.addPage(page)); + indexPages.forEach((page: any) => combinedPdf.addPage(page)); } await this.add_index_link_annotations_to_pdf( @@ -549,6 +386,7 @@ export class IndexBuilder { ] >, index_item_layouts: Array<{ + pageIndex: number; top: number; left: number; width: number; @@ -576,22 +414,19 @@ export class IndexBuilder { const ctx = pdfDoc.context; const pageHeight = indexPages[0].getSize().height; const pageWidth = indexPages[0].getSize().width; - const printableHeight = pageHeight - 40 - 40; - const marginTop = 40; - const marginBottom = 40; + const marginTop = this.getPdfMarginTopPt(); + const minRectHeight = 12; sorted_entries.forEach((entry, index) => { const layout = index_item_layouts[index]; if (!layout) { return; } - const pageOffset = Math.floor(layout.top / printableHeight); - const targetPage = indexPages[Math.min(pageOffset, indexPages.length - 1)]; - const localTop = layout.top - pageOffset * printableHeight; - const yTop = pageHeight - marginTop - localTop; - const yBottom = yTop - Math.max(layout.height, 12); - const xLeft = Math.max(layout.left - 12, 0); - const xRight = Math.min(layout.left + layout.width + 12, pageWidth); + const layoutLeftPt = layout.left * CSS_PX_TO_PT; + const layoutWidthPt = layout.width * CSS_PX_TO_PT; + const layoutHeightPt = layout.height * CSS_PX_TO_PT; + const xLeft = Math.max(layoutLeftPt, 0); + const xRight = Math.min(layoutLeftPt + layoutWidthPt, pageWidth); const mappedPage = this.remap_page_after_index_insert( entry[1].page, inserted_index_pages, @@ -604,6 +439,17 @@ export class IndexBuilder { const destPage = pages[destPageIndex]; const destPageRef = (destPage as any).ref; const destPageHeight = destPage.getSize().height; + const totalHeight = Math.max(layoutHeightPt, minRectHeight); + const targetPage = indexPages[Math.min( + Math.max(layout.pageIndex, 0), + indexPages.length - 1, + )]; + if (!targetPage) { + return; + } + const localTopPt = layout.top * CSS_PX_TO_PT; + const yTop = pageHeight - marginTop - localTopPt; + const yBottom = yTop - totalHeight; this.add_link_annotation( targetPage as any, ctx, @@ -713,48 +559,3 @@ export class IndexBuilder { await Deno.writeFile(this.file_path, outlinedPdfBytes); } } - -export class OpenPGPSigner { - file_path: PathLike; - output_path: PathLike; - pri_key: Promise; - pri_passphrase?: string; - - constructor( - file: PathLike, - pri_key: PathLike, - output_path: PathLike, - pri_passphrase?: string, - ) { - this.file_path = file; - this.output_path = output_path; - this.pri_passphrase = pri_passphrase; - this.pri_key = openpgp.readPrivateKey({ - armoredKey: Deno.readTextFileSync(pri_key as string), - }); - } - - private async getDecryptedPrivateKey(): Promise { - let privateKey = await this.pri_key; - if (this.pri_passphrase) { - privateKey = await openpgp.decryptKey({ - privateKey, - passphrase: this.pri_passphrase, - }); - } - return privateKey; - } - - async sign() { - const file_bytes = await Deno.readFile(this.file_path as string); - const message = await openpgp.createMessage({ binary: file_bytes }); - const signingKey = await this.getDecryptedPrivateKey(); - const signature = await openpgp.sign({ - message, - signingKeys: signingKey, - format: "armored", - detached: true, - }); - await Deno.writeTextFile(this.output_path as string, signature); - } -} diff --git a/tools/gp/pdf_builder/utils.ts b/tools/gp/pdf_builder/utils.ts new file mode 100644 index 0000000..7ae792b --- /dev/null +++ b/tools/gp/pdf_builder/utils.ts @@ -0,0 +1,61 @@ +import "openpgp"; +import * as openpgp from "openpgp"; +import { PathLike } from "node:fs"; +export function resolveChromiumPath(): string { + const chromiumPath = Deno.env.get("CHROMIUM_PATH"); + if (chromiumPath) { + return chromiumPath; + } + + throw new Error( + [ + "CHROMIUM_PATH is not set.", + "Use the provided flake shell or set CHROMIUM_PATH manually.", + 'Example: export CHROMIUM_PATH="$(command -v chromium)"', + ].join("\n"), + ); +} +export class OpenPGPSigner { + file_path: PathLike; + output_path: PathLike; + pri_key: Promise; + pri_passphrase?: string; + + constructor( + file: PathLike, + pri_key: PathLike, + output_path: PathLike, + pri_passphrase?: string, + ) { + this.file_path = file; + this.output_path = output_path; + this.pri_passphrase = pri_passphrase; + this.pri_key = openpgp.readPrivateKey({ + armoredKey: Deno.readTextFileSync(pri_key as string), + }); + } + + private async getDecryptedPrivateKey(): Promise { + let privateKey = await this.pri_key; + if (this.pri_passphrase) { + privateKey = await openpgp.decryptKey({ + privateKey, + passphrase: this.pri_passphrase, + }); + } + return privateKey; + } + + async sign() { + const file_bytes = await Deno.readFile(this.file_path as string); + const message = await openpgp.createMessage({ binary: file_bytes }); + const signingKey = await this.getDecryptedPrivateKey(); + const signature = await openpgp.sign({ + message, + signingKeys: signingKey, + format: "armored", + detached: true, + }); + await Deno.writeTextFile(this.output_path as string, signature); + } +} diff --git a/tools/gp/printable_template/style.html b/tools/gp/printable_template/style.html index 023326d..d08d56e 100644 --- a/tools/gp/printable_template/style.html +++ b/tools/gp/printable_template/style.html @@ -107,6 +107,8 @@ word-break: break-all; word-wrap: normal; line-height: 1.5; + break-inside: avoid; + page-break-inside: avoid; } index li page_title { color: black; @@ -118,6 +120,8 @@ white-space: normal; word-break: break-word; overflow-wrap: anywhere; + line-height: 1.5; + display: inline-block; } index li page_num { @@ -132,4 +136,8 @@ font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; } + index li a { + break-inside: avoid; + page-break-inside: avoid; + }