I’m working on a project that needs to create PDFs using Puppeteer in a Node.js backend and trigger this action from an Angular frontend. Here’s what I’ve got so far:
But I’m not sure if this is the right approach. How can I properly connect the Puppeteer function to an ng-click event in Angular? Any tips on best practices for this kind of setup?
Your approach is on the right track, but there are a few improvements we can make. First, consider using a POST request instead of GET for generating PDFs, as it’s more suitable for actions that change server state. Also, it’s a good practice to make your server-side function asynchronous.
This setup provides better error handling and follows RESTful principles more closely. Remember to add appropriate loading indicators and error messages in your UI for a better user experience.
I’ve actually implemented a similar feature in one of my projects. One thing I’d suggest is to consider sending the PDF back to the client instead of just a confirmation message. This way, you can offer immediate download or preview.
Here’s a rough idea of how you could modify your server code:
hey mate, i’ve done something similar before. instead of using $http.get, try $http.post and send any necessary data. on the server side, use app.post and make the generatePDF function async. wait for it to finish before sending the response. like this: