How I create a invoice to PDF

Fermin Perdomo Fermin Perdomo
โ€ข โ€ข schedule 2 min read

In this post, I will teach you how to generate a pdf from your invoice data.

The loadView method is commonly used with PDF generation packages like barryvdh/laravel-dompdf to convert a Blade view into a PDF document.Hereโ€™s a step-by-step example of how to use loadView:1. Install the Required PackageIf you haven't already, install barryvdh/laravel-dompdf:
2. Set Up the Controller to Use loadViewIn your controller, you can use loadView to load a Blade view and generate a PDF.3. Create the Blade View for the Invoice<!DOCTYPE html> <html> <head> <title>Invoice #{{ $order->id }}</title> <style> body { font-family: Arial, sans-serif; } .invoice-box { max-width: 800px; margin: auto; padding: 30px; border: 1px solid #eee; } table { width: 100%; line-height: inherit; text-align: left; border-collapse: collapse; } table td { padding: 5px; vertical-align: top; } table tr.heading td { background: #eee; border-bottom: 1px solid #ddd; font-weight: bold; } table tr.item td { border-bottom: 1px solid #eee; } table tr.total td { border-top: 2px solid #eee; font-weight: bold; } </style> </head> <body> <div class="invoice-box"> <h2>Invoice #{{ $order->id }}</h2> <p>Date: {{ $order->created_at->format('d/m/Y') }}</p> <p>Customer: {{ $order->user->name }}</p> <table> <tr class="heading"> <td>Item</td> <td>Quantity</td> <td>Price</td> </tr> @foreach($order->items as $item) <tr class="item"> <td>{{ $item->name }}</td> <td>{{ $item->pivot->quantity }}</td> <td>${{ number_format($item->pivot->price, 2) }}</td> </tr> @endforeach <tr class="total"> <td></td> <td>Total:</td> <td>${{ number_format($order->total, 2) }}</td> </tr> </table> </div> </body> </html> 4. Define the RouteAdd the route to your web.php file:5. Access the PDFNow, visit:http://your-app-url/invoice/1/downloadThis will generate and download a PDF file named invoice_1.pdf based on the Blade template.

Reactions

lock You need to be logged in to react.
Log In

Newsletter

Get new posts delivered straight to your inbox.

mail

Great Tools for Developers

Git Tower

Git Tower

A powerful Git client for Mac and Windows that simplifies version control.

Visit arrow_forward
Mailcoach

Mailcoach

Self-hosted email marketing platform for sending newsletters and automated emails.

Visit arrow_forward
Uptimia

Uptimia

Website monitoring and performance testing tool to ensure your site is always up and running.

Visit arrow_forward
Cloudways

Cloudways

Managed cloud hosting platform that simplifies server management for developers.

Visit arrow_forward

Comments

No comments yet. Be the first to share your thoughts.

chat_bubble Join the conversation โ€” log in to leave a comment.
Log In