import { PFiles } from "../file.ts"; import { PDFParse } from "pdf-parse"; import { PDFArray, PDFDocument, PDFHexString, PDFName, PDFNumber, } from "pdf-lib"; import "openpgp"; import { resolveChromiumPath } from "./utils.ts"; import { DocuBuilder } from "./docu_builder.ts"; type IndexItemLayout = { pageIndex: number; left: number; top: number; width: number; height: number; }; 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< string, { page: number; passage_index: number; column_name: string; } >(); total_pages: number = 0; template_dir: string = ""; passage_dir: string = ""; file_path: string = ""; cfg: any; constructor(file_path: string, cfg: any) { if (cfg.journal.build_mode == "flat") { this.template_dir = cfg.journal.from_directory; this.passage_dir = cfg.journal.from_directory; } else { this.template_dir = cfg.journal.template_directory; this.passage_dir = cfg.journal.from_directory; } this.file_path = file_path; this.cfg = cfg; } get_index_titles_from_file_name(mode: "detached" | "flat"): { titles: Array; passages_index: Map; passages_column_name: Map; } { const pfiles = new PFiles(mode, this.passage_dir, this.cfg); const titles: Array = []; const passages_index: Map = new Map(); const passages_column_name: Map = new Map(); pfiles.pcolumes.forEach((colume) => { colume.pfiles.forEach((pfile) => { titles.push(pfile.meta.title); passages_index.set(pfile.meta.title, pfile.meta.index); passages_column_name.set(pfile.meta.title, pfile.meta.colume); }); }); return { titles, passages_index, passages_column_name }; } async read_pdf() { const parser = new PDFParse({ url: this.file_path }); try { const pdfText = await parser.getText(); this.total_pages = pdfText.total; return pdfText; } finally { await parser.destroy(); } } //修改实现为在每页文本中查找目录标题,支持目录标题跨行的情况。通过维护一个标题游标,逐页扫描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( this.cfg.journal.build_mode ?? "flat", ); // Normalize titles (remove spaces/newlines) and require exact equality. // Matching supports title text split across multiple PDF lines. const normalizedTitles = titles.map((t) => ({ orig: t, norm: t.replaceAll("\n", "").replaceAll(" ", ""), })); let titleCursor = 0; for (let i = 1; i <= this.total_pages; i++) { if (titleCursor >= normalizedTitles.length) { break; } const pageTextRaw = pdf_obj.getPageText(i); const lines = pageTextRaw.split(/\r?\n/).map((l: string) => l.trim()).filter( (l: string) => l.length > 0, ); const normalizedLines = lines.map((line: string) => line.replaceAll(" ", "") ); const tt = normalizedTitles[titleCursor]; for (let start = 0; start < normalizedLines.length; start++) { let merged = ""; for (let end = start; end < normalizedLines.length; end++) { merged += normalizedLines[end]; if (merged.length > tt.norm.length) { break; } if (merged === tt.norm) { if (!this.indexed_map.has(tt.orig)) { this.indexed_map.set(tt.orig, { page: i, passage_index: passages_index.get(tt.orig)?.valueOf() ?? 0, column_name: passages_column_name.get(tt.orig) ?? "", }); } titleCursor++; break; } } if (titleCursor >= normalizedTitles.length) { break; } } } } private get_sorted_index_entries() { return [...this.indexed_map.entries()].sort((a, b) => { const pageA = a[1].page; const pageB = b[1].page; if (pageA !== pageB) { return pageA - pageB; } const indexA = a[1].passage_index; const indexB = b[1].passage_index; if (indexA !== indexB) { return indexA - indexB; } return a[0].localeCompare(b[0], "zh-Hans-CN"); }); } 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}${pageStyles}${debugStyles}

目录

`; 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); try { await builder.with_browser(); builder.pages = [await this.as_html_page()]; await builder.build_as_pdf(cfg); } finally { await builder.close_browser(); } } 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({ 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( ({ 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(); } } async combine_index_pdf_to_main_pdf( index_pdf_path: string = "./tmps/index.pdf", insert_index_after_page = 1, ) { const combinedPdf = await PDFDocument.create(); const index_html = this.as_html_page(); const indexPdfBytes = await Deno.readFile(index_pdf_path); const mainPdfBytes = await Deno.readFile(this.file_path); const indexPdf = await PDFDocument.load(indexPdfBytes); const mainPdf = await PDFDocument.load(mainPdfBytes); const indexPages = await combinedPdf.copyPages( indexPdf, indexPdf.getPageIndices(), ); const mainPages = await combinedPdf.copyPages( mainPdf, mainPdf.getPageIndices(), ); //插入index页到指定位置,默认插入到第一页后面 if (mainPages.length > 0) { mainPages.slice(0, insert_index_after_page).forEach((page: any) => combinedPdf.addPage(page) ); indexPages.forEach((page: any) => combinedPdf.addPage(page)); mainPages.slice(insert_index_after_page).forEach((page: any) => combinedPdf.addPage(page) ); } else { indexPages.forEach((page: any) => combinedPdf.addPage(page)); } await this.add_index_link_annotations_to_pdf( combinedPdf, this.get_sorted_index_entries(), indexPages.length, insert_index_after_page, index_html, ); const combinedPdfBytes = await combinedPdf.save(); await Deno.writeFile(this.file_path, combinedPdfBytes); return indexPages.length; } private remap_page_after_index_insert( page: number, inserted_index_pages: number, insert_index_after_page: number, ): number { if (inserted_index_pages <= 0) { return page; } if (page <= insert_index_after_page) { return page; } return page + inserted_index_pages; } private add_link_annotation( page: any, ctx: any, rect: [number, number, number, number], destPageRef: any, destPageHeight: number, ) { const annotation = ctx.obj({ Type: PDFName.of("Annot"), Subtype: PDFName.of("Link"), Rect: PDFArray.withContext(ctx), Border: PDFArray.withContext(ctx), Dest: PDFArray.withContext(ctx), }); const rectArray = annotation.lookup(PDFName.of("Rect"), PDFArray); rect.forEach((value) => rectArray.push(PDFNumber.of(value))); const borderArray = annotation.lookup(PDFName.of("Border"), PDFArray); borderArray.push(PDFNumber.of(0)); borderArray.push(PDFNumber.of(0)); borderArray.push(PDFNumber.of(0)); const destArray = annotation.lookup(PDFName.of("Dest"), PDFArray); destArray.push(destPageRef); destArray.push(PDFName.of("FitH")); destArray.push(PDFNumber.of(destPageHeight)); const annotationRef = ctx.register(annotation); const annots = page.node.Annots(); if (annots) { annots.push(annotationRef); } else { page.node.set(PDFName.of("Annots"), ctx.obj([annotationRef])); } } private add_index_link_annotations( pdfDoc: PDFDocument, sorted_entries: Array< [ string, { page: number; column_name: string }, ] >, index_item_layouts: Array<{ pageIndex: number; top: number; left: number; width: number; height: number; }>, inserted_index_pages: number, insert_index_after_page: number, ) { if ( inserted_index_pages <= 0 || sorted_entries.length === 0 || index_item_layouts.length === 0 ) { return; } const pages = pdfDoc.getPages(); const indexPages = pages.slice( insert_index_after_page, insert_index_after_page + inserted_index_pages, ); if (indexPages.length === 0) { return; } const ctx = pdfDoc.context; const pageHeight = indexPages[0].getSize().height; const pageWidth = indexPages[0].getSize().width; const marginTop = this.getPdfMarginTopPt(); const minRectHeight = 12; sorted_entries.forEach((entry, index) => { const layout = index_item_layouts[index]; if (!layout) { return; } 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, insert_index_after_page, ); const destPageIndex = Math.min( Math.max(mappedPage - 1, 0), pages.length - 1, ); 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, [xLeft, yBottom, xRight, yTop], destPageRef, destPageHeight, ); }); } private async add_index_link_annotations_to_pdf( pdfDoc: PDFDocument, sorted_entries: Array< [ string, { page: number; column_name: string }, ] >, inserted_index_pages: number, insert_index_after_page: number, index_html: string, ) { const index_item_layouts = await this.get_index_item_layouts(index_html); this.add_index_link_annotations( pdfDoc, sorted_entries, index_item_layouts, inserted_index_pages, insert_index_after_page, ); } async add_pdf_outline(options?: { inserted_index_pages?: number; insert_index_after_page?: number; open_outline_by_default?: boolean; }) { const inserted_index_pages = options?.inserted_index_pages ?? 0; const insert_index_after_page = options?.insert_index_after_page ?? 1; const open_outline_by_default = options?.open_outline_by_default ?? true; const pdfBytes = await Deno.readFile(this.file_path); const pdfDoc = await PDFDocument.load(pdfBytes); const pages = pdfDoc.getPages(); if (pages.length === 0 || this.indexed_map.size === 0) { return; } const sorted_entries = this.get_sorted_index_entries(); const ctx = pdfDoc.context; // deno-lint-ignore no-explicit-any const itemNodes: Array<{ ref: any; dict: any }> = []; for (const [title, { page }] of sorted_entries) { const mappedPage = this.remap_page_after_index_insert( page, inserted_index_pages, insert_index_after_page, ); const targetPageIndex = Math.min( Math.max(mappedPage - 1, 0), pages.length - 1, ); // deno-lint-ignore no-explicit-any const pageRef = (pages[targetPageIndex] as any).ref; const targetPageHeight = pages[targetPageIndex].getSize().height; const dict = ctx.obj({ Title: PDFHexString.fromText(title), Dest: [ pageRef, PDFName.of("FitH"), PDFNumber.of(targetPageHeight), ], }); const ref = ctx.register(dict); itemNodes.push({ ref, dict }); } const outlinesDict = ctx.obj({ Type: PDFName.of("Outlines"), Count: itemNodes.length, First: itemNodes[0].ref, Last: itemNodes[itemNodes.length - 1].ref, }); const outlinesRef = ctx.register(outlinesDict); itemNodes.forEach((node, index) => { node.dict.set(PDFName.of("Parent"), outlinesRef); if (index > 0) { node.dict.set(PDFName.of("Prev"), itemNodes[index - 1].ref); } if (index < itemNodes.length - 1) { node.dict.set(PDFName.of("Next"), itemNodes[index + 1].ref); } }); // deno-lint-ignore no-explicit-any const catalog = (pdfDoc.catalog as any).dict; catalog.set(PDFName.of("Outlines"), outlinesRef); if (open_outline_by_default) { catalog.set(PDFName.of("PageMode"), PDFName.of("UseOutlines")); } const outlinedPdfBytes = await pdfDoc.save(); await Deno.writeFile(this.file_path, outlinedPdfBytes); } }