Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pdf-service): styling fixes after move to pdfkit #1530

Merged
merged 8 commits into from
Dec 9, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ describe("createChecklistPage", () => {
1,
"Merkblatt: Antrag auf Bewilligung von Beratungshilfe von Luca Mustermensch",
);
expect(mockPDFDocument.text).toHaveBeenNthCalledWith(2, "Anhang");
expect(mockPDFDocument.text).toHaveBeenNthCalledWith(
2,
"Ihre nächsten Schritte",
);
expect(mockPDFDocument.text).toHaveBeenNthCalledWith(
3,
"So schicken Sie den Antrag ins Amtsgericht",
Expand All @@ -42,7 +45,10 @@ describe("createChecklistPage", () => {
1,
"Merkblatt: Antrag auf Bewilligung von Beratungshilfe von Luca Mustermensch",
);
expect(mockPDFDocument.text).toHaveBeenNthCalledWith(2, "Anhang");
expect(mockPDFDocument.text).toHaveBeenNthCalledWith(
2,
"Ihre nächsten Schritte",
);
expect(mockPDFDocument.text).toHaveBeenNthCalledWith(
3,
"So stellen Sie den Antrag online",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,64 @@ import { abgabeContext } from "~/domains/shared/formular/abgabe/context";
import { createChecklistSteps } from "../createChecklistSteps";

describe("createChecklistSteps", () => {
const mockDocumentStruct = mockPdfKitDocumentStructure();
const mockPDFDocument = mockPdfKitDocument(mockDocumentStruct);
mockPDFDocument.x = 0;
mockPDFDocument.y = 0;

it("should create checklist steps for 'ausdrucken' abgabeArt", () => {
const mockDocumentStruct = mockPdfKitDocumentStructure();
const mockPDFDocument = mockPdfKitDocument(mockDocumentStruct);
const userData: BeratungshilfeFormularContext = {
abgabeArt: abgabeContext.abgabeArt.Enum.ausdrucken,
};
createChecklistSteps(mockPDFDocument, mockDocumentStruct, userData);

expect(mockPDFDocument.text).toHaveBeenNthCalledWith(
1,
"1. Antrag ausdrucken",
);
expect(mockPDFDocument.text).toHaveBeenNthCalledWith(
3,
expect(mockPDFDocument.text).toHaveBeenCalledWith("1. Antrag ausdrucken");
expect(mockPDFDocument.text).toHaveBeenCalledWith(
"2. Antrag unterschreiben",
);
expect(mockPDFDocument.text).toHaveBeenNthCalledWith(
5,
expect(mockPDFDocument.text).toHaveBeenCalledWith(
"3. Benötigte Dokumente kopieren",
);
expect(mockPDFDocument.text).toHaveBeenNthCalledWith(
7,
"4. Antrag abgeben",
);
expect(mockPDFDocument.text).toHaveBeenCalledWith("4. Antrag abgeben");
expect(mockPDFDocument.list).toHaveBeenCalledWith(
[
"Unterlagen zu Ihrem rechtlichen Problem",
"Kopie Ihres aktuellen Mietvertrags",
],
expect.any(Number),
expect.any(Number),
expect.any(Object),
Comment on lines +34 to 36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused on what this code is actually asserting? Why do we need these here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pdfs for abgabeArt ausdrucken and online are not the same. They have slightly different content. The test is asserting that some of the relevant content is actually fed into the methods composing the pdf. The two lines you've highlighted are arguments of the list method (x and y position). But only the first argument is relevant, hence the arbitrary expect.any(Number).

);
});

it("should create checklist steps for 'online' abgabeArt", () => {
const mockDocumentStruct = mockPdfKitDocumentStructure();
const mockPDFDocument = mockPdfKitDocument(mockDocumentStruct);
const onlineUserData: BeratungshilfeFormularContext = {
abgabeArt: abgabeContext.abgabeArt.Enum.online,
};

createChecklistSteps(mockPDFDocument, mockDocumentStruct, onlineUserData);

expect(mockPDFDocument.text).toHaveBeenNthCalledWith(
1,
expect(mockPDFDocument.text).toHaveBeenCalledWith(
"1. Antrag prüfen und speichern",
);
expect(mockPDFDocument.text).toHaveBeenNthCalledWith(
3,
expect(mockPDFDocument.text).toHaveBeenCalledWith(
"2. Benötigte Dokumente scannen",
);
expect(mockPDFDocument.text).toHaveBeenNthCalledWith(
5,
expect(mockPDFDocument.text).toHaveBeenCalledWith(
"3. Antrag über das Portal Mein Justizpostfach versenden",
);
expect(mockPDFDocument.list).toHaveBeenCalledWith(
[
"Unterlagen zu Ihrem rechtlichen Problem",
"Kopie Ihres aktuellen Mietvertrags",
],
expect.any(Number),
expect.any(Number),
expect.any(Object),
);
});

it("should include relevant documents based on user data conditions", () => {
const mockDocumentStruct = mockPdfKitDocumentStructure();
const mockPDFDocument = mockPdfKitDocument(mockDocumentStruct);
const customUserData: BeratungshilfeFormularContext = {
abgabeArt: abgabeContext.abgabeArt.Enum.ausdrucken,
staatlicheLeistungen: "buergergeld",
Expand All @@ -94,6 +86,8 @@ describe("createChecklistSteps", () => {
"Kontoauszüge der letzten 3 Monate",
"Kopie des letzten Jahreskontoauszugs für Ihre Lebensversicherung",
]),
expect.any(Number),
expect.any(Number),
expect.any(Object),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const createChecklistPage = (
);
const checklistPageStruct = doc.struct("Sect");

createHeading(doc, checklistPageStruct, "Anhang", "H1");
createHeading(doc, checklistPageStruct, "Ihre nächsten Schritte", "H1");

createHeading(
doc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ export const createChecklistSteps = (
typeof step.value === "string"
? step.value
: step.value(Boolean(conditions.courtName)),
{
indent: pdfStyles.sectionIndented.paddingLeft,
},
doc.x + pdfStyles.sectionIndented.paddingLeft,
doc.y,
)
// workaround, because pdfkit only supports indentation for the first line of a paragraph
.text("", doc.x - pdfStyles.sectionIndented.paddingLeft, doc.y)
.moveDown(1);
}),
);
Expand All @@ -145,8 +146,15 @@ export const createChecklistSteps = (
.font(pdfStyles.page.font)
.list(
relevantDocuments.map((doc) => doc),
{ paragraphGap: 8, indent: pdfStyles.list.paddingLeft },
);
doc.x + pdfStyles.sectionIndented.paddingLeft,
doc.y,
{
paragraphGap: 8,
// the value of bulletIdent does not seem to have any effect
bulletIndent: pdfStyles.list.paddingLeft,
},
)
.text("", doc.x - pdfStyles.sectionIndented.paddingLeft, doc.y);
}),
);
}
Expand Down
13 changes: 11 additions & 2 deletions app/services/pdf/attachment/createAttachmentEntries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,29 @@ export function createAttachmentEntries(
attachment: AttachmentEntries | undefined,
) {
if (attachment) {
attachment.forEach((entry) => {
attachment.forEach((entry, index) => {
documentStruct.add(
doc.struct(entry.level?.toUpperCase() ?? "P", {}, () => {
doc
// only move down if the current entry is a heading
// and the previous entry is not a heading
// and the current entry is not the first entry
.moveDown(
entry.level && !attachment[index - 1]?.level && index !== 0
? 1
: 0,
)
.fontSize(
entry.level
? pdfStyles[entry.level].fontSize
: pdfStyles.page.fontSize,
)
.font(pdfStyles.bold.font)
.text(entry.title)
.moveDown(entry.level ? 0.5 : 0);
.moveDown(entry.level ? 1 : 0);
}),
);

if (entry.text) {
documentStruct.add(
doc.struct("P", {}, () => {
Expand Down
2 changes: 1 addition & 1 deletion app/services/pdf/pdfStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ export const pdfStyles = {
font: fontMap.bold,
},
list: {
paddingLeft: 23,
paddingLeft: 10,
},
};
Loading