Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

fix(pdf): prevent list overlap at page boundaries

Amruth Pillai2d44aff10d8a
diff --git a/packages/pdf/src/forme/list-pagination.integration.test.tsx b/packages/pdf/src/forme/list-pagination.integration.test.tsx
new file mode 100644
index 000000000..535381ca2
--- /dev/null
+++ b/packages/pdf/src/forme/list-pagination.integration.test.tsx
@@ -0,0 +1,55 @@
+import assert from "node:assert/strict";
+import { expect, it } from "vitest";
+import * as forme from "@formepdf/core";
+import { getDocument } from "pdfjs-dist/legacy/build/pdf.mjs";
+import { act } from "react";
+import { Document, Page, Text, View } from "./primitives";
+import { renderResumeElement } from "./render";
+
+it("reserves every line of a list item moved to the next page", async () => {
+ const result = await act(() =>
+ renderResumeElement(
+ forme,
+ <Document>
+ <Page size={{ width: 200, height: 150 }} style={{ padding: 10, fontSize: 10, lineHeight: 1.5 }}>
+ <View style={{ height: 120 }} />
+ <View>
+ <View data-resume-list-item style={{ flexDirection: "row", marginBottom: 6 }}>
+ <Text data-resume-list-marker style={{ width: 10 }}>
+ •
+ </Text>
+ <View data-resume-list-content style={{ flex: 1 }}>
+ <Text>{"First line\nSecond line"}</Text>
+ </View>
+ </View>
+ <View data-resume-list-item style={{ flexDirection: "row" }}>
+ <Text data-resume-list-marker style={{ width: 10 }}>
+ •
+ </Text>
+ <View data-resume-list-content style={{ flex: 1 }}>
+ <Text>Next item</Text>
+ </View>
+ </View>
+ </View>
+ </Page>
+ </Document>,
+ ),
+ );
+ const loading = getDocument({ data: result.pdf.slice(), useSystemFonts: true });
+ try {
+ const pdf = await loading.promise;
+ expect(pdf.numPages).toBe(2);
+ const page = await pdf.getPage(2);
+ const lines = (await page.getTextContent()).items
+ .filter((item) => "str" in item)
+ .filter((item) => item.str.trim() && item.str !== "•");
+ expect(lines.map((line) => line.str)).toEqual(["First line", "Second line", "Next item"]);
+ const [, second, next] = lines;
+ assert(second && next);
+ assert(second.transform[5] !== undefined && next.transform[5] !== undefined);
+ // PDF coordinates grow upward: the following line's top must stay below the preceding baseline.
+ expect(next.transform[5] + next.height).toBeLessThanOrEqual(second.transform[5]);
+ } finally {
+ await loading.destroy();
+ }
+});
diff --git a/packages/pdf/src/forme/render.ts b/packages/pdf/src/forme/render.ts
index 030929215..83b6e284a 100644
--- a/packages/pdf/src/forme/render.ts
+++ b/packages/pdf/src/forme/render.ts
@@ -105,22 +105,34 @@ function unplacedBlocks(layout: RenderWithLayoutResult["layout"]): string[] {
const isFixed = (element: ElementInfo): boolean =>
element.sourceLocation?.file === FIXED_SOURCE || element.children.some(isFixed);
-/** List items whose marker sits on an earlier page than their first line (see `LIST_ROLE`). */
-function listMarkersLeftBehind(layout: RenderWithLayoutResult["layout"]): number[] {
+/** List items needing a page break to keep their marker and full row height (see `LIST_ROLE`). */
+function listItemsNeedingBreak(layout: RenderWithLayoutResult["layout"]): number[] {
const markerPage = new Map<number, number>();
const contentPage = new Map<number, number>();
+ const itemPage = new Map<number, number>();
layout.pages.forEach((page, pageIndex) => {
const visit = (element: ElementInfo) => {
const source = element.sourceLocation;
const pages =
- source?.column === LIST_ROLE.marker ? markerPage : source?.column === LIST_ROLE.content ? contentPage : null;
+ source?.column === LIST_ROLE.marker
+ ? markerPage
+ : source?.column === LIST_ROLE.content
+ ? contentPage
+ : source?.column === LIST_ROLE.item
+ ? itemPage
+ : null;
// Forme leaves some fragments of a splitting box at y ±Number.MAX_VALUE; they aren't on this page.
if (source && pages && !pages.has(source.line) && !offPage(element.y)) pages.set(source.line, pageIndex);
element.children.forEach(visit);
};
page.elements.forEach(visit);
});
- return [...markerPage].flatMap(([line, page]) => ((contentPage.get(line) ?? page) > page ? [line - 1] : []));
+ return [...markerPage].flatMap(([line, page]) => {
+ const content = contentPage.get(line) ?? page;
+ // Forme can move both companions to a new page but drop their row, reserving only the marker's line height.
+ const detached = page > 0 && content === page && itemPage.get(line) !== page;
+ return content > page || detached ? [line - 1] : [];
+ });
}
/**
@@ -224,14 +236,14 @@ export async function renderResumeElement(
return { result, warnings: converted.warnings };
};
- // A marker left on the page its first line leaves: that item starts the next page instead. Breaks move what
- // follows, so a few passes settle it; an item that already starts a page is never broken again.
+ // Explicit breaks keep markers with their first line and restore rows lost when an item moves to a new page.
+ // Breaks move what follows, so a few passes settle it; each item is repaired at most once.
const layOut = async (keepNestedRowsWhole: boolean) => {
for (let pass = 0; ; pass++) {
const laidOut = await layOutOnce(keepNestedRowsWhole);
- const leftBehind = listMarkersLeftBehind(laidOut.result.layout).filter((item) => !breakBeforeListItems.has(item));
- if (leftBehind.length === 0 || pass === 3) return laidOut;
- for (const item of leftBehind) breakBeforeListItems.add(item);
+ const needsBreak = listItemsNeedingBreak(laidOut.result.layout).filter((item) => !breakBeforeListItems.has(item));
+ if (needsBreak.length === 0 || pass === 3) return laidOut;
+ for (const item of needsBreak) breakBeforeListItems.add(item);
}
};