This commit is contained in:
1 parent
5977b1b272
commit
09af3bd8c0
18 files changed
+57
-30
No files matched your search
@@ -1,14 +1,14 @@
|
||||
import { Browser } from "playwright";
|
||||
import { resolve } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { PFiles } from "../file.ts";
|
||||
import { PFiles } from "../publication/file.ts";
|
||||
import { md } from "../mdit/md.ts";
|
||||
import TurndownService from "turndown";
|
||||
|
||||
import "openpgp";
|
||||
|
||||
import { resolveChromiumPath } from "./utils.ts";
|
||||
import { renderTemplate, type TemplateVariables } from "../template_engine.ts";
|
||||
import { renderTemplate, type TemplateVariables } from "../template/engine.ts";
|
||||
|
||||
//原文->中间体(html)->目标格式(pdf/md/...)
|
||||
//生成pdf的流程:原文->中间体(html)->playwright渲染->pdf
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { PFiles } from "../file.ts";
|
||||
import { PFiles } from "../publication/file.ts";
|
||||
import { PDFParse } from "pdf-parse";
|
||||
import {
|
||||
PDFArray,
|
||||
@@ -168,10 +168,14 @@ export class IndexBuilder {
|
||||
});
|
||||
}
|
||||
}
|
||||
const missingTitles = titles.filter((title) => !this.indexed_map.has(title));
|
||||
const missingTitles = titles.filter((title) =>
|
||||
!this.indexed_map.has(title)
|
||||
);
|
||||
if (missingTitles.length > 0) {
|
||||
throw new Error(
|
||||
`Index coverage incomplete (${this.indexed_map.size}/${titles.length}); missing: ${missingTitles.join("、")}`,
|
||||
`Index coverage incomplete (${this.indexed_map.size}/${titles.length}); missing: ${
|
||||
missingTitles.join("、")
|
||||
}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
import { OpenPGPSigner } from "./utils.ts";
|
||||
import { DocuBuilder } from "./docu_builder.ts";
|
||||
import { IndexBuilder } from "./index_builder.ts";
|
||||
import type { TemplateVariables } from "../template/engine.ts";
|
||||
|
||||
export type GPConfig = {
|
||||
journal: {
|
||||
from_directory: string;
|
||||
template_directory?: string;
|
||||
build_mode?: "detached" | "flat";
|
||||
build_js_script?: string | false;
|
||||
};
|
||||
asset_base?: string;
|
||||
pdf: {
|
||||
generate_index?: boolean;
|
||||
use_qpdf?: {
|
||||
enabled?: boolean;
|
||||
options?: string;
|
||||
};
|
||||
signing?: {
|
||||
enabled?: boolean;
|
||||
key_path: string;
|
||||
output_path: string;
|
||||
};
|
||||
};
|
||||
playwright: Record<string, unknown> & { path: string };
|
||||
md?: {
|
||||
path?: string;
|
||||
};
|
||||
markdown_output?: string;
|
||||
};
|
||||
|
||||
export type PDFPipelineOptions = {
|
||||
addIndex?: boolean | number;
|
||||
useQpdf?: boolean;
|
||||
signPdf?: boolean;
|
||||
signPassphrase?: string;
|
||||
keepIndexPdf?: boolean;
|
||||
variables?: TemplateVariables;
|
||||
};
|
||||
|
||||
export async function processPdfConfig(
|
||||
config: GPConfig,
|
||||
options: PDFPipelineOptions = {},
|
||||
) {
|
||||
const builder = new DocuBuilder(config, options.variables);
|
||||
|
||||
try {
|
||||
await builder.with_browser();
|
||||
console.debug(`Processing config: ${JSON.stringify(config)}`);
|
||||
builder.with_files_sync(config.journal.from_directory);
|
||||
await builder.as_html_page();
|
||||
await builder.build_as_pdf(config.playwright);
|
||||
} finally {
|
||||
await builder.close_browser();
|
||||
}
|
||||
|
||||
const shouldAddIndex = options.addIndex ?? config.pdf.generate_index;
|
||||
if (shouldAddIndex) {
|
||||
const insertIndexAfterPage = typeof shouldAddIndex === "number"
|
||||
? shouldAddIndex
|
||||
: 1;
|
||||
await addIndexToPdf(
|
||||
config,
|
||||
insertIndexAfterPage,
|
||||
options.keepIndexPdf ?? false,
|
||||
);
|
||||
}
|
||||
|
||||
if (options.useQpdf ?? config.pdf.use_qpdf?.enabled) {
|
||||
await runQpdf(config);
|
||||
}
|
||||
|
||||
if (options.signPdf ?? config.pdf.signing?.enabled) {
|
||||
await signPdf(config, options.signPassphrase);
|
||||
}
|
||||
}
|
||||
|
||||
async function addIndexToPdf(
|
||||
config: GPConfig,
|
||||
insertIndexAfterPage = 1,
|
||||
keepIndexPdf = false,
|
||||
) {
|
||||
console.log(`Adding index to PDF ${config.playwright.path}...`);
|
||||
|
||||
const indexBuilder = new IndexBuilder(config.playwright.path, config);
|
||||
await indexBuilder.build_index();
|
||||
console.log(
|
||||
`Index entries for ${config.playwright.path}: ${indexBuilder.indexed_map.size}`,
|
||||
);
|
||||
if (indexBuilder.indexed_map.size === 0) {
|
||||
console.warn(`Skipping empty index for ${config.playwright.path}`);
|
||||
return;
|
||||
}
|
||||
const tempDir = await makePdfTempDir(config.playwright.path);
|
||||
const tempIndexPdfPath = `${tempDir}/index.pdf`;
|
||||
await indexBuilder.html2pdf({
|
||||
path: tempIndexPdfPath,
|
||||
format: "A4",
|
||||
headerTemplate:
|
||||
'<div style="width:100%; font-size:10px; text-align:center; color:#666;"><span class="title"></span></div>',
|
||||
footerTemplate:
|
||||
'<div style="width:100%; font-size:10px; text-align:center; color:#666;"><span class="title"></span></div>',
|
||||
margin: {
|
||||
top: "20mm",
|
||||
bottom: "20mm",
|
||||
},
|
||||
printBackground: true,
|
||||
});
|
||||
let insertedIndexPages: number;
|
||||
try {
|
||||
insertedIndexPages = await indexBuilder.combine_index_pdf_to_main_pdf(
|
||||
tempIndexPdfPath,
|
||||
insertIndexAfterPage,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Retained failing index PDF for diagnosis: ${tempIndexPdfPath}`,
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
await indexBuilder.add_pdf_outline({
|
||||
inserted_index_pages: insertedIndexPages,
|
||||
});
|
||||
|
||||
if (keepIndexPdf) {
|
||||
console.log(`Kept index PDF for debugging: ${tempIndexPdfPath}`);
|
||||
} else {
|
||||
try {
|
||||
await Deno.remove(tempIndexPdfPath);
|
||||
await Deno.remove(tempDir);
|
||||
} catch (_error) {
|
||||
// temp file may already be gone if the pipeline aborted late
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function makePdfTempDir(outputPath: string) {
|
||||
await Deno.mkdir("./tmps", { recursive: true });
|
||||
const safeStem = outputPath.split("/").pop()?.replace(/\.pdf$/i, "") ?? "pdf";
|
||||
return await Deno.makeTempDir({
|
||||
dir: "./tmps",
|
||||
prefix: `${safeStem}-index-`,
|
||||
});
|
||||
}
|
||||
|
||||
async function runQpdf(config: GPConfig) {
|
||||
if (!config.pdf.use_qpdf?.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Using qpdf to linearize PDF ${config.playwright.path}...`);
|
||||
const outputPath = config.playwright.path.replace(
|
||||
/\.pdf$/,
|
||||
"_linearized.pdf",
|
||||
);
|
||||
const qpdfArgs = [
|
||||
config.playwright.path,
|
||||
config.pdf.use_qpdf.options ?? "",
|
||||
outputPath,
|
||||
].filter((arg) => arg.length > 0);
|
||||
|
||||
const cmd = new Deno.Command("qpdf", {
|
||||
args: qpdfArgs,
|
||||
stdout: "piped",
|
||||
stderr: "piped",
|
||||
});
|
||||
|
||||
const child = cmd.spawn();
|
||||
|
||||
for await (const chunk of child.stdout) {
|
||||
console.log(new TextDecoder().decode(chunk));
|
||||
}
|
||||
|
||||
const status = await child.status;
|
||||
console.log(`Status: ${status.code}`);
|
||||
if (status.code === 0) {
|
||||
await Deno.remove(config.playwright.path);
|
||||
await Deno.rename(outputPath, config.playwright.path);
|
||||
console.log(`Successfully linearized PDF: ${config.playwright.path}`);
|
||||
} else {
|
||||
console.error(`Failed to linearize PDF: ${config.playwright.path}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function signPdf(config: GPConfig, passphrase?: string) {
|
||||
if (!config.pdf.signing?.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Signing PDF ${config.playwright.path}...`);
|
||||
const signer = new OpenPGPSigner(
|
||||
config.playwright.path,
|
||||
config.pdf.signing.key_path,
|
||||
config.pdf.signing.output_path,
|
||||
passphrase,
|
||||
);
|
||||
await signer.sign();
|
||||
console.log(`Successfully signed PDF: ${config.pdf.signing.output_path}`);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { type GPConfig, processPdfConfig } from "./pipeline.ts";
|
||||
import type { TemplateVariables } from "../template/engine.ts";
|
||||
|
||||
type WorkerRequest = {
|
||||
id: number;
|
||||
config: GPConfig;
|
||||
options: {
|
||||
addIndex?: boolean | number;
|
||||
useQpdf?: boolean;
|
||||
signPdf?: boolean;
|
||||
signPassphrase?: string;
|
||||
keepIndexPdf?: boolean;
|
||||
variables?: TemplateVariables;
|
||||
};
|
||||
};
|
||||
|
||||
type WorkerResponse =
|
||||
| { id: number; ok: true }
|
||||
| { id: number; ok: false; error: string };
|
||||
|
||||
const workerGlobal = self as unknown as {
|
||||
onmessage: ((event: MessageEvent<WorkerRequest>) => void) | null;
|
||||
postMessage: (message: WorkerResponse) => void;
|
||||
};
|
||||
|
||||
workerGlobal.onmessage = async (event: MessageEvent<WorkerRequest>) => {
|
||||
const { id, config, options } = event.data;
|
||||
try {
|
||||
await processPdfConfig(config, options);
|
||||
workerGlobal.postMessage({ id, ok: true } satisfies WorkerResponse);
|
||||
} catch (error) {
|
||||
workerGlobal.postMessage(
|
||||
{
|
||||
id,
|
||||
ok: false,
|
||||
error: error instanceof Error ? error.stack ?? error.message : String(
|
||||
error,
|
||||
),
|
||||
} satisfies WorkerResponse,
|
||||
);
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user