In this episode, we look at how to download invoices as PDF files in our Laravel Cashier integration. The process is pretty simple: since each invoice from Stripe has a unique ID, we start by displaying this ID so we can identify which invoice we're working with.
Next, we jump into the subscription controller and add a new method to handle the invoice download. Using the billable trait on our user model, all we need to do is call the downloadInvoice
method, passing in the invoice ID from our request.
We then set up a new route in our web routes file that points to our new method and expects the invoice ID in the query string. After wiring up an anchor tag in our frontend with the correct route and parameters, users can click to download their invoices.
Before everything works smoothly, we need to require the dompdf/dompdf
package since it's used under the hood to generate the PDF. Once that's installed, we can try it out, download invoices, and see all the company and product info in the PDF—just like you'd expect from an invoice download feature!