This commit is contained in:
2026-07-12 22:59:21 -05:00
parent 14699b5f26
commit 70f09fa9da

View File

@@ -47,7 +47,7 @@ interface Args {
api: string; api: string;
limit: number; limit: number;
only: string | null; only: string | null;
dpi: number; dpi: number | "auto";
maxPages: number; maxPages: number;
force: boolean; force: boolean;
outDir: string; outDir: string;
@@ -73,7 +73,10 @@ function parseArgs(): Args {
api, api,
limit: parseInt(get("--limit", "0")!, 10) || 0, limit: parseInt(get("--limit", "0")!, 10) || 0,
only: get("--only"), only: get("--only"),
dpi: parseInt(get("--dpi", "150")!, 10) || 150, dpi: ((): number | "auto" => {
const v = get("--dpi", "auto")!;
return v === "auto" ? "auto" : parseInt(v, 10) || 150;
})(),
maxPages: parseInt(get("--max-pages", "8")!, 10) || 8, maxPages: parseInt(get("--max-pages", "8")!, 10) || 8,
force: a.includes("--force"), force: a.includes("--force"),
outDir: get("--out-dir", path.dirname(input))!, outDir: get("--out-dir", path.dirname(input))!,
@@ -234,6 +237,19 @@ async function renderPdf(pdfPath: string, dpi: number, maxPages: number, tmpDir:
return files; return files;
} }
/** Hat das PDF eine nennenswerte Textebene? (Hybrid: Werte getippt → 150 DPI
* reicht. Reiner Scan: keine Textebene, Handschrift möglich → 200 DPI.) */
async function hasTextLayer(pdfPath: string, maxPages: number): Promise<boolean> {
try {
const { stdout } = await execFileP("pdftotext", ["-l", String(maxPages), pdfPath, "-"], {
timeout: 30_000, maxBuffer: 10 * 1024 * 1024,
});
return stdout.replace(/\s+/g, "").length > 100;
} catch {
return false; // im Zweifel als Scan behandeln → hohe Auflösung
}
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// VLM-Aufruf (llama-server, OpenAI-kompatibel, JSON-Schema) // VLM-Aufruf (llama-server, OpenAI-kompatibel, JSON-Schema)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -481,8 +497,11 @@ async function main(): Promise<void> {
const tmpDir = await fsp.mkdtemp(path.join(os.tmpdir(), "bvs-")); const tmpDir = await fsp.mkdtemp(path.join(os.tmpdir(), "bvs-"));
try { try {
progress(`${tag} … rendere`); const dpi = args.dpi === "auto"
const images = await renderPdf(pdfPath, args.dpi, args.maxPages, tmpDir); ? ((await hasTextLayer(pdfPath, args.maxPages)) ? 150 : 200)
: args.dpi;
progress(`${tag} … rendere (${dpi} dpi)`);
const images = await renderPdf(pdfPath, dpi, args.maxPages, tmpDir);
let vis: VisionRaw | null = null; let vis: VisionRaw | null = null;
let lastErr = ""; let lastErr = "";