PDF Exporting
Relevant source files
The following files were used as context for generating this wiki page:The PDF exporting feature allows users to generate professional, print-ready PDF documents for their tailored resumes and cover letters. The system uses a hybrid architecture where a backend service utilizes a headless browser (Playwright) to render dedicated frontend React pages, ensuring that the PDF output is a pixel-perfect representation of the web preview. This approach allows for complex layouts and dynamic styling using standard web technologies.
This document outlines the architecture, data flow, and key components of the PDF generation system. For more information on the resume templates themselves, see the Resume Template System documentation.
Sources: AGENTS.md, README.md, REPORT.md
Architecture Overview
The PDF generation process is initiated from the frontend but orchestrated by the backend. When a user clicks a "Download" button, the frontend makes an API request to a specific backend endpoint. The backend then uses a headless browser to visit a special, non-interactive print-only version of the resume or cover letter page on the frontend. This page is rendered with the user's data and formatting settings, which are passed as URL query parameters. The headless browser generates a PDF from the rendered HTML, and the backend service streams this PDF back to the user's browser as a file download.
This diagram illustrates the end-to-end sequence for downloading a resume PDF.
Frontend Implementation
The frontend is responsible for initiating the download, collecting formatting settings, and providing the print-ready HTML pages that the backend renders.
Download Triggers
The user initiates a PDF download from the ResumeBuilder component. The handleDownload (for resumes) and handleDownloadCoverLetter (for cover letters) functions orchestrate this process. They call the corresponding functions in the API client, passing the resume ID and the current templateSettings.
Sources: apps/frontend/components/builder/resume-builder.tsx:300-318, 345-365
API Client
The file apps/frontend/lib/api/resume.ts contains the functions that communicate with the backend's PDF generation endpoints.
downloadResumePdf(resumeId, settings)downloadCoverLetterPdf(resumeId, pageSize)
The downloadResumePdf function is particularly important as it serializes all the template formatting options into URL query parameters. The backend uses these parameters to construct the correct URL for the headless browser to render.
// Source: apps/frontend/lib/api/resume.ts:162-198
export async function downloadResumePdf(
resumeId: string,
settings?: TemplateSettings
): Promise<Blob> {
const params = new URLSearchParams();
if (settings) {
params.set('template', settings.template);
params.set('pageSize', settings.pageSize);
params.set('marginTop', String(settings.margins.top));
params.set('marginBottom', String(settings.margins.bottom));
params.set('marginLeft', String(settings.margins.left));
params.set('marginRight', String(settings.margins.right));
// ... other settings
}
// ...
const res = await apiFetch(`/resumes/${encodeURIComponent(resumeId)}/pdf?${params.toString()}`);
// ...
return await res.blob();
}
PDF Generation Parameters
The following table details the query parameters sent to the PDF generation endpoints. These are parsed by the print-specific pages to apply the correct styling.
| Parameter | Type | Description | Source File |
|---|---|---|---|
template | swiss-single | swiss-two-column | The resume layout template to use. | resume.ts:168 |
pageSize | A4 | LETTER | The paper size for the PDF. | resume.ts:169 |
marginTop | number (mm) | Top margin of the page. | resume.ts:170 |
marginBottom | number (mm) | Bottom margin of the page. | resume.ts:171 |
marginLeft | number (mm) | Left margin of the page. | resume.ts:172 |
marginRight | number (mm) | Right margin of the page. | resume.ts:173 |
sectionSpacing | 1-5 (SpacingLevel) | The gap between major sections. | resume.ts:174 |
itemSpacing | 1-5 (SpacingLevel) | The gap between items within a section. | resume.ts:175 |
lineHeight | 1-5 (SpacingLevel) | The text line height. | resume.ts:176 |
fontSize | 1-5 (SpacingLevel) | The base font size for the document. | resume.ts:177 |
headerScale | 1-5 (SpacingLevel) | Multiplier for the main header font size. | resume.ts:178 |
headerFont | serif | sans-serif | mono | Font family for headers. | resume.ts:179 |
bodyFont | serif | sans-serif | mono | Font family for body text. | resume.ts:180 |
compactMode | boolean | Applies a multiplier to reduce spacing. | resume.ts:181 |
showContactIcons | boolean | Toggles visibility of icons in the header. | resume.ts:182 |
Sources: apps/frontend/lib/api/resume.ts:162-184, apps/frontend/app/print/resumes/[id]/page.tsx:14-36
Print-Specific Pages
The application contains dedicated, non-interactive pages exclusively for PDF rendering:
apps/frontend/app/print/resumes/[id]/page.tsxapps/frontend/app/print/cover-letter/[id]/page.tsx
These pages perform the following key tasks:
- Fetch the necessary data (resume or cover letter content) using the
idfrom the URL. - Parse all formatting and layout settings from the URL query parameters.
- Pass the fetched data and parsed settings to the appropriate React components (
Resumeor a cover letter layout). - The
Resumecomponent, in turn, applies the settings as inline CSS variables to a root element, which are then used by the template's stylesheet.
An important detail is that page margins are handled differently. While the margin values are passed to the backend, the frontend print page sets its CSS margins to 0. This is because Playwright's PDF renderer applies the margins directly, which ensures they are consistent on every page of a multi-page document.
Sources: apps/frontend/app/print/resumes/[id]/page.tsx:150-154, apps/frontend/app/print/cover-letter/[id]/page.tsx
Backend Implementation
The backend exposes API endpoints to handle PDF generation requests. It uses Playwright to control a headless Chromium instance.
PDF Service (pdf.py)
Based on project documentation, the backend contains a PDF service (apps/backend/app/pdf.py) that encapsulates the Playwright logic.
The general flow is as follows:
- Initialize a single, shared Playwright browser instance on application startup.
- When a request is received, create a new browser page.
- Navigate to the frontend's print URL, which includes the resume ID and all formatting parameters.
- Wait for the page to fully render.
- Call Playwright's
page.pdf()method, passing in options likepageSizeand margins. - Return the generated PDF buffer to the API router.
- Close the page.
To prevent race conditions during concurrent PDF generation requests, the browser initialization logic is protected by an asyncio.Lock.
Sources: REPORT.md, INSTALL.md
Styling for Print
The visual appearance of the PDF is controlled by CSS, with special considerations for the print medium.
CSS Custom Properties (Variables)
The resume templates are styled using CSS custom properties defined in apps/frontend/app/(default)/css/globals.css. The print pages override these variables using inline styles based on the URL query parameters. This allows for dynamic control over the PDF's appearance without needing separate stylesheets.
Key variables include:
--section-gap--item-gap--line-height--font-size-base--header-scale--header-font--body-font--margin-top,--margin-bottom,--margin-left,--margin-right
Sources: apps/frontend/app/(default)/css/globals.css:205-224
Page Break Control
To ensure a professional layout, the CSS includes rules to prevent awkward page breaks. Individual items within a section (like a single job experience or project) are prevented from splitting across pages using break-inside: avoid.
/* Source: apps/frontend/app/(default)/css/globals.css:368-372 */
/* Individual items should stay together (job entries, projects, etc.) */
.resume-body .resume-item {
break-inside: avoid;
page-break-inside: avoid;
}
Additionally, rules are in place to prevent section titles from being orphaned at the bottom of a page. The break-after: avoid property ensures that the title stays with the content that follows it.
/* Source: apps/frontend/app/(default)/css/globals.css:375-383 */
/* Section titles should not be orphaned at bottom of page */
.resume-body .resume-section-title,
.resume-body .resume-section-title-sm {
break-after: avoid;
page-break-after: avoid;
/* Ensure at least 3 lines of content follow the header before a page break */
orphans: 3;
widows: 3;
}
These rules are reinforced within an @media print block to ensure they are applied correctly by the headless browser.
Sources: apps/frontend/app/(default)/css/globals.css:362-416