修复了一些目录bug,但仍然有偏移,需要手动调整
This commit is contained in:
1 parent
ab5ca2485f
commit
c886980a31
1 file changed
+70
-58
@@ -412,11 +412,43 @@ export class IndexBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async get_index_item_layouts(index_html: string) {
|
||||||
|
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();
|
||||||
|
try {
|
||||||
|
await page.setContent(index_html, { timeout: 120000 });
|
||||||
|
await page.emulateMedia({ media: "print" });
|
||||||
|
return await page.evaluate(() => {
|
||||||
|
const items = Array.from(
|
||||||
|
document.querySelectorAll<HTMLElement>("li.index-item"),
|
||||||
|
);
|
||||||
|
return items.map((item) => {
|
||||||
|
const rect = item.getBoundingClientRect();
|
||||||
|
return {
|
||||||
|
top: rect.top,
|
||||||
|
left: rect.left,
|
||||||
|
width: rect.width,
|
||||||
|
height: rect.height,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
await page.close();
|
||||||
|
await browser.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async combine_index_pdf_to_main_pdf(
|
async combine_index_pdf_to_main_pdf(
|
||||||
index_pdf_path: string = "./tmps/index.pdf",
|
index_pdf_path: string = "./tmps/index.pdf",
|
||||||
insert_index_after_page = 1,
|
insert_index_after_page = 1,
|
||||||
) {
|
) {
|
||||||
const combinedPdf = await PDFDocument.create();
|
const combinedPdf = await PDFDocument.create();
|
||||||
|
const index_html = this.as_html_page();
|
||||||
const indexPdfBytes = await Deno.readFile(index_pdf_path);
|
const indexPdfBytes = await Deno.readFile(index_pdf_path);
|
||||||
const mainPdfBytes = await Deno.readFile(this.file_path);
|
const mainPdfBytes = await Deno.readFile(this.file_path);
|
||||||
|
|
||||||
@@ -446,10 +478,10 @@ export class IndexBuilder {
|
|||||||
|
|
||||||
await this.add_index_link_annotations_to_pdf(
|
await this.add_index_link_annotations_to_pdf(
|
||||||
combinedPdf,
|
combinedPdf,
|
||||||
index_pdf_path,
|
|
||||||
this.get_sorted_index_entries(),
|
this.get_sorted_index_entries(),
|
||||||
indexPages.length,
|
indexPages.length,
|
||||||
insert_index_after_page,
|
insert_index_after_page,
|
||||||
|
index_html,
|
||||||
);
|
);
|
||||||
|
|
||||||
const combinedPdfBytes = await combinedPdf.save();
|
const combinedPdfBytes = await combinedPdf.save();
|
||||||
@@ -476,6 +508,7 @@ export class IndexBuilder {
|
|||||||
ctx: any,
|
ctx: any,
|
||||||
rect: [number, number, number, number],
|
rect: [number, number, number, number],
|
||||||
destPageRef: any,
|
destPageRef: any,
|
||||||
|
destPageHeight: number,
|
||||||
) {
|
) {
|
||||||
const annotation = ctx.obj({
|
const annotation = ctx.obj({
|
||||||
Type: PDFName.of("Annot"),
|
Type: PDFName.of("Annot"),
|
||||||
@@ -495,7 +528,8 @@ export class IndexBuilder {
|
|||||||
|
|
||||||
const destArray = annotation.lookup(PDFName.of("Dest"), PDFArray);
|
const destArray = annotation.lookup(PDFName.of("Dest"), PDFArray);
|
||||||
destArray.push(destPageRef);
|
destArray.push(destPageRef);
|
||||||
destArray.push(PDFName.of("Fit"));
|
destArray.push(PDFName.of("FitH"));
|
||||||
|
destArray.push(PDFNumber.of(destPageHeight));
|
||||||
|
|
||||||
const annotationRef = ctx.register(annotation);
|
const annotationRef = ctx.register(annotation);
|
||||||
const annots = page.node.Annots();
|
const annots = page.node.Annots();
|
||||||
@@ -506,27 +540,6 @@ export class IndexBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async get_page_line_counts(pdf_path: string): Promise<number[]> {
|
|
||||||
const parser = new PDFParse({ url: pdf_path });
|
|
||||||
try {
|
|
||||||
const pdfText = await parser.getText();
|
|
||||||
const counts: number[] = [];
|
|
||||||
for (let page = 1; page <= pdfText.total; page++) {
|
|
||||||
const pageTextRaw = pdfText.getPageText(page);
|
|
||||||
const lines = pageTextRaw.split(/\r?\n/).map((l) => l.trim()).filter(
|
|
||||||
(l) => l.length > 0,
|
|
||||||
);
|
|
||||||
// The TOC title ("目录") occupies one line on the first page and should
|
|
||||||
// not be counted as an index item. Subtracting one keeps row assignment
|
|
||||||
// aligned with the actual clickable entries.
|
|
||||||
counts.push(Math.max(lines.length - 1, 0));
|
|
||||||
}
|
|
||||||
return counts;
|
|
||||||
} finally {
|
|
||||||
await parser.destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private add_index_link_annotations(
|
private add_index_link_annotations(
|
||||||
pdfDoc: PDFDocument,
|
pdfDoc: PDFDocument,
|
||||||
sorted_entries: Array<
|
sorted_entries: Array<
|
||||||
@@ -535,13 +548,18 @@ export class IndexBuilder {
|
|||||||
{ page: number; column_name: string },
|
{ page: number; column_name: string },
|
||||||
]
|
]
|
||||||
>,
|
>,
|
||||||
index_page_line_counts: number[],
|
index_item_layouts: Array<{
|
||||||
|
top: number;
|
||||||
|
left: number;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
}>,
|
||||||
inserted_index_pages: number,
|
inserted_index_pages: number,
|
||||||
insert_index_after_page: number,
|
insert_index_after_page: number,
|
||||||
) {
|
) {
|
||||||
if (
|
if (
|
||||||
inserted_index_pages <= 0 || sorted_entries.length === 0 ||
|
inserted_index_pages <= 0 || sorted_entries.length === 0 ||
|
||||||
index_page_line_counts.length === 0
|
index_item_layouts.length === 0
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -556,33 +574,24 @@ export class IndexBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const ctx = pdfDoc.context;
|
const ctx = pdfDoc.context;
|
||||||
const { width, height } = indexPages[0].getSize();
|
const pageHeight = indexPages[0].getSize().height;
|
||||||
const left = 36;
|
const pageWidth = indexPages[0].getSize().width;
|
||||||
const right = 36;
|
const printableHeight = pageHeight - 40 - 40;
|
||||||
const top = 110;
|
const marginTop = 40;
|
||||||
const rowHeight = 22;
|
const marginBottom = 40;
|
||||||
const clickableHeight = 18;
|
|
||||||
|
|
||||||
let entryCursor = 0;
|
sorted_entries.forEach((entry, index) => {
|
||||||
index_page_line_counts.forEach((lineCount: number, pageIndex: number) => {
|
const layout = index_item_layouts[index];
|
||||||
const targetPage = indexPages[Math.min(pageIndex, indexPages.length - 1)];
|
if (!layout) {
|
||||||
const targetHeight = targetPage.getSize().height;
|
|
||||||
const usableRows = Math.max(
|
|
||||||
Math.min(
|
|
||||||
lineCount,
|
|
||||||
Math.floor((targetHeight - top - 60) / rowHeight),
|
|
||||||
),
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
|
|
||||||
for (let rowIndex = 0; rowIndex < usableRows; rowIndex++) {
|
|
||||||
const entry = sorted_entries[entryCursor];
|
|
||||||
if (!entry) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const pageOffset = Math.floor(layout.top / printableHeight);
|
||||||
const yTop = targetHeight - top - rowIndex * rowHeight;
|
const targetPage = indexPages[Math.min(pageOffset, indexPages.length - 1)];
|
||||||
const yBottom = yTop - clickableHeight;
|
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 mappedPage = this.remap_page_after_index_insert(
|
const mappedPage = this.remap_page_after_index_insert(
|
||||||
entry[1].page,
|
entry[1].page,
|
||||||
inserted_index_pages,
|
inserted_index_pages,
|
||||||
@@ -594,20 +603,19 @@ export class IndexBuilder {
|
|||||||
);
|
);
|
||||||
const destPage = pages[destPageIndex];
|
const destPage = pages[destPageIndex];
|
||||||
const destPageRef = (destPage as any).ref;
|
const destPageRef = (destPage as any).ref;
|
||||||
|
const destPageHeight = destPage.getSize().height;
|
||||||
this.add_link_annotation(
|
this.add_link_annotation(
|
||||||
targetPage as any,
|
targetPage as any,
|
||||||
ctx,
|
ctx,
|
||||||
[left, yBottom, width - right, yTop],
|
[xLeft, yBottom, xRight, yTop],
|
||||||
destPageRef,
|
destPageRef,
|
||||||
|
destPageHeight,
|
||||||
);
|
);
|
||||||
entryCursor++;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private async add_index_link_annotations_to_pdf(
|
private async add_index_link_annotations_to_pdf(
|
||||||
pdfDoc: PDFDocument,
|
pdfDoc: PDFDocument,
|
||||||
index_pdf_path: string,
|
|
||||||
sorted_entries: Array<
|
sorted_entries: Array<
|
||||||
[
|
[
|
||||||
string,
|
string,
|
||||||
@@ -616,14 +624,13 @@ export class IndexBuilder {
|
|||||||
>,
|
>,
|
||||||
inserted_index_pages: number,
|
inserted_index_pages: number,
|
||||||
insert_index_after_page: number,
|
insert_index_after_page: number,
|
||||||
|
index_html: string,
|
||||||
) {
|
) {
|
||||||
const index_page_line_counts = await this.get_page_line_counts(
|
const index_item_layouts = await this.get_index_item_layouts(index_html);
|
||||||
index_pdf_path,
|
|
||||||
);
|
|
||||||
this.add_index_link_annotations(
|
this.add_index_link_annotations(
|
||||||
pdfDoc,
|
pdfDoc,
|
||||||
sorted_entries,
|
sorted_entries,
|
||||||
index_page_line_counts,
|
index_item_layouts,
|
||||||
inserted_index_pages,
|
inserted_index_pages,
|
||||||
insert_index_after_page,
|
insert_index_after_page,
|
||||||
);
|
);
|
||||||
@@ -664,9 +671,14 @@ export class IndexBuilder {
|
|||||||
);
|
);
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
const pageRef = (pages[targetPageIndex] as any).ref;
|
const pageRef = (pages[targetPageIndex] as any).ref;
|
||||||
|
const targetPageHeight = pages[targetPageIndex].getSize().height;
|
||||||
const dict = ctx.obj({
|
const dict = ctx.obj({
|
||||||
Title: PDFHexString.fromText(title),
|
Title: PDFHexString.fromText(title),
|
||||||
Dest: [pageRef, PDFName.of("Fit")],
|
Dest: [
|
||||||
|
pageRef,
|
||||||
|
PDFName.of("FitH"),
|
||||||
|
PDFNumber.of(targetPageHeight),
|
||||||
|
],
|
||||||
});
|
});
|
||||||
const ref = ctx.register(dict);
|
const ref = ctx.register(dict);
|
||||||
itemNodes.push({ ref, dict });
|
itemNodes.push({ ref, dict });
|
||||||
|
|||||||
Reference in new issue
Block a user