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): preserve imported rich text without semantic descendants (#3438)

Amruth Pillaiaada380888d6
diff --git a/packages/pdf/src/semantic/rich-text-table.integration.test.tsx b/packages/pdf/src/semantic/rich-text-table.integration.test.tsx
new file mode 100644
index 000000000..3b8083b6a
--- /dev/null
+++ b/packages/pdf/src/semantic/rich-text-table.integration.test.tsx
@@ -0,0 +1,87 @@
+import type { ResumeData } from "@reactive-resume/schema/resume/data";
+import type { Template } from "@reactive-resume/schema/templates";
+import { describe, expect, it } from "vitest";
+import { renderToBuffer } from "@react-pdf/renderer";
+import { getDocument } from "pdfjs-dist/legacy/build/pdf.mjs";
+import { act } from "react";
+import { defaultResumeData } from "@reactive-resume/schema/resume/default";
+import { ResumeDocument } from "../document";
+
+const table = (paragraphs = false) =>
+ `<table style="width: 240pt; border-collapse: collapse"><tbody>${[
+ ["Alpha", "Beta"],
+ ["Gamma", "Delta"],
+ ]
+ .map(
+ (row) =>
+ `<tr>${row.map((text) => `<td style="width: 120pt; border: 1pt solid black; padding: 4pt">${paragraphs ? `<p>${text}</p>` : text}</td>`).join("")}</tr>`,
+ )
+ .join("")}</tbody></table>`;
+
+const fixture = (html: string, mode: "legacy" | "semantic", css = ""): ResumeData => {
+ const data = structuredClone(defaultResumeData);
+ data.basics.name = "Table probe";
+ data.picture.hidden = true;
+ data.summary.content = html;
+ data.metadata.layout.pages = [{ fullWidth: true, main: ["summary"], sidebar: [] }];
+ data.metadata.typography.body.fontFamily = "Helvetica";
+ data.metadata.typography.heading.fontFamily = "Helvetica";
+ data.metadata.stylesheet = { mode, source: { languageVersion: 1, text: `@version 1; ${css}` } };
+ return data;
+};
+
+const readPdf = async (data: ResumeData, template: Template) => {
+ const bytes = new Uint8Array(await act(() => renderToBuffer(<ResumeDocument data={data} template={template} />)));
+ const loading = getDocument({ data: bytes });
+ try {
+ const document = await loading.promise;
+ const page = await document.getPage(1);
+ const content = await page.getTextContent();
+ return content.items.flatMap((item) =>
+ "str" in item ? [{ text: item.str, x: item.transform[4], y: item.transform[5] }] : [],
+ );
+ } finally {
+ await loading.destroy();
+ }
+};
+
+describe("imported rich-text tables", () => {
+ for (const template of ["ditgar", "onyx"] as const) {
+ for (const mode of ["legacy", "semantic"] as const) {
+ it(`${template} ${mode} preserves bare cell text and row/column positions`, async () => {
+ const items = await readPdf(fixture(table(), mode), template);
+ const cell = (text: string) => {
+ const cell = items.find((item) => item.text === text);
+ if (!cell) throw new Error(`Missing table cell ${text}`);
+ return cell;
+ };
+ const alpha = cell("Alpha");
+ const beta = cell("Beta");
+ const gamma = cell("Gamma");
+ const delta = cell("Delta");
+ expect(alpha.y).toBe(beta.y);
+ expect(gamma.y).toBe(delta.y);
+ expect(alpha.y).toBeGreaterThan(gamma.y);
+ expect(alpha.x).toBe(gamma.x);
+ expect(beta.x).toBe(delta.x);
+ expect(beta.x).toBeGreaterThan(alpha.x);
+ });
+ }
+ }
+
+ it("preserves table cells containing recognized paragraphs", async () => {
+ const items = await readPdf(fixture(table(true), "semantic"), "ditgar");
+ expect(items.map((item) => item.text)).toEqual(expect.arrayContaining(["Alpha", "Beta", "Gamma", "Delta"]));
+ });
+
+ it("preserves raw text inside an unrecognized block wrapper", async () => {
+ const items = await readPdf(fixture("<div>Wrapper content</div>", "semantic"), "ditgar");
+ expect(items.map((item) => item.text)).toContain("Wrapper content");
+ });
+
+ it("still honors explicit semantic rich-text hiding", async () => {
+ const items = await readPdf(fixture(table(), "semantic", "rich-text { display: none; }"), "ditgar");
+ expect(items.map((item) => item.text)).toContain("Table probe");
+ expect(items.map((item) => item.text)).not.toContain("Alpha");
+ });
+});
diff --git a/packages/pdf/src/semantic/tree.ts b/packages/pdf/src/semantic/tree.ts
index a125c28f0..22a26169f 100644
--- a/packages/pdf/src/semantic/tree.ts
+++ b/packages/pdf/src/semantic/tree.ts
@@ -204,8 +204,10 @@ const buildField = ({
direction,
)
: [];
+ // Imported HTML may contain only text inside wrappers without semantic kinds (for example table cells).
+ // Keep its rich-text host visible even when it has no individually addressable descendants.
const children =
- RICH_TEXT_FIELDS.has(name) && typeof value === "string" && richTextChildren.length > 0
+ RICH_TEXT_FIELDS.has(name) && typeof value === "string"
? [
semanticNode({
key: semanticNodeKeys.richText(key, name),