# PDF of an encrypted site, printed at commit

An [encrypted site](/documentation/configuration/password) offers its PDFs (the sections of
[`docs.pdf`](/documentation/integrations/docs-pdf), the decks of
[`publish.pdf`](/documentation/integrations/docs-pdf#4-the-published-decks)) like any other site:
the button above the docs pages, "Download the deck (PDF)" in the toolbar of each deck, and
`<PdfDownload>` in an MDX page. This page is for a site whose host has no Chromium to print them,
such as Vercel when it builds on `git push`. The PDFs are printed on your machine at each commit,
kept sealed in the repository, and copied by the host's build.

## Why

- **Printing needs Chromium.** `recit-site build --pdf` prints the PDFs with Playwright's Chromium.
  A host's build image usually lacks it, and installing it there downloads about 110 MB on every
  build ([Deploy on Vercel](/documentation/configuration/deploy-vercel#pdfs-on-vercel)).
- **An encrypted site draws a new key on every build.** A PDF sealed by one build does not open in
  another, so you cannot copy the PDFs of your local build into the host's.

So the PDFs are printed where Chromium is (your machine, in a git hook) into a **store**, a folder
in the repository that is sealed by the site's password. The host's build opens the store with that
password, which it already has, and seals each PDF again with its own key. It needs only Node, no
browser.

## The circuit

1. **At commit.** `recit export --to pdf --site . --prebuilt --changed <files>` prints the PDFs that
   read one of the staged files into `recit-pdf/`, sealed. The hook adds the folder to the commit.
2. **On the host.** `recit-site build`, without `--pdf`, finds `recit-pdf/`, opens it with the
   password and seals each PDF again with the build's key. It then lists them in `exports.json` and
   in the envelope of the build (`recit-access.json`), the same way `--pdf` does. With `VERCEL` set,
   this happens before the build moves to `.vercel/output/static`.
3. **In the site.** The deck toolbar and the docs button find the PDFs in `exports.json`,
   `<PdfDownload>` links the path it is given, and all three decrypt the file in the page: the
   download starts with `%PDF-`.

A site that runs `recit-site build --pdf` keeps doing so; nothing changes for it.

## The store

`recit-pdf/` holds one sealed file per PDF, at the path it gets in the build (`pdf/lorem.pdf`,
`stories/lorem-deck.pdf`). It also holds `recit-pdf.json`, the manifest, sealed too, and
`recit-access.json`, the envelope of the store. Change the folder with `publish.pdf.prebuilt`:

```ts title="recit.config.ts"
export default defineConfig({
  docs: { pdf: { sections: ["Lorem"] } },
  publish: { pdf: { prebuilt: "recit-pdf" } }, // the default
  access: { password: { env: "RECIT_SITE_PASSWORD" }, mode: "encrypted" },
});
```

- **Sealed by the password, never by a key in the repository.** The store's envelope is the same
  kind as the build's: a content key of its own, wrapped by the password (PBKDF2). Reading the
  repository is not enough to open a PDF. On a site in clear, the store is in clear.
- **A fingerprint per PDF.** The manifest records, for each PDF, the SHA-256 of what it was printed
  from. For a deck, that is its source, the config it is built with, its `customCss` and the local
  files it names. For a docs section, it is the pages of the section, the list of the docs pages,
  the config and the site's sheet. The Récit version is recorded too. A Markdown link to a page or a
  deck is not a source: the PDF shows the link, so editing the deck does not reprint the section.
- **Deterministic.** A PDF whose sources did not change is not printed again, and a file whose
  content did not change keeps its bytes. A commit that touches one deck changes that deck's file
  and the manifest, nothing else.

Print it once, then commit it:

```bash
RECIT_SITE_PASSWORD=… pnpm exec recit export --to pdf --site . --prebuilt
git add recit-pdf
```

The command builds the site first (`recit-site build`, which needs `@recit/site` in the site's
dependencies), then prints. It reads the password from the variable named by `access.password` the
way `recit-site build` does: the environment first, then the site's `.env` files (`.env`,
`.env.local`, `.env.production`, `.env.production.local`, the last one that sets it wins). A
variable exported in your shell always wins over the files. Keep the password in `.env.local`, which
git ignores (see below): the hook then finds it without an export. The password is never printed,
and never written in clear into the store or its manifest.

## The git hook

With [lefthook](https://lefthook.dev), at the root of the repository:

```yaml title="lefthook.yml"
pre-commit:
  commands:
    recit-pdf:
      # The staged files, deleted ones included: {staged_files} leaves deletions out
      files: git diff --name-only --cached
      # Prints only the PDFs that read one of these files, then adds the store to the commit
      run: pnpm exec recit export --to pdf --site . --prebuilt --changed {files} && git add recit-pdf
```

For a site in a subfolder of the repository (`sites/www`), run the command there. lefthook then
gives `{files}` relative to that folder:

```yaml title="lefthook.yml"
pre-commit:
  commands:
    recit-pdf:
      root: "sites/www/"
      files: git diff --name-only --cached
      run: pnpm exec recit export --to pdf --site . --prebuilt --changed {files} && git add recit-pdf
```

- `--changed {files}` prints only the PDFs whose fingerprint reads one of the staged files: a commit
  that touches one deck prints that deck, in seconds. A commit that touches no source prints nothing
  and starts neither the build nor a browser.
- `files: git diff --name-only --cached` lists the deleted files too. lefthook's own
  `{staged_files}` leaves them out, and skips the command when a commit only deletes: the PDF of a
  deleted deck would then stay in the store.
- `git add recit-pdf` stages what was printed and what was removed: a deck that is no longer
  published loses its PDF in the same commit.
- The hook needs Chromium (`pnpm exec playwright install chromium`, once) and the password, in the
  environment or in a `.env` file of the site such as `.env.local`. Without them, the commit stops
  with the reason.
- `LEFTHOOK=0 git commit` or `git commit --no-verify` skips the hook. The PDFs are then older than
  their sources. The build says so, and the CI check below fails.

Another hook manager works the same way: run the command with the staged files, deleted ones
included, then `git add` the store.

The options of the PDFs (`notes`, `colorMode`, `page.size`) come from the config, in `publish.pdf`
and `docs.pdf`: a change there reprints the PDFs it concerns. `--prebuilt` refuses `--notes`,
`--color-mode` and `--page-size`, which no fingerprint would record.

## `.gitignore`

The store is committed. The build output and the environment files are not:

```gitignore title=".gitignore"
dist
.vercel
.env*.local
```

Do not ignore `recit-pdf/`: the host's build reads it from the repository. Do exclude it from your
formatter and linter (`"!**/recit-pdf"` in Biome's `files.includes`, `ignorePatterns` of oxfmt,
`.prettierignore`): on an encrypted site its files, `recit-pdf.json` included, are sealed bytes, not
text, and a tool that rewrites them breaks the store.

Keep the password out of the repository: on Vercel it is an environment variable of the project. The
`.env.production` of Récit's sealed example is a test fixture, not a model.

## What the build does

`recit-site build` reports the store before the Vite build:

```text
PDF store recit-pdf/: 3 PDF(s), sealed again with the key of this build.
```

| Case                                                   | What happens                                                         |
| ------------------------------------------------------ | -------------------------------------------------------------------- |
| the store opens, every fingerprint matches             | the PDFs are sealed again and listed, with the line above            |
| a PDF is older than its sources                        | a warning names it (below), and the PDF is still copied              |
| the same, with `--strict-pdf`                          | the build fails                                                      |
| the source of a deck is gone (no longer published)     | a warning names it, and its PDF is left out of the build             |
| the password is missing or wrong                       | the build fails: `RECIT_SITE_PASSWORD does not open the PDF store …` |
| a store in clear on an encrypted site, or the reverse  | the build fails                                                      |
| a file or the envelope of the store was edited by hand | the build fails: the file no longer opens or matches the manifest    |
| `recit-site build --pdf`                               | `--pdf` prints from this build; the store is ignored, with a message |
| `recit-site build --no-pdf-store`                      | the store is left out                                                |
| `recit-site build --experimental-ssr`                  | the store is ignored, with a warning                                 |

A PDF that does not match its sources is a warning by default, not a failure. The host cannot print
it again, and a site with a slightly old PDF is better than a failed deployment. To make it a
failure, use `recit-site build --strict-pdf`, or run the CI check.

## A staleness warning

```text
⚠ Stale PDF: deck "lorem-deck" (recit-pdf/stories/lorem-deck.pdf): recit.config.ts changed since it was printed.
Print them again on a machine with Chromium: recit export --to pdf --site . --prebuilt --changed, then commit recit-pdf/.
```

Each line names the deck or the docs section, its file in the store, and the sources that changed
since it was printed. `docs/` in that list means the list of docs pages changed (a page added,
removed or renamed); "the list of the decks of stories/" means a deck was added to or removed from
`publish.dir` without a new print. The PDF of a deck whose source is gone is not copied at all: the
warning says so. On your machine:

```bash
RECIT_SITE_PASSWORD=… pnpm exec recit export --to pdf --site . --prebuilt --changed
git add recit-pdf && git commit -m "Print the PDFs again"
```

`--changed` without a file list compares every fingerprint and prints what differs. A new Récit
version counts as a change for every PDF. To force a reprint of everything, run the command without
`--changed`.

## Check the store in CI

```bash
pnpm exec recit export --to pdf --site . --prebuilt --check
```

`--check` prints nothing and writes nothing. It lists the PDFs that are missing, out of date or no
longer asked for, and exits with code 1 when there is one. It needs the password in the CI's
environment, but not Chromium. Run it in CI (on pushes to `main` and on pull requests) to catch a
commit made without the hook.

## PowerPoint decks in the same store (preview)

With `@recit/pptx` installed next to `recit` ([Exporting](/formats/deck/exporting)), the published
decks of a site go into the same `publish.pdf.prebuilt` store (`recit-pdf/` by default), sealed by
the same password on an encrypted site:

```bash
RECIT_SITE_PASSWORD=… pnpm exec recit export --to pptx --site . --prebuilt
RECIT_SITE_PASSWORD=… pnpm exec recit export --to pptx --site . --prebuilt --changed
RECIT_SITE_PASSWORD=… pnpm exec recit export --to pptx --site . --prebuilt --check
```

- Each deck is exported on its own (no `recit-site build` first); pass `--pptx-images` to store one
  2× slide picture per slide with speaker notes kept.
- The PDF command leaves the `.pptx` entries of `recit-pdf.json` untouched, and `--to pptx` leaves
  the PDFs untouched. A site that ships both runs both commands in `lefthook.yml` before
  `git add recit-pdf`.
- `recit-site build` re-seals the `.pptx` files next to each deck's page, lists them in
  `exports.json` (`kind: "pptx"`), and warns with
  `recit export --to pptx --site . --prebuilt --changed` when one is older than its sources
  (`--strict-pdf` fails instead).

## The manifest, exports.json

How `recit-site build` and `recit export --to pdf` or `--to pptx` write `exports.json` is covered in
[The circuit](#the-circuit) and above. Every other command or target that emits manifested files —
`recit render` (MP4, captions, transcript, stills and posters) and `recit export` with `--to images`
(cards, slides, slices, and PNG or SVG charts from `*.chart.json` or `--charts`), `--to epub`,
`--to ics`, `--to rss`, `--variants`, or a plugin `--to` exporter — merges its entries into the same
manifest next to its output (`ExportManifestSchema` of `@recit/core/schema`, `recit schema exports`,
preview), keyed and sorted by `files.path`, so the deck toolbar, the docs PDF button
(`DocsPdfButton`), `<StoryGallery>` of `@recit/site-stories` and tooling read a single file:

| Key                              | Subject              | Description                                                                                                                                                                                                            |
| -------------------------------- | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `$schema`                        | envelope             | Optional JSON Schema URL (`"https://recit.dev/schema/v1/exports.json"`)                                                                                                                                                |
| `version`                        | envelope             | Manifest format version (`1`, required)                                                                                                                                                                                |
| `files`                          | envelope             | Array of exported file entries, sorted by `files.path` (required)                                                                                                                                                      |
| `files.path`                     | identity             | Relative path of the exported file from the manifest's folder (required)                                                                                                                                               |
| `files.kind`                     | identity             | File kind: `"pdf"`, `"epub"`, `"ics"`, `"rss"`, `"png"`, `"jpeg"`, `"webp"`, `"svg"`, `"json"`, `"vtt"`, `"txt"`, `"mp4"`, `"plugin"` or `"pptx"` (required)                                                           |
| `files.source`                   | identity             | Story path, docs route or config entry the file came from (required)                                                                                                                                                   |
| `files.locale`                   | identity             | BCP 47 locale tag of the file (required)                                                                                                                                                                               |
| `files.bytes`                    | identity             | File size in bytes, integer `>= 0` (required)                                                                                                                                                                          |
| `files.sha256`                   | identity             | 64-character lowercase hex SHA-256 digest of the file (required)                                                                                                                                                       |
| `files.origin`                   | origin               | Discriminated union naming what produced the file (required)                                                                                                                                                           |
| `files.origin.type`              | origin               | `"story"`, `"block"` or `"tool"` (required)                                                                                                                                                                            |
| `files.origin.format`            | origin               | Story format id (`"deck"`, `"motion"`, `"docs"`, `"report"`, `"webtoon"`, `"book"`, `"roadmap"`, `"calendar"`, `"webstory"`, `"glossary"`, `"directory"`, `"atlas"` or `"form"`) when `files.origin.type` is `"story"` |
| `files.origin.block`             | origin               | Registered block directive name (such as `"form"` or `"chart"`) when `files.origin.type` is `"block"`                                                                                                                  |
| `files.origin.tool`              | origin               | Tool id (`"skills"`) when `files.origin.type` is `"tool"` (reserved in the schema; no command writes it yet)                                                                                                           |
| `files.preset`                   | media and copy       | Optional output preset name (such as `"instagram-post"`)                                                                                                                                                               |
| `files.variant`                  | media and copy       | Optional variant key from the variants file (`{variant}`)                                                                                                                                                              |
| `files.aspect`                   | media and copy       | Optional aspect ratio string (`"4:5"`, `"9:16"`…)                                                                                                                                                                      |
| `files.channel`                  | media and copy       | Optional publishing channel name (such as `"Instagram"` or `"LinkedIn"`)                                                                                                                                               |
| `files.copy`                     | media and copy       | Optional post copy resolved for `files.locale`                                                                                                                                                                         |
| `files.copy.text`                | media and copy       | Main post text (required when `files.copy` is present)                                                                                                                                                                 |
| `files.copy.alternates`          | media and copy       | Optional array of alternative hook strings                                                                                                                                                                             |
| `files.warnings`                 | warnings             | Optional array of non-fatal checks recorded on a file that was still written                                                                                                                                           |
| `files.warnings.code`            | warnings             | Check code, such as `"safe-area"` or `"overflow"` (required on each warning)                                                                                                                                           |
| `files.warnings.message`         | warnings             | Human-readable warning message (required on each warning)                                                                                                                                                              |
| `files.warnings.scene`           | warnings             | Optional `id` of the scene where the warning occurred                                                                                                                                                                  |
| `files.pages`                    | documents and feeds  | Page count, positive integer (`files.kind: "pdf"`)                                                                                                                                                                     |
| `files.scope`                    | documents and feeds  | Optional PDF scope: `"story"`, `"page"` or `"section"` (`files.kind: "pdf"`)                                                                                                                                           |
| `files.routes`                   | documents and feeds  | Optional array of site routes starting with `"/"` covered by the file (`files.kind: "pdf"` and `"pptx"`)                                                                                                               |
| `files.title`                    | documents and feeds  | Optional label shown on the download button (`files.kind: "pdf"` and `"pptx"`)                                                                                                                                         |
| `files.preview`                  | documents and feeds  | Optional PNG of the first page of a story's PDF, written next to it by `recit export --to pdf` (`files.kind: "pdf"`; none on an encrypted site)                                                                        |
| `files.preview.path`             | documents and feeds  | Relative path of the PNG from the manifest's folder (`<name>.preview.png`)                                                                                                                                             |
| `files.preview.width`            | documents and feeds  | Width of the PNG in pixels                                                                                                                                                                                             |
| `files.preview.height`           | documents and feeds  | Height of the PNG in pixels                                                                                                                                                                                            |
| `files.chapters`                 | documents and feeds  | Chapter count in the spine, positive integer (`files.kind: "epub"`)                                                                                                                                                    |
| `files.events`                   | documents and feeds  | `VEVENT` count in the file, positive integer (`files.kind: "ics"`)                                                                                                                                                     |
| `files.subscribable`             | documents and feeds  | Optional boolean marking a calendar feed apps subscribe to (`files.kind: "ics"`)                                                                                                                                       |
| `files.items`                    | documents and feeds  | `<item>` count in the feed, positive integer (`files.kind: "rss"`)                                                                                                                                                     |
| `files.width`                    | images and films     | Pixel width, positive integer (`files.kind: "png"`, `"jpeg"`, `"webp"`, `"svg"` and `"mp4"`; an SVG takes it from its `viewBox`)                                                                                       |
| `files.height`                   | images and films     | Pixel height, positive integer (`files.kind: "png"`, `"jpeg"`, `"webp"`, `"svg"` and `"mp4"`; an SVG takes it from its `viewBox`)                                                                                      |
| `files.scene`                    | images and films     | `id` of the scene or ready chart shown in the picture (`files.kind: "png"`, `"jpeg"`, `"webp"` and `"svg"`)                                                                                                            |
| `files.alt`                      | images and films     | Optional alternative text of the picture (`files.kind: "png"`, `"jpeg"`, `"webp"` and `"svg"`)                                                                                                                         |
| `files.slice`                    | images and films     | Optional 1-based slice number of a webtoon episode, starting in `files.scene` (`files.kind: "png"`, `"jpeg"`, `"webp"` and `"svg"`)                                                                                    |
| `files.role`                     | images and films     | Optional picture role, `"poster"` for a film cover (`files.kind: "png"`, `"jpeg"`, `"webp"` and `"svg"`)                                                                                                               |
| `files.fps`                      | images and films     | Frame rate in frames per second, positive integer (`files.kind: "mp4"`)                                                                                                                                                |
| `files.frames`                   | images and films     | Film length in frames at `files.fps`, positive integer (`files.kind: "mp4"`)                                                                                                                                           |
| `files.cues`                     | images and films     | Cue count in the WebVTT caption file, integer `>= 0` (`files.kind: "vtt"`)                                                                                                                                             |
| `files.content`                  | blocks, films, tools | `"definition"` or `"json-schema"` on `files.kind: "json"` (reserved in the schema; no command writes it yet), or `"transcript"` on `files.kind: "txt"`                                                                 |
| `files.id`                       | blocks, films, tools | `id` of the block (such as a `:::form`) the JSON file comes from (`files.kind: "json"`, reserved in the schema; no command writes it yet)                                                                              |
| `files.plugin`                   | blocks, films, tools | Name of the Récit plugin whose exporter wrote the file (`files.kind: "plugin"`, preview)                                                                                                                               |
| `files.target`                   | blocks, films, tools | `--to` target key from the plugin's `exporters` (`files.kind: "plugin"`, preview)                                                                                                                                      |
| `files.mediaType`                | blocks, films, tools | Optional MIME type reported by the plugin exporter (`files.kind: "plugin"`, preview)                                                                                                                                   |
| `files.slides`                   | PowerPoint report    | Slide count, positive integer (`files.kind: "pptx"`, preview)                                                                                                                                                          |
| `files.report`                   | PowerPoint report    | Array of per-slide conversion reports, in slide order (`files.kind: "pptx"`, preview)                                                                                                                                  |
| `files.report.slide`             | PowerPoint report    | 1-based slide number in the deck (required on each item)                                                                                                                                                               |
| `files.report.id`                | PowerPoint report    | Optional `id` of the scene when the deck sets one                                                                                                                                                                      |
| `files.report.native`            | PowerPoint report    | Count of editable PowerPoint elements: text boxes, tables, images and shapes (integer `>= 0`, required)                                                                                                                |
| `files.report.images`            | PowerPoint report    | Count of elements rasterised as a picture of their rendering (integer `>= 0`, required)                                                                                                                                |
| `files.report.notes`             | PowerPoint report    | Whether the slide's speaker notes were written to PowerPoint notes (boolean, required)                                                                                                                                 |
| `files.report.fallbacks`         | PowerPoint report    | Array listing each element rasterised as a picture and why (required)                                                                                                                                                  |
| `files.report.fallbacks.element` | PowerPoint report    | Selector or tag of the rasterised element, such as `pre.mermaid` or `svg` (required on each fallback)                                                                                                                  |
| `files.report.fallbacks.reason`  | PowerPoint report    | Reason the element was rasterised (required on each fallback)                                                                                                                                                          |

## Coming from a home-made circuit

A site that printed its PDFs with its own scripts can drop them. That covers a script run at commit
that re-encrypted the PDFs with a project key, the key module, the folder of `.bin` files, the
script that injected them after the build, a custom download button, and a CSS rule that hid
`DocsPdfButton`. Instead:

1. Delete these scripts, the key module and the `.bin` folder. The key was in clear in the
   repository, so treat it as public.
2. Replace the custom button with
   [`<PdfDownload>`](/documentation/integrations/docs-pdf#link-a-pdf-from-a-page-pdfdownload), and
   the CSS rule with
   [`docs.pdf.button`](/documentation/integrations/docs-pdf#hide-or-move-the-button-docspdfbutton).
3. Print the store once, paste the `lefthook.yml` above, and build with a plain `recit-site build`.
