Skip to content

Export Endpoints

API endpoints for exporting briefing sessions as PDF documents and future formats.

Generate and download a PDF document from a completed briefing session.

Source: src/app/api/export/pdf/[sessionId]/route.ts

ParameterLocationTypeRequiredDescription
sessionIdPathstringYesThe briefing session ID to export

Authentication: Required via auth() from @/lib/auth. Bypassed when ENABLE_DEV_AUTH=true.

Success 200:

HeaderValue
Content-Typeapplication/pdf
Content-Dispositionattachment; filename="briefing-{short-id}.pdf"

Body: raw PDF binary data.

Errors:

StatusBodyCause
401{ error: 'Unauthorized' }Missing or invalid auth session
404{ error: 'Session not found' }No blocks exist for the given sessionId
Terminal window
# Download briefing PDF (requires auth cookie)
curl -o briefing.pdf \
-H "Cookie: next-auth.session-token=..." \
http://localhost:3000/api/export/pdf/abc-12345

In the browser, the download is triggered by navigating to the URL or using window.open():

window.open(`/api/export/pdf/${sessionId}`, '_blank');

The PDF is generated server-side by generateBriefingPDF() in src/lib/pdf/generator.ts:

  1. Load blocks — Fetches all blocks for the session from SQLite via getBlocksDB()
  2. Extract by type — Finds delivery, diagnosis, risk_balance, precedent, and other block types
  3. Build document — Creates a jsPDF instance with A4 page size
  4. Render sections — Renders each block type into PDF sections with appropriate formatting
  5. Handle overflow — Uses checkPage() to detect when content exceeds page height and adds new pages
  6. Text wrapping — Uses doc.splitTextToSize(text, maxWidth) for long text content
  7. Return bytes — Outputs new Uint8Array(doc.output('arraybuffer'))

Planned for v0.5 (Issue #158):

  • Export briefing as a professional legal memo in both PDF and DOCX formats
  • DOCX will use a template with proper legal document formatting
  • Will support the same block-to-section rendering pipeline as PDF