Skip to content

Commit

Permalink
feat(pdf-service): append hinweisblatt to pkh pdf (#1518)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbrtrfl authored Dec 5, 2024
1 parent 7bb5893 commit 3e3d6b5
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/domains/beratungshilfe/services/pdf/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function beratungshilfePdfFromUserdata(
const filledPdfFormDocument = await fillPdf({
flowId: "/beratungshilfe/antrag",
pdfValues,
yPositionsDruckvermerk: [90, 108, 138],
yPositionsDruckvermerk: [90, 108, 138], // Different y positions because the form boxes jump for each page
xPositionsDruckvermerk: 28,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fs from "node:fs";
import path from "node:path";
import { describe, it, expect } from "vitest";

describe("loadHinweisblatt", () => {
it("should fail when the location of the PDF file has changed", () => {
const originalPath = path.resolve(
path.join(
process.cwd(),
"data/pdf/",
"prozesskostenhilfe",
"Formular_Hinweisblatt_Prozess_Verfahrenskostenhilfe.pdf",
),
);

const fileExists = fs.existsSync(originalPath);
expect(fileExists).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { PDFDocument } from "pdf-lib";
import type { ProzesskostenhilfeFormularContext } from "~/domains/prozesskostenhilfe/formular";
import { addMetadataToPdf } from "~/services/pdf/addMetadataToPdf";
import { appendPagesToPdf } from "~/services/pdf/appendPagesToPdf";
import { pdfFillReducer } from "~/services/pdf/fillOutFunction";
import { fillPdf } from "~/services/pdf/fillPdf.server";
import { pdfFromUserData } from "~/services/pdf/pdfFromUserData";
import { prozesskostenhilfePdfFromUserdata } from "..";
import loadHinweisblatt from "../loadHinweisblatt";

vi.mock("~/services/pdf/fillOutFunction", () => ({
pdfFillReducer: vi.fn(),
}));
vi.mock("~/services/pdf/fillPdf.server", () => ({
fillPdf: vi.fn(),
}));
vi.mock("~/services/pdf/addMetadataToPdf", () => ({
addMetadataToPdf: vi.fn(),
}));
vi.mock("~/services/pdf/appendPagesToPdf", () => ({
appendPagesToPdf: vi.fn(),
}));
vi.mock("~/services/pdf/pdfFromUserData", () => ({
pdfFromUserData: vi.fn(),
}));
vi.mock("../loadHinweisblatt", () => ({
default: vi.fn(),
}));

const userData: ProzesskostenhilfeFormularContext = {
vorname: "Angelika",
nachname: "M",
};

describe("prozesskostenhilfePdfFromUserdata", () => {
beforeEach(() => {
vi.resetAllMocks();
});

it("should call all expected functions", async () => {
const mockPdfDocument = await PDFDocument.create();
const pdfBytes = await mockPdfDocument.save();
const mockPdfBuffer = Buffer.from(pdfBytes);
const mockUint8Array = new Uint8Array(pdfBytes);

vi.mocked(pdfFillReducer).mockReturnValue({
pdfValues: {},
attachment: [],
});
vi.mocked(fillPdf).mockResolvedValue(mockPdfDocument);
vi.mocked(addMetadataToPdf).mockReturnValue(mockPdfDocument);
vi.mocked(pdfFromUserData).mockResolvedValue(mockPdfBuffer);
vi.mocked(appendPagesToPdf).mockResolvedValue(mockUint8Array);
vi.mocked(loadHinweisblatt).mockResolvedValue(mockPdfDocument);

await prozesskostenhilfePdfFromUserdata(userData);

expect(pdfFillReducer).toHaveBeenCalled();
expect(fillPdf).toHaveBeenCalled();
expect(addMetadataToPdf).toHaveBeenCalled();
expect(appendPagesToPdf).toHaveBeenCalled();
expect(loadHinweisblatt).toHaveBeenCalled();
});
});
17 changes: 14 additions & 3 deletions app/domains/prozesskostenhilfe/services/pdf/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
pdfFromUserData,
type PDFDocumentBuilder,
} from "~/services/pdf/pdfFromUserData";
import loadHinweisblatt from "./loadHinweisblatt";
import { fillPerson } from "./pdfForm/A_person";
import { fillRechtsschutzversicherung } from "./pdfForm/B_rechtsschutzversicherung";
import { fillUnterhaltsanspruch } from "./pdfForm/C_unterhaltspflichtige_person";
Expand Down Expand Up @@ -85,11 +86,13 @@ export async function prozesskostenhilfePdfFromUserdata(
],
});

const xPositionDruckvermerk = 9;

const filledPdfFormDocument = await fillPdf({
flowId: "/prozesskostenhilfe/formular",
pdfValues,
yPositionsDruckvermerk: [43, 51, 40, 44],
xPositionsDruckvermerk: 9,
yPositionsDruckvermerk: [43, 51, 40, 44], // Different y positions because the form boxes jump for each page
xPositionsDruckvermerk: xPositionDruckvermerk,
});

printNameInSignatureFormField(filledPdfFormDocument, userData);
Expand All @@ -108,8 +111,16 @@ export async function prozesskostenhilfePdfFromUserdata(

const mainPdfDocument = await PDFDocument.load(pdfKitBuffer);

return appendPagesToPdf(filledPdfFormDocumentWithMetadata, mainPdfDocument);
await appendPagesToPdf(filledPdfFormDocumentWithMetadata, mainPdfDocument);
}

await appendPagesToPdf(
filledPdfFormDocumentWithMetadata,
await loadHinweisblatt({
yPositionDruckvermerk: 44,
xPositionDruckvermerk,
}),
);

return filledPdfFormDocumentWithMetadata.save();
}
27 changes: 27 additions & 0 deletions app/domains/prozesskostenhilfe/services/pdf/loadHinweisblatt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import fs from "node:fs";
import path from "node:path";
import { PDFDocument } from "pdf-lib";
import { addDruckvermerk } from "~/services/pdf/druckvermerk";

export default async function loadHinweisblatt({
yPositionDruckvermerk,
xPositionDruckvermerk,
}: {
yPositionDruckvermerk: number;
xPositionDruckvermerk: number;
}) {
const filepath = path.resolve(
path.join(
process.cwd(),
"data/pdf/",
"prozesskostenhilfe",
"Formular_Hinweisblatt_Prozess_Verfahrenskostenhilfe.pdf",
),
);

const pdfDoc = await PDFDocument.load(fs.readFileSync(filepath));

addDruckvermerk(pdfDoc, yPositionDruckvermerk, xPositionDruckvermerk);

return pdfDoc;
}
Binary file not shown.

0 comments on commit 3e3d6b5

Please sign in to comment.