增加了自动openpgp签名选项,修改了打印版本的样式

This commit is contained in:
:NoBody: committed 2026-05-08 22:47:21 +08:00
1 parent 2c78a92b25
commit 71b70b3773
51 files changed
+1180 -423

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
./outputs/
private_key.asc
+21 -1
View File
@@ -1,4 +1,8 @@
import { DocuBuilder, IndexBuilder } from "../pdf_builder/builder.ts";
import {
DocuBuilder,
IndexBuilder,
OpenPGPSigner,
} from "../pdf_builder/builder.ts";
export async function generate_pdf() {
const cfg = JSON.parse(await Deno.readTextFile("config.json"));
@@ -111,3 +115,19 @@ export async function use_qpdf() {
}
}
}
export async function sign_pdf() {
const config = JSON.parse(await Deno.readTextFile("config.json"));
for (const cfg of config.settings) {
if (cfg.pdf.signing?.enabled) {
console.log(`Signing PDF ${cfg.playwright.path}...`);
const signer = new OpenPGPSigner(
cfg.playwright.path,
cfg.pdf.signing.key_path,
cfg.pdf.signing.output_path,
);
await signer.sign();
console.log(`Successfully signed PDF: ${cfg.pdf.signing.output_path}`);
}
}
}
+8
View File
@@ -1,7 +1,9 @@
import { OpenPGPSigner } from "../pdf_builder/builder.ts";
import {
add_index_to_pdf,
generate_md,
generate_pdf,
sign_pdf,
use_qpdf,
} from "./handlers.ts";
@@ -34,6 +36,12 @@ export async function match_sub_command_to_function(
if (options.use_qpdf) {
await use_qpdf();
}
if (options.sign_pdf) {
console.log("Signing PDF...");
await sign_pdf();
}
break;
case "md":
+16
View File
@@ -10,6 +10,11 @@
"use_qpdf": {
"enabled": true,
"options": ["--linearize"]
},
"signing": {
"enabled": true,
"key_path": "./private_key.asc",
"output_path": "./outputs/mobile.pdf.sig"
}
},
@@ -39,6 +44,11 @@
"use_qpdf": {
"enabled": true,
"options": ["--linearize"]
},
"signing": {
"enabled": true,
"key_path": "./private_key.asc",
"output_path": "./outputs/computer.pdf.sig"
}
},
@@ -69,6 +79,11 @@
"use_qpdf": {
"enabled": true,
"options": [""]
},
"signing": {
"enabled": true,
"key_path": "./private_key.asc",
"output_path": "./outputs/printable.pdf.sig"
}
},
@@ -78,6 +93,7 @@
"printBackground": true,
"displayHeaderFooter": true,
"margin": {
"top": "15mm",
"bottom": "15mm"
},
"headerTemplate": "<div><span class=\"head\"></span></div>",
+1
View File
@@ -6,6 +6,7 @@
"@std/assert": "jsr:@std/assert@1",
"@std/toml": "jsr:@std/toml@^1.0.11",
"markdown-it": "npm:markdown-it@^14.1.1",
"openpgp": "npm:openpgp@^6.3.0",
"pdf-lib": "npm:pdf-lib@^1.17.1",
"pdf-parse": "npm:pdf-parse@^2.4.5",
"playwright": "npm:playwright@^1.59.1",
+5
View File
@@ -6,6 +6,7 @@
"jsr:@std/internal@^1.0.12": "1.0.12",
"jsr:@std/toml@^1.0.11": "1.0.11",
"npm:markdown-it@^14.1.1": "14.1.1",
"npm:openpgp@^6.3.0": "6.3.0",
"npm:pdf-lib@^1.17.1": "1.17.1",
"npm:pdf-parse@^2.4.5": "2.4.5",
"npm:playwright@^1.59.1": "1.59.1",
@@ -144,6 +145,9 @@
"[email protected]": {
"integrity": "sha512-Lf+9+2r+Tdp5wXDXC4PcIBjTDtq4UKjCPMQhKIuzpJNW0b96kVqSwW0bT7FhRSfmAiFYgP+SCRvdrDozfh0U5w=="
},
"[email protected]": {
"integrity": "sha512-pLzCU8IgyKXPSO11eeharQkQ4GzOKNWhXq79pQarIRZEMt1/ssyr+MIuWBv1mNoenJLg04gvPx+fi4gcKZ4bag=="
},
"[email protected]": {
"integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw=="
},
@@ -241,6 +245,7 @@
"jsr:@std/assert@1",
"jsr:@std/toml@^1.0.11",
"npm:markdown-it@^14.1.1",
"npm:openpgp@^6.3.0",
"npm:pdf-lib@^1.17.1",
"npm:pdf-parse@^2.4.5",
"npm:playwright@^1.59.1",
+16 -1
View File
@@ -24,10 +24,25 @@ import { PDFParse } from "pdf-parse";
// }
// });
// Deno.test({
// name: "IndexBuilder test",
// sanitizeOps: false,
// sanitizeResources: false,
// async fn() {
// },
// });
Deno.test({
name: "IndexBuilder test",
name: "OpenPGPSigner test",
sanitizeOps: false,
sanitizeResources: false,
async fn() {
const { OpenPGPSigner } = await import("./pdf_builder/builder.ts");
const signer = new OpenPGPSigner(
"./outputs/mobile.pdf",
"./private_key.asc",
"./outputs/mobile.pdf.sig",
);
await signer.sign();
},
});
+2 -2
View File
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:3883185177f823de95d23a6efc85117b76f26581746605d2ee67bb2c203c2528
size 10087670
oid sha256:ff2451c815f89662cbce4855841e60e7f943a834f559d62df7b182e70162a19f
size 17018877
+9
View File
@@ -0,0 +1,9 @@
-----BEGIN PGP SIGNATURE-----
wrsEABYKAG0Fgmn99hgJEAYZg7eHStDTRRQAAAAAABwAIHNhbHRAbm90YXRp
b25zLm9wZW5wZ3Bqcy5vcme9J20+lO53nsP34Lfva4ry27H1UKGRGKhEvFSA
/e0OGhYhBPS+eiDpODeqSiF8eQYZg7eHStDTAAAwpAD/S2Ks1m+rR+qNb96t
DRR9h3YGCnBo5Bq59lEv181eK84BAON6knpCTHixDUZfkfVe8xFnH37FanYf
KhTGF8mo3yYE
=Obx7
-----END PGP SIGNATURE-----
+9
View File
@@ -0,0 +1,9 @@
-----BEGIN PGP SIGNATURE-----
wrsEABYKAG0Fgmn99hgJEAYZg7eHStDTRRQAAAAAABwAIHNhbHRAbm90YXRp
b25zLm9wZW5wZ3Bqcy5vcmfOYe8+whjw/NFwKlv932HKp+3r2U9FsAcAj4xE
6Q1fJhYhBPS+eiDpODeqSiF8eQYZg7eHStDTAABk4AEA1BORmJ5+cPtLcQfd
XKcZ6UFOLPp/za9Fqi/XKCeVRx0A/0sWLimU+nV36VVJMHZfO/Krwe1k+vnH
yU78COAkbbkD
=McFY
-----END PGP SIGNATURE-----
+2 -2
View File
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:fb0761b00666703d6c52ce26c5e4ea0800f26e0d6afb4d64bf61a935bdc4c2b5
size 4123530
oid sha256:4922f39d6b75744653c8b04396b20eb27929b5e591b7aeba14935a1e8552c21b
size 14128313
+9
View File
@@ -0,0 +1,9 @@
-----BEGIN PGP SIGNATURE-----
wrsEABYKAG0Fgmn99hgJEAYZg7eHStDTRRQAAAAAABwAIHNhbHRAbm90YXRp
b25zLm9wZW5wZ3Bqcy5vcmdYOf9P7zqvoEY74JWz9WAKgusPf6yks+3I/Lqq
2sDVdxYhBPS+eiDpODeqSiF8eQYZg7eHStDTAACciwD8CvzkA1oKNC8wyW0p
HVNmbbD0GNls7pz3dKh4hRxhMCQBAMfhCr7OAuiE+BdXfmoQLblISpeRdsm4
st3ElVysqH4N
=T9g0
-----END PGP SIGNATURE-----
+47 -5
View File
@@ -4,7 +4,9 @@ import MarkdownIt from "markdown-it";
import TurndownService from "turndown";
import { PDFParse } from "pdf-parse";
import { PDFDocument, PDFHexString, PDFName } from "pdf-lib";
import "openpgp";
import * as openpgp from "openpgp";
import { PathLike } from "node:fs";
function resolveChromiumPath(): string {
const chromiumPath = Deno.env.get("CHROMIUM_PATH");
if (chromiumPath) {
@@ -88,26 +90,40 @@ export class DocuBuilder {
const html = this.pages.join("\n");
// Ensure a DOMParser is available; if not, try to load deno_dom dynamically.
let domParserCtor = (globalThis as {
DOMParser?: new () => {
let domParserCtor: {
new (): {
parseFromString: (
html: string,
mimeType: string,
) => { documentElement?: { outerHTML: string } } | null;
};
}).DOMParser;
} | undefined = (globalThis as { DOMParser?: unknown }).DOMParser as
| {
new (): {
parseFromString: (
html: string,
mimeType: string,
) => { documentElement?: { outerHTML: string } } | null;
};
}
| undefined;
if (domParserCtor == null) {
try {
const denoDom = await import(
// deno-lint-ignore no-import-prefix
"https://deno.land/x/deno_dom/deno-dom-wasm.ts"
);
if (denoDom && denoDom.DOMParser) {
// deno-lint-ignore no-explicit-any
(globalThis as any).DOMParser = denoDom.DOMParser;
domParserCtor = denoDom.DOMParser;
// Cast via unknown to satisfy TypeScript when converting between incompatible constructor types
domParserCtor = denoDom
.DOMParser as unknown as typeof domParserCtor;
}
} catch (e) {
// ignore; will throw below with helpful message
console.error(e);
}
}
@@ -419,3 +435,29 @@ export class IndexBuilder {
await Deno.writeFile(this.file_path, outlinedPdfBytes);
}
}
export class OpenPGPSigner {
file_path: PathLike;
output_path: PathLike;
pri_key: Promise<openpgp.PrivateKey>;
constructor(file: PathLike, pri_key: PathLike, output_path: PathLike) {
this.file_path = file;
this.output_path = output_path;
this.pri_key = openpgp.readPrivateKey({
armoredKey: Deno.readTextFileSync(pri_key as string),
});
}
async sign() {
const file_bytes = await Deno.readFile(this.file_path as string);
const message = await openpgp.createMessage({ binary: file_bytes });
const signature = await openpgp.sign({
message,
signingKeys: await this.pri_key,
format: "armored",
detached: true,
});
await Deno.writeTextFile(this.output_path as string, signature);
}
}
+5 -6
View File
@@ -4,8 +4,8 @@
.global {
font-family: "R-PMingLiU", sans-serif;
font-size: var(--font-size);
margin: 1rem 2rem 2rem 2rem;
line-height: 16px;
margin: 1rem 4rem 2rem 4rem;
line-height: 20px;
/* letter-spacing: -0.15rem; */
}
@@ -50,8 +50,8 @@
height: 100%;
object-fit: scale-down;
max-width: 100%;
max-height: 25cm;
max-width: 45%;
max-height: 10cm;
border-radius: 12px;
border: 4px solid linear-gradient(45deg, #e799b1, #44b144, #2f90ba);
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
@@ -69,8 +69,7 @@
/*目录美化部分*/
index h1 {
font-size: 1rem;
color: white;
background-color: red;
color: black;
border: 0.18rem solid red;
display: inline-flex;
width: fit-content;