# PDF Export

## Objective

Turn a record or report into a clean file people can save and send.

Server-rendered PDFs of the app's important records and reports, generated in the background and delivered when ready.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Build one document template per exportable record type, with a defined page size, margins, header, and footer.
2. Pin the fonts into the project and embed them. A font that exists on your laptop and not on the server produces a different document, and you will not notice until a customer does.
3. Paginate deliberately: repeat table headers on every page, keep headings with the content that follows, and define what happens when a single row is taller than a page.
4. Fetch private images and logos through short-lived authorized access at render time. Do not make an asset public just so the renderer can reach it.
5. Generate anything large asynchronously and key the job so a second identical request joins the existing job instead of starting another.
6. Do NOT rebuild raw tabular data extraction here — column selection, filter application, and export permissions are owned by CSV Export; extend that rather than building a second exporter. Generic queue progress and download-when-ready UI is owned by Background Job Progress.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- A table that spills across pages must repeat its header row, and no row may be cut in half at the page break.
- Missing glyphs must be caught in a test, not shipped as boxes in a customer invoice.
- An image that fails to load must render a placeholder and finish the document — one dead URL must not fail the whole export.
- Long unbroken strings (URLs, tokens, IDs) will overflow the page box unless you explicitly allow them to wrap.
- The generated file must respect the requester's permissions at render time, not at request time — a job that runs ten minutes later must re-check access.
- Right-to-left text, non-Latin scripts, and currency symbols need a font that actually contains them.
- A finished PDF stored for download needs an expiry; permanent artefacts of private data accumulate forever.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Fonts are embedded and output is byte-consistent across local, staging, and production.
- [ ] Multi-page tables repeat headers and never clip content at a page boundary.
- [ ] Private images render without being made publicly accessible.
- [ ] Two identical export requests produce one job, not two.
- [ ] Permissions are re-checked when the job runs, not only when it is queued.
- [ ] The entry defers tabular extraction to CSV Export and progress UI to Background Job Progress.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
