# `documents.pdf_in_route`

A route's action renders a PDF, directly or through an action it calls, which only a subscriber may.

Area 16, [Built-in services](/docs/built-in-services.md); not declinable; the next step is usually the agent's.

## Why Jadpo refuses it

`documents.pdf` sends a document's HTML to an outside service and waits
for the PDF. That is slow work, and a provider allows only so
many renders at once (`max_concurrent:`), so a route that renders keeps
its client waiting, may run past its deadline, and makes clients queue
behind each other when many ask at once. Jadpo follows every route's
action through the actions, functions and queries it calls, and refuses
each `documents.pdf` it reaches. A subscriber renders instead, after the
route has answered.

## What it looks like

















`domains/billing/routes/receipts.jadpo` prints a receipt while the customer waits:

```jadpo
use billing/types { ReceiptAsked, ReceiptFile }
use billing/actions { receipt_pdf }

/// Where customers print their receipts.
routes Receipts {
    prefix: /receipts

    /// A receipt's PDF.
    POST /print {
        authentication: RouteAuthentication.none
        input: ReceiptAsked
        output: ReceiptFile
        status: SuccessStatus.ok
        run: receipt_pdf(input.number)
    }
}
```

`domains/billing/actions/receipt_pdf.jadpo`, the action it runs, renders the receipt:

```jadpo
use billing/types { ReceiptFile, ReceiptNumber }
use billing/failures { ReceiptNotRendered }
use billing/documents { ReceiptDocument }

/// A receipt's PDF, printed while the customer waits.
action receipt_pdf(number: ReceiptNumber) fails ReceiptNotRendered -> ReceiptFile {
    var pdf: Bytes = attempt documents.pdf(
        ReceiptDocument {
            number: number
        },
        locale: Locale.en_gb
    ) {
        timed_out: ReceiptNotRendered
    }
    return ReceiptFile {
        pdf: pdf
    }
}
```

```text
error: `documents.pdf` is reached from route `POST /receipts/print`
 --> domains/billing/actions/receipt_pdf.jadpo:7:30
  |
7 |     var pdf: Bytes = attempt documents.pdf(
  |                              ^^^^^^^^^^^^^^ renders a PDF while a client waits
  reason: Rendering a PDF is slow work through an outside service, so it runs in a subscriber, never while a route's client waits: a slow render would hold the request past its deadline, and a provider's cap on renders at once would make clients queue.
  next: Have `receipt_pdf` emit an event, and render the PDF in a subscriber of it, keeping the bytes with `put`; the route answers `202` (agent)
  rule: documents.pdf_in_route
  help: https://jadpo.dev/docs/errors/documents.pdf_in_route

check failed: 1 error, 17 files
```

`(agent)` at the end of the `next:` line means your coding agent can make
the fix itself.

## How to fix it

**If the client can fetch the PDF once it is ready**, have the route's
action keep what was asked for and emit an event, and render the PDF in a
subscriber of it, keeping the bytes with `put`. The route answers `202
Accepted` with the receipt's id, and the customer downloads the PDF once
it is there.





















`domains/billing/actions/request_receipt.jadpo` keeps the request and emits the event:

```jadpo
use billing/entities { Receipt }
use billing/types { ReceiptAskedFor, ReceiptNumber }
use billing/events { ReceiptRequested }

/// Asks for a receipt; a subscriber prints it.
action request_receipt(number: ReceiptNumber) -> ReceiptAskedFor {
    var receipt: Receipt = Receipt.create {
        set: {
            number: number
            pdf: none
        }
    }
    emit ReceiptRequested {
        receipt_id: receipt.id
    }
    return ReceiptAskedFor {
        id: receipt.id
    }
}
```

`domains/billing/subscribers/print_requested.jadpo` prints it, through `print_receipt`, which renders the receipt and keeps the PDF on it:

```jadpo
use billing/actions { print_receipt }
use billing/events { ReceiptRequested }

/// Prints each receipt asked for.
subscriber print_requested {
    on: ReceiptRequested
    run: print_receipt(event.receipt_id)
}
```

`domains/billing/routes/receipts.jadpo` answers at once:

```jadpo
use billing/types { ReceiptAsked, ReceiptAskedFor }
use billing/actions { request_receipt }

/// Where customers print their receipts.
routes Receipts {
    prefix: /receipts

    /// Asks for a receipt's PDF, printed in the background.
    POST /print {
        authentication: RouteAuthentication.none
        input: ReceiptAsked
        output: ReceiptAskedFor
        status: SuccessStatus.accepted
        run: request_receipt(input.number)
    }
}
```

The route now answers at once, and the receipt is printed after it, in
the subscriber, as many at a time as the provider allows.

## Related

- [Documents: PDFs](/docs/documents.md#pdfs)
- [How polls and `max_concurrent:` run](/docs/how-polls-and-max-concurrent-run.md)
- [Subscribers](/docs/subscribers.md)
