Skip to main content

Fall Risk PDF Report API

Overview

API endpoint to generate and download PDF reports for fall risk assessments. This allows multiple parts of the application to generate PDF reports without duplicating the PDF generation logic.


GET /api/fall-risk-results/[fallRiskResultId]/report

Generates and downloads a PDF report for a fall risk assessment result using Puppeteer to render the player report page.

Authentication

Requires valid Firebase ID token or ClientToken in headers:

  • Authorization: Bearer <firebase_id_token>
  • OR ClientToken: <client_token>

Path Parameters

ParameterTypeRequiredDescription
fallRiskResultIdstringYesThe unique fall risk result ID

Query Parameters

ParameterTypeRequiredDescription
uidstringYesUser/player ID for fetching report data

Response

  • 200 OK: Returns PDF file with appropriate headers
    • Content-Type: application/pdf
    • Content-Disposition: attachment; filename*=UTF-8''[encoded_filename]
    • Cache-Control: no-cache, no-store, must-revalidate
  • 400 Bad Request: Missing required parameter (uid)
  • 401 Unauthorized: Invalid or missing authentication
  • 404 Not Found: Fall risk result not found
  • 500 Server Error: PDF generation failed

Usage Examples

Client-Side Download

// From client-side code
const user = AUTH.currentUser;
const token = await user.getIdToken();

const response = await fetch(
`/api/fall-risk-results/${fallRiskResultId}/report?uid=${uid}`,
{
headers: { Authorization: `Bearer ${token}` },
}
);

if (response.ok) {
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "report.pdf";
a.click();
window.URL.revokeObjectURL(url);
}

Admin Dashboard Usage

// Example: From an admin dashboard
async function downloadReport(fallRiskResultId: string, uid: string) {
const token = await getAuthToken();

const response = await fetch(
`/api/fall-risk-results/${fallRiskResultId}/report?uid=${uid}`,
{
headers: { Authorization: `Bearer ${token}` },
}
);

if (!response.ok) {
throw new Error("Failed to generate report");
}

// Download the PDF
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `fall-risk-report-${uid}.pdf`;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}

Implementation Details

PDF Generation Method

The API uses Puppeteer to:

  1. Navigate to the player report page (/b2b/player/[uid]?pdf=true)
  2. Pass authentication headers for data access
  3. Wait for the PDF content to render
  4. Generate a PDF from the rendered page
  5. Return the PDF as a downloadable file

Environment Support

EnvironmentChromium Package
Production (Vercel)@sparticuz/chromium-min
Local DevelopmentLocal Chrome installation
Local Development

For local development, you can configure the Chrome path via the CHROME_PATH environment variable. Default path on Windows is C:\Program Files\Google\Chrome\Application\chrome.exe.


Benefits

  1. Centralized Logic: PDF generation logic is centralized in one place
  2. Reusable: Multiple parts of the application can use the same endpoint
  3. Server-Side Rendering: More reliable PDF generation using server-side rendering
  4. Consistent Output: All reports have consistent formatting and styling
  5. Better Organization: Fall-risk specific components are properly grouped