done
This commit is contained in:
1 parent
365b1db47e
commit
0161480fa2
79 files changed
+5050
-187
No files matched your search
+86
-10
@@ -1,27 +1,28 @@
|
||||
import { DocuBuilder } from "../pdf_builder/builder.ts";
|
||||
import { DocuBuilder, IndexBuilder } from "../pdf_builder/builder.ts";
|
||||
|
||||
export async function generate_pdf() {
|
||||
const builder = new DocuBuilder();
|
||||
try {
|
||||
await builder.with_browser();
|
||||
const cfg = JSON.parse(await Deno.readTextFile("config.json"));
|
||||
for (const config of cfg.settings) {
|
||||
const builder = new DocuBuilder(config);
|
||||
|
||||
const cfg = JSON.parse(await Deno.readTextFile("config.json"));
|
||||
for (const config of cfg.settings) {
|
||||
try {
|
||||
await builder.with_browser();
|
||||
console.debug(`Processing config: ${JSON.stringify(config)}`);
|
||||
builder.with_files_sync(config.journal.from_directory);
|
||||
builder.as_html_page();
|
||||
await builder.build_as_pdf(config.playwright);
|
||||
} finally {
|
||||
await builder.close_browser();
|
||||
}
|
||||
} finally {
|
||||
await builder.close_browser();
|
||||
}
|
||||
}
|
||||
|
||||
export async function generate_md() {
|
||||
const builder = new DocuBuilder();
|
||||
const cfg = JSON.parse(await Deno.readTextFile("config.json"));
|
||||
try {
|
||||
const cfg = JSON.parse(await Deno.readTextFile("config.json"));
|
||||
for (const config of cfg.settings) {
|
||||
const builder = new DocuBuilder(config);
|
||||
|
||||
console.debug(`Processing config: ${JSON.stringify(config)}`);
|
||||
builder.with_files_sync(config.journal.from_directory);
|
||||
builder.as_markdown();
|
||||
@@ -35,3 +36,78 @@ export async function generate_md() {
|
||||
// No browser to close in this function
|
||||
}
|
||||
}
|
||||
|
||||
export async function add_index_to_pdf(insert_index_after_page: number = 1) {
|
||||
const config = JSON.parse(await Deno.readTextFile("config.json"));
|
||||
for (const cfg of config.settings) {
|
||||
console.log(`Adding index to PDF ${cfg.playwright.path}...`);
|
||||
|
||||
const indexBuilder = new IndexBuilder(
|
||||
cfg.playwright.path,
|
||||
cfg.journal.from_directory,
|
||||
);
|
||||
await indexBuilder.build_index();
|
||||
const _html = indexBuilder.as_html_page();
|
||||
//console.log(html);
|
||||
await indexBuilder.html2pdf({
|
||||
"path": "./tmps/index.pdf",
|
||||
"format": "A4",
|
||||
"printBackground": true,
|
||||
});
|
||||
//console.log(indexBuilder.indexed_map);
|
||||
const insertedIndexPages = await indexBuilder
|
||||
.combine_index_pdf_to_main_pdf(
|
||||
undefined,
|
||||
insert_index_after_page,
|
||||
);
|
||||
await indexBuilder.add_pdf_outline({
|
||||
inserted_index_pages: insertedIndexPages,
|
||||
});
|
||||
|
||||
await Deno.remove("./tmps/index.pdf");
|
||||
}
|
||||
}
|
||||
|
||||
export async function use_qpdf() {
|
||||
const config = JSON.parse(await Deno.readTextFile("config.json"));
|
||||
for (const cfg of config.settings) {
|
||||
if (cfg.pdf.use_qpdf?.enabled) {
|
||||
console.log(`Using qpdf to linearize PDF ${cfg.playwright.path}...`);
|
||||
const outputPath = cfg.playwright.path.replace(
|
||||
/\.pdf$/,
|
||||
"_linearized.pdf",
|
||||
);
|
||||
|
||||
const qpdfArgs = [
|
||||
cfg.playwright.path,
|
||||
cfg.pdf.use_qpdf.options,
|
||||
outputPath,
|
||||
];
|
||||
|
||||
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(cfg.playwright.path);
|
||||
await Deno.rename(outputPath, cfg.playwright.path);
|
||||
console.log(`Successfully linearized PDF: ${cfg.playwright.path}`);
|
||||
} else {
|
||||
console.error(`Failed to linearize PDF: ${cfg.playwright.path}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+36
-4
@@ -1,16 +1,48 @@
|
||||
import { generate_md, generate_pdf } from "./handlers.ts";
|
||||
import {
|
||||
add_index_to_pdf,
|
||||
generate_md,
|
||||
generate_pdf,
|
||||
use_qpdf,
|
||||
} from "./handlers.ts";
|
||||
|
||||
export function match_sub_command_to_function(sub_command: Array<string>) {
|
||||
export async function match_sub_command_to_function(
|
||||
sub_command: Array<string>,
|
||||
options: Record<string, any>,
|
||||
) {
|
||||
switch (sub_command[0]) {
|
||||
case "generate":
|
||||
switch (sub_command[1]) {
|
||||
case "pdf":
|
||||
return generate_pdf;
|
||||
await generate_pdf();
|
||||
if (
|
||||
options.add_index != false && options.add_index != undefined
|
||||
) {
|
||||
console.log(
|
||||
`add_index option is set to ${options.add_index}. Adding index to PDF...`,
|
||||
);
|
||||
if (typeof options.add_index === "boolean") {
|
||||
await add_index_to_pdf();
|
||||
} else if (typeof options.add_index === "number") {
|
||||
await add_index_to_pdf(options.add_index);
|
||||
} else {
|
||||
console.warn(
|
||||
`Invalid value for add_index: ${options.add_index}. Expected boolean or number. Skipping index addition.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.use_qpdf) {
|
||||
await use_qpdf();
|
||||
}
|
||||
break;
|
||||
|
||||
case "md":
|
||||
return generate_md;
|
||||
await generate_md();
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown sub-command: ${sub_command[1]}`);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown sub-command: ${sub_command[0]}`);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user