All files / web/src/lib/flowcharts pdf-export.ts

0% Statements 0/64
0% Branches 0/1
0% Functions 0/1
0% Lines 0/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65                                                                                                                                 
/**
 * PDF Export for Flowcharts
 *
 * Client-side utility that calls the server-side API to generate
 * print-ready PDFs from Mermaid flowchart content using Typst.
 */

export interface FlowchartPDFOptions {
  /** Title to display on the PDF */
  title: string
  /** Optional description */
  description?: string
  /** Paper size */
  paperSize?: 'letter' | 'a4'
  /** Orientation */
  orientation?: 'portrait' | 'landscape'
  /** Flowchart ID (for built-in flowcharts, used for QR code remix link) */
  flowchartId?: string
  /** Workshop session ID (for draft flowcharts, used for QR code link) */
  sessionId?: string
}

/**
 * Downloads a flowchart as a PDF
 *
 * Sends the mermaid content to the server, which renders it to SVG
 * and generates a PDF using Typst. Includes a QR code for remixing.
 */
export async function downloadFlowchartPDF(
  mermaidContent: string,
  options: FlowchartPDFOptions
): Promise<void> {
  const response = await fetch('/api/flowcharts/pdf', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      mermaidContent,
      title: options.title,
      description: options.description,
      paperSize: options.paperSize || 'letter',
      orientation: options.orientation || 'portrait',
      flowchartId: options.flowchartId,
      sessionId: options.sessionId,
    }),
  })

  if (!response.ok) {
    const error = await response.json().catch(() => ({ error: 'Unknown error' }))
    throw new Error(error.error || 'Failed to generate PDF')
  }

  // Get the PDF blob and trigger download
  const blob = await response.blob()
  const url = URL.createObjectURL(blob)
  const link = document.createElement('a')
  link.href = url
  link.download = `${options.title.toLowerCase().replace(/\s+/g, '-')}-flowchart.pdf`
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
  URL.revokeObjectURL(url)
}