# Render and print

The renderer reads a complete exam from the URL fragment and renders it entirely in the browser.

## Endpoint

Preview without opening the print dialog:

```text
https://render.printexam.com/#<payload>
```

Render and open the browser print dialog:

```text
https://render.printexam.com/?print#<payload>
```

`payload` is the UTF-8 JSON document encoded as standard Base64 or URL-safe Base64. Padding is optional.

## Create a URL

1. Validate the complete exam against [`/schema.json`](/schema.json).
2. Serialize it once with `JSON.stringify`.
3. Encode the resulting UTF-8 bytes as Base64.
4. Put the encoded value after `#` without another encoding pass.
5. Add `?print` only when print mode is requested.

### Browser

```js
function createPrintexamUrl(exam, { print = false } = {}) {
  const bytes = new TextEncoder().encode(JSON.stringify(exam));
  const binary = Array.from(bytes, (byte) => String.fromCharCode(byte)).join("");
  const payload = btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
  return `https://render.printexam.com/${print ? "?print" : ""}#${payload}`;
}
```

### Node.js

```js
function createPrintexamUrl(exam, { print = false } = {}) {
  const payload = Buffer.from(JSON.stringify(exam), "utf8").toString("base64url");
  return `https://render.printexam.com/${print ? "?print" : ""}#${payload}`;
}
```

## Expected behavior

- No payload: the renderer shows a missing-content error.
- Invalid Base64, JSON, or schema: the renderer shows an invalid-content error.
- Valid payload: the renderer displays the exam.
- Valid payload with `?print`: the renderer displays the exam and opens the browser print dialog.

The renderer does not upload, store, grade, or return a PDF file. Saving as PDF is performed by the browser print dialog.

## Privacy

The fragment is not sent to the server in the HTTP request. The complete URL can still appear in browser history, logs created by browser extensions, copied messages, and shared documents. Avoid unnecessary personal or sensitive data.
