Programmatically render a TSX/JSX Page from inside an API Route (in order to generate a PDF file).
Unanswered
Africanized honey bee posted this in #help-forum
Africanized honey beeOP
Hi! I'm somewhat experienced with Next.js but don't have a deep understanding of its inner workings.
We have an API endpoint that is responsible for generating a PDF report. Originally, we were calling the Page route via
I'm hoping that there's a way to do the following (user flow):
1. API user calls endpoint:
2. API endpoint "POST: Recipe" parses and validates the Recipe - the Recipe table has a "pdf" column which needs to store a PDF generated from a TSX file
3. within
* side note: we're using TailwindCSS for styling
* side note: images associated with the Recipe are saved to an S3 bucket so all images are full/absolute HTTPS urls
4. within
5. the PDF file is then added to the Recipe object and saved to the DB
I have had some experience with a few other MVC server side frameworks in the past and recall that a few of them support the programmatic loading/compiling of another route at runtime directly on the Server. I'm hoping that Next.js has some way of doing this as well.
Has anyone ever tried something like this with Next.js?
Version of Next.js:
We have an API endpoint that is responsible for generating a PDF report. Originally, we were calling the Page route via
fetch()
API in order to retrieve the fully rendered HTML page but this adds unwanted complexity and latency.I'm hoping that there's a way to do the following (user flow):
1. API user calls endpoint:
POST /api/receipe
2. API endpoint "POST: Recipe" parses and validates the Recipe - the Recipe table has a "pdf" column which needs to store a PDF generated from a TSX file
3. within
POST: Recipe
Route handler, the handler function loads something like the Next.js Router and asks it to "render" the /pages/recipe/[id]
Route points to TSX file that exports a React Component function that receives one parameter, namely, receipeData
* side note: we're using TailwindCSS for styling
* side note: images associated with the Recipe are saved to an S3 bucket so all images are full/absolute HTTPS urls
4. within
POST: Receipe
handler, the compiled HTML page is saved to a const recipeHtml
as a string and then we pass that on to Puppeteer to render the HTML and generate a binary PDF file5. the PDF file is then added to the Recipe object and saved to the DB
I have had some experience with a few other MVC server side frameworks in the past and recall that a few of them support the programmatic loading/compiling of another route at runtime directly on the Server. I'm hoping that Next.js has some way of doing this as well.
Has anyone ever tried something like this with Next.js?
Version of Next.js:
^14.2.8