新版本

This commit is contained in:
dodgerblue25 committed 2026-10-26 12:49:29 +08:00
commit 742b5bfe2d
509 files changed
+54763

No files matched your search

+182
View File
@@ -0,0 +1,182 @@
import { OpenPGPSigner } from "./pdf_builder/utils.ts";
import { DocuBuilder } from "./pdf_builder/docu_builder.ts";
import { IndexBuilder } from "./pdf_builder/index_builder.ts";
export type GPConfig = {
journal: {
from_directory: string;
template_directory?: string;
build_mode?: "detached" | "flat";
build_js_script?: string | false;
};
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;
};
export async function processPdfConfig(
config: GPConfig,
options: PDFPipelineOptions = {},
) {
const builder = new DocuBuilder(config);
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();
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,
});
const insertedIndexPages = await indexBuilder.combine_index_pdf_to_main_pdf(
tempIndexPdfPath,
insertIndexAfterPage,
);
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}`);
}