[208] [57] PDF-Text-Prüfungen: Poppler in der CI installiert, fehlendes Poppler nie still grün (lokal übersprungen, CI rot); Anonymität zusätzlich über die an jsPDF übergebenen Zeichenketten

This commit is contained in:
Claude committed 2026-11-14 15:32:58 +00:00
1 parent 6c7562c351
commit ead1fbc5d2
10 files changed
+98 -20

No files matched your search

+10 -5
View File
@@ -18,7 +18,7 @@ import { exportIfcSpf } from "./exportIfc";
import { exportGlb, exportGltf, exportMtl, exportObj, exportStl } from "./exportMesh";
import { exportScheduleCsv } from "./exportSchedule";
import { exportProjectStep } from "./exportStep";
import { pdfVisibleText } from "./pdfText.testing";
import { captureJsPdfText, pdfVisibleText } from "./pdfText.testing";
import { buildPagesPdf, layoutPages, pdfBytes } from "./layoutPdf";
import { planToSvgString } from "./planToSvgString";
import { areaReportToCsv, buildAreaReport, buildAreaReportPdf } from "./areaReport";
@@ -56,7 +56,7 @@ function project(anonymous: boolean | null): Project {
const latin1 = (b: Uint8Array): string => Buffer.from(b).toString("latin1");
/** Seiteninhalt (Flate-Streams) des PDF entpackt, hintereinander. */
function streamsText(bytes: Uint8Array): string {
function streamsText(bytes: Uint8Array, captured = ""): string {
const all = Buffer.from(bytes);
const text = all.toString("latin1");
let out = "";
@@ -69,7 +69,8 @@ function streamsText(bytes: Uint8Array): string {
// kein Flate-Stream
}
}
return out + "\n" + pdfVisibleText(bytes);
// `captured`: die an jsPDF übergebenen Zeichenketten — gilt auch ohne Poppler (`pdftotext`), das den Glyphen-Text liest.
return out + "\n" + pdfVisibleText(bytes) + "\n" + captured;
}
/** Alle Ausgaben, die ohne Browser/GPU entstehen: Name → Text (Binärformate als latin1). */
@@ -77,19 +78,23 @@ async function allOutputs(p: Project): Promise<Record<string, string>> {
const plan = generatePlan(p, "eg", new Set(flattenCategories(p.layers).map((l) => l.code)));
const out: Record<string, string> = {};
// PDF: Blatt (Info + Seiteninhalt) und Berichte (Info + Inhalt)
const cap = captureJsPdfText();
let mark = cap.mark();
const doc = await buildPagesPdf(p, layoutPages(p, ["l1"]));
const sheet = pdfBytes(p, doc!);
out["PDF-Blatt"] = latin1(sheet);
out["PDF-Blatt-Text"] = streamsText(sheet);
out["PDF-Blatt-Text"] = streamsText(sheet, cap.since(mark));
out["PDF-Blatt-Info"] = pdfInfoText(sheet);
const reports = { Flaechen: buildAreaReport(p, { ivhb: true }), Programm: buildProgramReport(p), Raumbuch: buildRoomBookReport(p), Oeffnungen: buildOpeningReport(p), Bauteile: buildElementReport(p), Kosten: buildCostReport(p) };
for (const [name, report] of Object.entries(reports)) {
out[`CSV-${name}`] = areaReportToCsv(report);
mark = cap.mark();
const bytes = new Uint8Array((await buildAreaReportPdf(report)).output("arraybuffer"));
out[`PDF-${name}`] = latin1(bytes);
out[`PDF-${name}-Text`] = streamsText(bytes);
out[`PDF-${name}-Text`] = streamsText(bytes, cap.since(mark));
out[`PDF-${name}-Info`] = pdfInfoText(bytes);
}
cap.restore();
// IFC
out["IFC4"] = exportIfcSpf(p);
out["IFC2x3"] = exportIfcSpf(p, { schema: "IFC2X3" });
+10 -7
View File
@@ -6,7 +6,7 @@ import type { Layout, Project } from "../model/types";
import { createPosterMaster, posterTemplate } from "../model/posterTemplates";
import { addImage, createLayoutImage } from "../model/layoutImages";
import { JPEG_64X48, PNG_40X30, PNG_40X30_ALPHA } from "../model/layoutImages.testing";
import { pdfVisibleText } from "./pdfText.testing";
import { captureJsPdfText, pdfVisibleText } from "./pdfText.testing";
import { buildPagesPdf, layoutPages, pdfBytes } from "./layoutPdf";
import { pdfInfoText } from "./pdfInfo";
@@ -17,7 +17,7 @@ const latin1 = (b: Uint8Array): string => {
};
/** Alle Flate-Streams des PDF entpackt, hintereinander (Seiteninhalt ist komprimiert). */
function streamsText(bytes: Uint8Array): string {
function streamsText(bytes: Uint8Array, captured = ""): string {
const all = Buffer.from(bytes);
const text = all.toString("latin1");
let out = "";
@@ -30,7 +30,8 @@ function streamsText(bytes: Uint8Array): string {
// kein Flate-Stream (z. B. JPEG)
}
}
return out + "\n" + pdfVisibleText(bytes);
// `captured`: an jsPDF übergebene Zeichenketten — gilt auch ohne Poppler.
return out + "\n" + pdfVisibleText(bytes) + "\n" + captured;
}
function indexOfBytes(hay: Uint8Array, needle: Uint8Array): number {
@@ -70,10 +71,11 @@ describe("Blatt-PDF mit Bild", () => {
it("Blattgrösse A1 quer, Plakat-Kopfzeile mit Kennwort und Plan x/n", async () => {
const p = poster({ competition: { anonymous: false, keyword: "FALKE 7" } });
const doc = await buildPagesPdf(p, layoutPages(p, ["l1"]));
const cap = captureJsPdfText();
const doc = await buildPagesPdf(p, layoutPages(p, ["l1"])).finally(() => cap.restore());
const bytes = pdfBytes(p, doc!);
expect(latin1(bytes)).toMatch(/\/MediaBox \[0 0 2383\.93\d* 1683\.7\d*/); // 841 × 594 mm in pt
const content = streamsText(bytes);
const content = streamsText(bytes, cap.strings());
expect(content).toContain("Kennwort");
expect(content).toContain("FALKE 7");
expect(content).toContain("Plan 1/1");
@@ -81,7 +83,8 @@ describe("Blatt-PDF mit Bild", () => {
it("anonyme Abgabe: keine Büro-/Autor-Angaben, Info nur mit Kennwort, kein jsPDF-Producer", async () => {
const p = poster({ competition: { anonymous: true, keyword: "FALKE 7" } });
const doc = await buildPagesPdf(p, layoutPages(p, ["l1"]));
const cap = captureJsPdfText();
const doc = await buildPagesPdf(p, layoutPages(p, ["l1"])).finally(() => cap.restore());
const bytes = pdfBytes(p, doc!);
const info = pdfInfoText(bytes);
expect(info).toContain("/Title (FALKE 7)");
@@ -89,7 +92,7 @@ describe("Blatt-PDF mit Bild", () => {
expect(info).not.toContain("/Author");
expect(info).not.toContain("/Creator");
expect(info).toContain("D:20000101000000");
const all = latin1(bytes) + streamsText(bytes);
const all = latin1(bytes) + streamsText(bytes, cap.strings());
expect(all).not.toContain("Büro Muster");
expect(all).not.toContain("Maria Muster");
expect(all).not.toContain("Haus Muster");
+3 -3
View File
@@ -10,7 +10,7 @@ import { createPosterMaster, posterTemplate } from "../model/posterTemplates";
import { buildAreaReport, buildAreaReportPdf } from "./areaReport";
import { buildPagesPdf, layoutPages, pdfBytes } from "./layoutPdf";
import { ensurePdfFonts, prepareSvgFonts, resolvePdfFamily, setPdfFont, setPdfFontLoader } from "./pdfFonts";
import { HAVE_POPPLER, pdfVisibleText, withPdfFile } from "./pdfText.testing";
import { RUN_POPPLER_TESTS, pdfVisibleText, withPdfFile } from "./pdfText.testing";
const SPECIAL = "Größe Ø 12 ± 3 m² m³ – „Bürgenstock“ ß Äöü";
const EMB = /\byes\s+(yes|no)\s+(yes|no)\s+\d+\s+\d+\s*$/; // Spalte „emb“ = yes
@@ -64,7 +64,7 @@ describe("PDF-Schriften", () => {
expect(root.getAttribute("font-family")).toBe("Inter");
});
it.runIf(HAVE_POPPLER)("Bericht-PDF: Schrift eingebettet, Sonderzeichen im Text", async () => {
it.runIf(RUN_POPPLER_TESTS)("Bericht-PDF: Schrift eingebettet, Sonderzeichen im Text", async () => {
const report = buildAreaReport(sampleProject, { areas: true });
report.title = SPECIAL;
const doc = await buildAreaReportPdf(report);
@@ -77,7 +77,7 @@ describe("PDF-Schriften", () => {
expect(pdfVisibleText(bytes).replace(/\s+/g, " ")).toContain(SPECIAL);
});
it.runIf(HAVE_POPPLER)("Blatt-PDF: Plakat-Kopfzeile in Inter, Sonderzeichen im Text", async () => {
it.runIf(RUN_POPPLER_TESTS)("Blatt-PDF: Plakat-Kopfzeile in Inter, Sonderzeichen im Text", async () => {
const master = createPosterMaster("m1", posterTemplate("poster-a1-landscape")!);
master.titleBlock = { projectName: SPECIAL, author: "Zoë Müller" };
const lay: Layout = { id: "l1", name: "Grundriss", paper: "a1", orientation: "landscape", masterId: "m1", viewports: [] };
+42
View File
@@ -1,13 +1,54 @@
// Test-Helfer ([208]): sichtbarer Text eines PDFs über `pdftotext` (Poppler). Mit den eingebetteten Schriften steht
// Text nicht mehr als `(Text)` im Inhaltsstrom, sondern als Glyphen-Nummern (Identity-H).
import { jsPDF } from "jspdf";
import { spawnSync } from "node:child_process";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
export const HAVE_POPPLER = spawnSync("pdftotext", ["-v"]).error === undefined && spawnSync("pdffonts", ["-v"]).error === undefined;
/** In der CI muss Poppler da sein: fehlt es, schlägt der Test fehl, statt still zu entfallen. */
export const IN_CI = !!process.env.CI;
/** Für `it.runIf(...)`: Poppler-Tests laufen, wenn es Poppler gibt — oder in der CI (dann scheitern sie hart, s. `needPoppler`). */
export const RUN_POPPLER_TESTS = HAVE_POPPLER || IN_CI;
if (!HAVE_POPPLER && !IN_CI) console.warn("[pdfText] pdftotext/pdffonts (Poppler) fehlt: Text-Prüfungen am PDF werden übersprungen bzw. lassen nur die Prüfung der übergebenen Zeichenketten gelten.");
/** Wirft in der CI, wenn Poppler fehlt (kein stilles Durchlaufen). */
export function needPoppler(): void {
if (!HAVE_POPPLER && IN_CI) throw new Error("pdftotext/pdffonts (poppler-utils) fehlt in der CI — Text-Prüfungen am PDF wären wirkungslos");
}
/**
* Fängt alle Zeichenketten ab, die an `jsPDF.text` gehen — unabhängig von Poppler. Anonymitäts-Prüfungen können so
* auch ohne `pdftotext` den eigentlichen Text prüfen. Wiederherstellen mit `restore()`.
*/
export function captureJsPdfText(): { strings: () => string; mark: () => number; since: (m: number) => string; restore: () => void } {
// jsPDF meldet jeden `text()`-Aufruf (auch aus Plugins wie autoTable) an die Ereignisse `preProcessText`; sie gelten für
// Dokumente, die nach dem Eintragen erzeugt werden.
const events = (jsPDF as unknown as { API: { events: [string, (p: { text: unknown }) => void][] } }).API.events;
const seen: string[] = [];
const entry: [string, (p: { text: unknown }) => void] = [
"preProcessText",
(p) => {
const t = p.text;
if (Array.isArray(t)) seen.push(...t.flat(Infinity).map(String));
else if (t !== undefined && t !== null) seen.push(String(t));
},
];
events.push(entry);
return {
strings: () => seen.join("\n"),
mark: () => seen.length,
since: (m) => seen.slice(m).join("\n"),
restore: () => {
const i = events.indexOf(entry);
if (i >= 0) events.splice(i, 1);
},
};
}
export function withPdfFile<T>(bytes: Uint8Array, fn: (file: string) => T): T {
needPoppler();
const dir = mkdtempSync(join(tmpdir(), "dossier-pdf-"));
try {
const file = join(dir, "t.pdf");
@@ -20,6 +61,7 @@ export function withPdfFile<T>(bytes: Uint8Array, fn: (file: string) => T): T {
/** Sichtbarer Text (Zeilen so, wie sie auf dem Blatt stehen); leer ohne Poppler. */
export function pdfVisibleText(bytes: Uint8Array): string {
needPoppler();
if (!HAVE_POPPLER) return "";
return withPdfFile(bytes, (f) => spawnSync("pdftotext", ["-layout", f, "-"], { encoding: "utf8" }).stdout);
}