# Record PDF Export URL: /docs/web/kiva/record-pdf-export A three-step wizard (Select a Template → Preview → PDF) that renders a Kiva data source record to PDF using its saved HTML templates, with an optional editable-template mode. **Record PDF Export** turns a single Kiva data source record into a PDF through a guided three-step wizard: 1. **Select a Template** — lists the data source's saved HTML/PDF templates (`tenant_html_template`). 2. **Preview** — renders the chosen template compiled against the record, using the [`HtmlTemplateEditor`](/docs/web/components/html-template-editor) in **preview-only** mode. 3. **PDF** — generates the final PDF server-side and shows it in an inline viewer with a download button. 4. **Email** *(optional, when `sendEmail` is set)* — mounts an email composer ([`useKivaEmailComposer`](/docs/web/hooks/use-kiva-email-composer)) with the generated PDF pre-attached, so the user can send the record straight from the wizard. The headless [`useKivaRecordPdfExport`](#hook--usekivarecordpdfexport) hook owns all data fetching, step state, and PDF generation. `KivaRecordPdfExportWizard` is the batteries-included UI built on top of it. The hook accepts a **`dataSourceId`** and **`recordId`** — the app / data source slugs needed by the render endpoints are resolved automatically (or supplied explicitly). **Demo:** ```tsx 'use client'; import { FileTextIcon } from 'lucide-react'; import { Step, Stepper } from '@kivacore/ui/components/stepper'; import { Button } from '@kivacore/ui/primitives/ui/button'; export function RecordPdfExportDemo() { return (

{''} requires an authenticated{' '} RestApiClient plus a dataSourceId and{' '} recordId. It lists the data source's saved HTML templates, previews the selected template compiled against the record (via the HtmlTemplateEditor in preview-only mode), and renders a server-side PDF — wire it to your tenant (or the playground) to see it live.

); } ``` ## Installation ```bash pnpm dlx @kivacore/ui-cli add @kivacore/ui-record-pdf-export ``` **Dependencies:** - [@tanstack/react-query](https://www.npmjs.com/package/@tanstack/react-query) - [lucide-react](https://www.npmjs.com/package/lucide-react) - [@kivacore/api-client](https://www.npmjs.com/package/@kivacore/api-client) ## Usage ### Wizard inside a dialog ```tsx import { useState } from 'react'; import { useKivaAuth } from '@kivacore/signin'; import { KivaRecordPdfExportWizard } from '@kivacore/ui/components/record-pdf-export'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@kivacore/ui/primitives/ui/dialog'; import { Button } from '@kivacore/ui/primitives/ui/button'; function ExportRecordButton({ dataSourceId, recordId }: { dataSourceId: string; recordId: string }) { const { client } = useKivaAuth(); const [open, setOpen] = useState(false); if (!client) return null; return ( ); } ``` ### Editable-template mode Pass `templateEditable` to let the user tweak the rendered document before exporting. The preview step then exposes the **Code**, **Data** (read-only reference), and **Preview** tabs — the Code tab holds the server-compiled document HTML for this record. When it is edited, the PDF step renders the edited HTML through the custom-body endpoint instead of the saved template. ```tsx ``` ### Email step Pass `sendEmail` to append an **Email** step. After the PDF is generated, the wizard mounts an email composer (via [`useKivaEmailComposer`](/docs/web/hooks/use-kiva-email-composer)) with the rendered PDF pre-attached (sent through its storage path) and the user's email accounts loaded into the From selector. Sending requires the `Messaging.Email.Send` OAuth scope. ```tsx ``` ### Headless — drive your own UI ```tsx import { HtmlTemplateEditor } from '@kivacore/ui/components/html-template-editor'; import { useKivaRecordPdfExport } from '@kivacore/ui/components/record-pdf-export'; function CustomExport({ client, dataSourceId, recordId }) { const { step, goNext, goBack, canGoNext, templates, selectedTemplateId, setSelectedTemplateId, previewEditorProps, pdfUrl, isGeneratingPdf } = useKivaRecordPdfExport({ client, dataSourceId, recordId }); // Step 1 — render `templates` and call `setSelectedTemplateId(id)`. // Step 2 — // Step 3 —