Skip to content

Commit

Permalink
Fix pdf space table (#1465)
Browse files Browse the repository at this point in the history
* refactor(createPdfKitDocument): create variable to define pdf width size

* refactor(addTable): reset the doc.y after print table

* refactor(addTableInfo): print table info data with the current doc.y

* refactor: print other details and distance info data with the current doc.y

* refactor(addNewPageInCaseMissingVerticalSpace): decrease minimal space to jump to a new page

* test: update tests

* refactor(getStartYPosition): delete unneeded file
  • Loading branch information
aaschlote authored Nov 22, 2024
1 parent 98b1211 commit 4d33860
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type PDFDocument from "pdfkit";
import { PDF_HEIGHT_SEIZE } from "~/services/pdf/createPdfKitDocument";

const MAX_VERTICAL_SPACE = PDF_HEIGHT_SEIZE - 90;
export const MAX_VERTICAL_SPACE = PDF_HEIGHT_SEIZE - 50;

export const addNewPageInCaseMissingVerticalSpace = (
doc: typeof PDFDocument,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("addCompensationAmount", () => {
const mockStruct = mockPdfKitDocumentStructure();
const mockDoc = mockPdfKitDocument(mockStruct);

addCompensationAmount(mockDoc, mockStruct, userDataMock, 0);
addCompensationAmount(mockDoc, mockStruct, userDataMock);

expect(addOtherDetailsItinerary).toBeCalledTimes(1);
});
Expand All @@ -50,7 +50,7 @@ describe("addCompensationAmount", () => {
const mockStruct = mockPdfKitDocumentStructure();
const mockDoc = mockPdfKitDocument(mockStruct);

addCompensationAmount(mockDoc, mockStruct, userDataMock, 0);
addCompensationAmount(mockDoc, mockStruct, userDataMock);

expect(addDistanceInfo).toBeCalledTimes(1);
});
Expand All @@ -59,7 +59,7 @@ describe("addCompensationAmount", () => {
const mockStruct = mockPdfKitDocumentStructure();
const mockDoc = mockPdfKitDocument(mockStruct);

addCompensationAmount(mockDoc, mockStruct, userDataMock, 0);
addCompensationAmount(mockDoc, mockStruct, userDataMock);

expect(mockDoc.text).toHaveBeenCalledWith(
DEMANDED_COMPENSATION_PAYMENT_TEXT,
Expand All @@ -75,7 +75,7 @@ describe("addCompensationAmount", () => {
isWeiterePersonen: YesNoAnswer.Enum.yes,
};

addCompensationAmount(mockDoc, mockStruct, userDataWeiterePersonenMock, 0);
addCompensationAmount(mockDoc, mockStruct, userDataWeiterePersonenMock);

expect(mockDoc.text).toHaveBeenCalledWith(
OTHER_PASSENGERS_DEMANDED_COMPENSATION_PAYMENT_TEXT,
Expand All @@ -86,7 +86,7 @@ describe("addCompensationAmount", () => {
const mockStruct = mockPdfKitDocumentStructure();
const mockDoc = mockPdfKitDocument(mockStruct);

addCompensationAmount(mockDoc, mockStruct, userDataMock, 0);
addCompensationAmount(mockDoc, mockStruct, userDataMock);

expect(addNewPageInCaseMissingVerticalSpace).toBeCalledTimes(1);
});
Expand All @@ -95,7 +95,7 @@ describe("addCompensationAmount", () => {
const mockStruct = mockPdfKitDocumentStructure();
const mockDoc = mockPdfKitDocument(mockStruct);

addCompensationAmount(mockDoc, mockStruct, userDataMock, 0);
addCompensationAmount(mockDoc, mockStruct, userDataMock);

expect(addMultiplePersonsInfo).toBeCalledTimes(1);
});
Expand All @@ -104,7 +104,7 @@ describe("addCompensationAmount", () => {
const mockStruct = mockPdfKitDocumentStructure();
const mockDoc = mockPdfKitDocument(mockStruct);

addCompensationAmount(mockDoc, mockStruct, userDataMock, 0);
addCompensationAmount(mockDoc, mockStruct, userDataMock);

expect(addWitnessesInfo).toBeCalledTimes(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ describe("addDistanceInfo", () => {
isWeiterePersonen: YesNoAnswer.Enum.no,
};

addDistanceInfo(mockDoc, userDataWeiterePersonenMock, 0);
addDistanceInfo(mockDoc, userDataWeiterePersonenMock);

expect(mockDoc.text).toHaveBeenCalledWith(
`Die Distanz zwischen ${startAirportMock} und ${endAirportMock} beträgt nach Großkreismethode ca. ${distanceValueMock} km. ${ARTICLE_AIR_PASSENGER_REGULATION_TEXT} ${compensationValueMock} €.`,
PDF_MARGIN_HORIZONTAL,
undefined,
);
});

Expand All @@ -81,20 +80,19 @@ describe("addDistanceInfo", () => {
isWeiterePersonen: YesNoAnswer.Enum.yes,
};

addDistanceInfo(mockDoc, userDataWeiterePersonenMock, 0);
addDistanceInfo(mockDoc, userDataWeiterePersonenMock);

expect(mockDoc.text).toHaveBeenCalledWith(
`Die Distanz zwischen ${startAirportMock} und ${endAirportMock} beträgt nach Großkreismethode ca. ${distanceValueMock} km. ${ARTICLE_AIR_PASSENGER_REGULATION_TEXT} ${compensationValueMock} € pro Person, insgesamt aus eigenem und abgetretenem Recht damit eine Gesamtsumme von ${compensationValueMock} €.`,
PDF_MARGIN_HORIZONTAL,
undefined,
);
});

it("should call addNewPageInCaseMissingVerticalSpace", () => {
const mockStruct = mockPdfKitDocumentStructure();
const mockDoc = mockPdfKitDocument(mockStruct);

addDistanceInfo(mockDoc, userDataMock, 0);
addDistanceInfo(mockDoc, userDataMock);

expect(addNewPageInCaseMissingVerticalSpace).toBeCalled();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ describe("addOtherDetailsItinerary", () => {
const mockStruct = mockPdfKitDocumentStructure();
const mockDoc = mockPdfKitDocument(mockStruct);

addOtherDetailsItinerary(mockDoc, 0, userDataMock.zusaetzlicheAngaben);
addOtherDetailsItinerary(mockDoc, userDataMock.zusaetzlicheAngaben);

expect(mockDoc.text).toHaveBeenCalledWith(
OTHER_DETAILS_ITINERARY,
expect.anything(),
expect.anything(),
);

expect(mockDoc.text).toHaveBeenCalledWith(userDataMock.zusaetzlicheAngaben);
Expand All @@ -28,12 +27,11 @@ describe("addOtherDetailsItinerary", () => {
const mockStruct = mockPdfKitDocumentStructure();
const mockDoc = mockPdfKitDocument(mockStruct);

addOtherDetailsItinerary(mockDoc, 0);
addOtherDetailsItinerary(mockDoc);

expect(mockDoc.text).not.toHaveBeenCalledWith(
OTHER_DETAILS_ITINERARY,
expect.anything(),
expect.anything(),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@ export const addCompensationAmount = (
doc: typeof PDFDocument,
documentStruct: PDFKit.PDFStructureElement,
userData: FluggastrechtContext,
compensationStartYPosition: number,
) => {
const compensationSect = doc.struct("Sect");
compensationSect.add(
doc.struct("P", {}, () => {
doc.font(FONTS_BUNDESSANS_REGULAR).fontSize(10);

addOtherDetailsItinerary(
doc,
compensationStartYPosition,
userData.zusaetzlicheAngaben,
);
addOtherDetailsItinerary(doc, userData.zusaetzlicheAngaben);

addDistanceInfo(doc, userData, compensationStartYPosition);
addDistanceInfo(doc, userData);

addNewPageInCaseMissingVerticalSpace(doc);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { calculateDistanceBetweenAirportsInKilometers } from "~/domains/fluggast
import { getAirportNameByIataCode } from "~/domains/fluggastrechte/services/airports/getAirportNameByIataCode";
import { getCompensationPayment } from "~/domains/fluggastrechte/services/airports/getCompensationPayment";
import { MARGIN_BETWEEN_SECTIONS } from "~/domains/fluggastrechte/services/pdf/configurations";
import { PDF_MARGIN_HORIZONTAL } from "~/services/pdf/createPdfKitDocument";
import { getStartYPosition } from "./getStartYPosition";
import {
PDF_MARGIN_HORIZONTAL,
PDF_WIDTH_SEIZE,
} from "~/services/pdf/createPdfKitDocument";
import { addNewPageInCaseMissingVerticalSpace } from "../../addNewPageInCaseMissingVerticalSpace";

export const ARTICLE_AIR_PASSENGER_REGULATION_TEXT =
Expand Down Expand Up @@ -40,34 +42,19 @@ const getDistanceText = (userData: FluggastrechtContext): string => {
return `${distanceText} pro Person, insgesamt aus eigenem und abgetretenem Recht damit eine Gesamtsumme von ${compensationTotalAmountValue} €.`;
};

const getYPositionDistanceText = (
doc: typeof PDFDocument,
compensationStartYPosition: number,
zusaetzlicheAngaben?: string,
): number => {
// in case exist the zusaetzlicheAngaben should start to print from the last y position of the document, otherwise from the value compensationStartYPosition
return typeof zusaetzlicheAngaben !== "undefined" &&
zusaetzlicheAngaben.length > 0
? doc.y
: getStartYPosition(compensationStartYPosition, doc.y);
};

export const addDistanceInfo = (
doc: typeof PDFDocument,
userData: FluggastrechtContext,
compensationStartYPosition: number,
) => {
addNewPageInCaseMissingVerticalSpace(doc);
const distanceText = getDistanceText(userData);

const distanceTextHeight = doc.heightOfString(distanceText, {
width: PDF_WIDTH_SEIZE,
});

addNewPageInCaseMissingVerticalSpace(doc, distanceTextHeight);

doc
.text(
getDistanceText(userData),
PDF_MARGIN_HORIZONTAL,
getYPositionDistanceText(
doc,
compensationStartYPosition,
userData.zusaetzlicheAngaben,
),
)
.text(distanceText, PDF_MARGIN_HORIZONTAL)
.moveDown(MARGIN_BETWEEN_SECTIONS);
};
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
import type PDFDocument from "pdfkit";
import { MARGIN_BETWEEN_SECTIONS } from "~/domains/fluggastrechte/services/pdf/configurations";
import { PDF_MARGIN_HORIZONTAL } from "~/services/pdf/createPdfKitDocument";
import { getStartYPosition } from "./getStartYPosition";
import {
PDF_MARGIN_HORIZONTAL,
PDF_WIDTH_SEIZE,
} from "~/services/pdf/createPdfKitDocument";
import { addNewPageInCaseMissingVerticalSpace } from "../../addNewPageInCaseMissingVerticalSpace";

export const OTHER_DETAILS_ITINERARY = "Weitere Angaben zum Reiseverlauf:";

export const addOtherDetailsItinerary = (
doc: typeof PDFDocument,
compensationStartYPosition: number,
zusaetzlicheAngaben?: string,
) => {
if (
typeof zusaetzlicheAngaben !== "undefined" &&
zusaetzlicheAngaben.length > 0
) {
const otherDetailsItineraryHeight = doc.heightOfString(
OTHER_DETAILS_ITINERARY,
{
width: PDF_WIDTH_SEIZE,
},
);

const zusaetzlicheAngabenHeight = doc.heightOfString(zusaetzlicheAngaben, {
width: doc.widthOfString(zusaetzlicheAngaben),
width: PDF_WIDTH_SEIZE,
});
addNewPageInCaseMissingVerticalSpace(doc, zusaetzlicheAngabenHeight);

addNewPageInCaseMissingVerticalSpace(
doc,
zusaetzlicheAngabenHeight + otherDetailsItineraryHeight,
);

doc
.text(
OTHER_DETAILS_ITINERARY,
PDF_MARGIN_HORIZONTAL,
getStartYPosition(compensationStartYPosition, doc.y), // start to print this text from this line
)
.text(OTHER_DETAILS_ITINERARY, PDF_MARGIN_HORIZONTAL)
.text(zusaetzlicheAngaben)
.moveDown(MARGIN_BETWEEN_SECTIONS);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,5 @@ export const createFactsOfCases = (
addNewPageInCaseMissingVerticalSpace(doc, COLUMN_HEIGHT * 4 + MARGIN_BOTTOM);
const startTableY = doc.y;
addTable(doc, documentStruct, startTableY, userData);
// Set tableEndYPosition based on the existence of `andereErsatzverbindungBeschreibung`
const tableEndYPosition = startTableY + COLUMN_HEIGHT * 4 + MARGIN_BOTTOM;
doc.moveDown(MARGIN_BETWEEN_SECTIONS);
const startCompensationYPosition =
typeof userData.andereErsatzverbindungBeschreibung !== "undefined" &&
userData.andereErsatzverbindungBeschreibung.length >= 0
? doc.y
: tableEndYPosition;

addCompensationAmount(
doc,
documentStruct,
userData,
startCompensationYPosition,
);
addCompensationAmount(doc, documentStruct, userData);
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,8 @@ describe("addTableInfo", () => {
const mockDoc = mockPdfKitDocument(mockStruct);

const andereErsatzverbindungBeschreibung = "Sample description";
const tableEndYPosition = 100;

addTableInfo(
mockDoc,
mockStruct,
andereErsatzverbindungBeschreibung,
tableEndYPosition,
);
addTableInfo(mockDoc, mockStruct, andereErsatzverbindungBeschreibung);

expect(mockDoc.struct).toHaveBeenCalledWith("Sect");

Expand All @@ -47,7 +41,6 @@ describe("addTableInfo", () => {
expect(mockDoc.text).toHaveBeenCalledWith(
andereErsatzverbindungBeschreibung,
PDF_MARGIN_HORIZONTAL,
100,
);
});

Expand All @@ -56,14 +49,8 @@ describe("addTableInfo", () => {
const mockDoc = mockPdfKitDocument(mockStruct);

const andereErsatzverbindungBeschreibung = "Sample description";
const tableEndYPosition = 100;

addTableInfo(
mockDoc,
mockStruct,
andereErsatzverbindungBeschreibung,
tableEndYPosition,
);
addTableInfo(mockDoc, mockStruct, andereErsatzverbindungBeschreibung);

expect(addNewPageInCaseMissingVerticalSpace).toBeCalledTimes(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export function addTable(
tableSect.add(table); // Add the table to the section
documentStruct.add(tableSect); // Add the section to the parent structure
doc.fill("black"); // Fill black due next pages of the table

// Set tableEndYPosition based on the existence of `andereErsatzverbindungBeschreibung`
// Get end position of the table generated
const tableEndYPosition = startTableY + COLUMN_HEIGHT * 4 + MARGIN_BOTTOM;
// reset the position of the table
doc.y = tableEndYPosition;
addTableInfo(
doc,
documentStruct,
userData.andereErsatzverbindungBeschreibung ?? "",
tableEndYPosition,
);
}
Loading

0 comments on commit 4d33860

Please sign in to comment.