How I create a invoice to PDF
Fermin Perdomo
In this post, I will teach you how to generate a pdf from your invoice data.
The
2. Set Up the Controller to Use
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.
Newsletter
Get new posts delivered straight to your inbox.
Great Tools for Developers
Git Tower
Get Started - It's FreeA powerful Git client for Mac and Windows that simplifies version control.
Mailcoach
Start freeSelf-hosted email marketing platform for sending newsletters and automated emails.
Uptimia
Start freeWebsite monitoring and performance testing tool to ensure your site is always up and running.
Cloudways
Start freeManaged cloud hosting platform that simplifies server management for developers.
Comments
No comments yet. Be the first to share your thoughts.
chat_bubble
Join the conversation โ log in to leave a comment.
Log In