201 lines
5.3 KiB
TypeScript
201 lines
5.3 KiB
TypeScript
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}`);
|
|
}
|