Skip to content

Instantly share code, notes, and snippets.

@03balogun
Last active February 27, 2025 07:07
Show Gist options
  • Select an option

  • Save 03balogun/8f69cc2754c163fd1305ed2da2f5360c to your computer and use it in GitHub Desktop.

Select an option

Save 03balogun/8f69cc2754c163fd1305ed2da2f5360c to your computer and use it in GitHub Desktop.
// app/api/report/route.ts
import {ErrorReport, Report} from "@/components/reports/Report"; // Import components
export async function GET(req: NextRequest) {
const response = await makeAPIRequestToTheBackend()
const component = response?.status
? Report({record: response?.data})
: ErrorReport({message: response?.message || 'An error occurred generating your report'})
const stream = (await renderToStream(component)) as unknown as ReadableStream;
const blob = await new Response(stream).blob();
const name = `expenseai-report-${Date.now()?.toString()}`
const headers = {
"Content-Type": "application/pdf",
"Cache-Control": "no-store, max-age=0",
"Content-Disposition": `inline; filename=${name}.pdf`
}
return new Response(blob, {
headers
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment