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): paint page backgrounds (#3611)

Amruth Pillai0c6e75eec4d7
diff --git a/docs/applying-custom-styles.mdx b/docs/applying-custom-styles.mdx
index 58d3e1952..24215578d 100644
--- a/docs/applying-custom-styles.mdx
+++ b/docs/applying-custom-styles.mdx
@@ -188,7 +188,7 @@ are lowercase and case-sensitive.
| Element | Targets | Typical use |
| ----------------- | ---------------------------------------------------- | ------------------------------------------------------- |
| `resume` | The whole resume | Scope a rule to one template. |
-| `page` | One PDF page | Set a page size. |
+| `page` | One authored layout page and its overflow pages | Set a page size or background color. |
| `region` | The header, main, sidebar or featured area of a page | Style a layout area. |
| `header` | The resume header | Style the name and contact area. |
| `section` | A section | Target a section type or placement. |
@@ -259,6 +259,17 @@ section[type="experience"] item:nth-of-type(1) {
}
```
+To change the background of the second page in **Layout**, use:
+
+```css
+page[page-number="2"] {
+ background-color: var(--resume-primary-color);
+}
+```
+
+The background covers the entire page, including its margins. `page-number` counts pages in **Layout**, not
+physical PDF pages: if an authored page overflows, its generated continuation pages use the same background.
+
### Reuse your Design settings
Your Design settings are available as read-only `--resume-*` variables. Define your own variables in `:root` and
diff --git a/packages/pdf/src/forme/to-forme.tsx b/packages/pdf/src/forme/to-forme.tsx
index 6a7cb7c2b..401922843 100644
--- a/packages/pdf/src/forme/to-forme.tsx
+++ b/packages/pdf/src/forme/to-forme.tsx
@@ -898,10 +898,32 @@ function convertPage(page: HostElement, context: Context, key: number, freeFormH
converted.style = unreversed.style;
const { children } = unreversed;
const fixed = children.filter((child) => isValidElement(child) && child.type === FormeFixed);
- if (fixed.length === 0)
+ // Forme doesn't paint Page backgroundColor. Repeat a full-page box behind the content, outside the margins.
+ const backgroundColor = converted.style.backgroundColor;
+ const wrapContent = fixed.length > 0 || (backgroundColor !== undefined && converted.style.flexDirection === "row");
+ if (backgroundColor !== undefined) {
+ const backgroundStyle: FormeStyle = {
+ position: "absolute",
+ left: 0,
+ top: 0,
+ width: dimensions.width,
+ height: dimensions.height,
+ backgroundColor,
+ };
+ fixed.unshift(
+ createElement(
+ FormeFixed,
+ { key: "background", position: "header" },
+ fixedOnPage(createElement(FormeView, { style: backgroundStyle }), backgroundStyle, pageContext, margin),
+ ),
+ );
+ delete converted.style.backgroundColor;
+ }
+ if (!wrapContent)
return createElement(
FormePage,
{ key, size: pageSizeProp(size, dimensions), margin, style: converted.style },
+ ...fixed,
...children,
);
diff --git a/packages/pdf/src/semantic/pagination.test.tsx b/packages/pdf/src/semantic/pagination.test.tsx
index c01d488b0..32fb72900 100644
--- a/packages/pdf/src/semantic/pagination.test.tsx
+++ b/packages/pdf/src/semantic/pagination.test.tsx
@@ -6,6 +6,7 @@ import { createElement } from "react";
import { defaultResumeData } from "@reactive-resume/schema/resume/default";
import { ResumeDocument } from "../document";
import { pdf, renderToBuffer } from "../forme/testing";
+import { rasterizePdf } from "./test/rasterize-pdf";
type HostNode = {
type: string;
@@ -134,6 +135,50 @@ describe("semantic pagination bindings", () => {
expect(fixed && nodeText(fixed)).toContain("Ada Lovelace");
});
+ it.each([1, 100])(
+ "paints only the selected Azurill page and its overflow with %i paragraphs (#3610)",
+ async (count) => {
+ const data = structuredClone(defaultResumeData);
+ data.picture.hidden = true;
+ data.basics.name = "Ada Lovelace";
+ data.summary.content = Array.from(
+ { length: count },
+ (_, index) => `<p>Second page paragraph ${index + 1}.</p>`,
+ ).join("");
+ data.metadata.typography.body.fontFamily = "Helvetica";
+ data.metadata.typography.heading.fontFamily = "Helvetica";
+ data.metadata.design.colors.background = "#ffffff";
+ data.metadata.design.colors.primary = "#123456";
+ data.metadata.layout.pages = [
+ { fullWidth: true, main: [], sidebar: [] },
+ { fullWidth: true, main: ["summary"], sidebar: [] },
+ ];
+ data.metadata.stylesheet = {
+ mode: "semantic",
+ source: {
+ languageVersion: 1,
+ text: 'page[page-number="2"] { background-color: var(--resume-primary-color); }',
+ },
+ };
+
+ const pages = await rasterizePdf(await renderPdf(data, "azurill"));
+ if (count === 1) expect(pages).toHaveLength(2);
+ else expect(pages.length).toBeGreaterThan(2);
+ for (const [index, page] of pages.entries()) {
+ const expected = index === 0 ? [255, 255, 255, 255] : [18, 52, 86, 255];
+ const points = [
+ [2, 2],
+ [page.width - 3, page.height - 3],
+ [page.width / 2, page.height / 2],
+ ] as const;
+ for (const [x, y] of points) {
+ const offset = (Math.floor(y) * page.width + Math.floor(x)) * 4;
+ expect(Array.from(page.data.subarray(offset, offset + 4))).toEqual(expected);
+ }
+ }
+ },
+ );
+
it("keeps Azurill overflow lossless while preserving a manual full-width authored continuation", async () => {
const { data, overflowTokens } = azurillAuthoredOverflowFixture();
const authoredPagesBeforeRender = structuredClone(data.metadata.layout.pages);