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
| Parameter | Type | Required | Description |
|---|---|---|---|
fallRiskResultId | string | Yes | The unique fall risk result ID |
Query Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
uid | string | Yes | User/player ID for fetching report data |
Response
- 200 OK: Returns PDF file with appropriate headers
Content-Type: application/pdfContent-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:
- Navigate to the player report page (
/b2b/player/[uid]?pdf=true) - Pass authentication headers for data access
- Wait for the PDF content to render
- Generate a PDF from the rendered page
- Return the PDF as a downloadable file
Environment Support
| Environment | Chromium Package |
|---|---|
| Production (Vercel) | @sparticuz/chromium-min |
| Local Development | Local 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
- Centralized Logic: PDF generation logic is centralized in one place
- Reusable: Multiple parts of the application can use the same endpoint
- Server-Side Rendering: More reliable PDF generation using server-side rendering
- Consistent Output: All reports have consistent formatting and styling
- Better Organization: Fall-risk specific components are properly grouped
Related
- Fall Risk Results API - Get fall risk assessment results
- Fall Risk API - Create new fall risk assessments