Laravel — Create nice invoices with DomPDF (template!)

Dennis Smink
Sep 3, 2018 · 2 min read

So you have to create a basic but good looking invoice design for your client (or yourself) in PDF format?

Image for post
Image for post
The invoice design I will provide in this article

Chances are that you’re using the laravel-dompdf package from Barryvdh, if so; good. Because this tutorial assumes you are using this package, I have not tried it with the original DomPDF wrapper though.

In my experience designing and giving a nice structural form to invoice templates has always been a pain in DomPDF, this is why I want to share this template with you.

Don’t worry there are no fancy tricks involved, just a plain template which you can adjust to your needs. Get it right here:

I always render the PDF’s from a method inside the Order.php model:

public function getPdf($type = 'stream')
{
$pdf = app('dompdf.wrapper')->loadView('order-pdf', ['order' => $this]);

if ($type == 'stream') {
return $pdf->stream('invoice.pdf');
}

if ($type == 'download') {
return $pdf->download('invoice.pdf');
}
}
...return $order->getPdf(); // Returns stream defaultreturn $order->getPdf('download'); // Returns the PDF as download

If you stream the PDF, it will render to your browser. If you download the PDF, as expected it will download the invoice.

I hope this article helped you getting started with a nice basic design for PDF invoice!

Note: please bare in mind that I am a Dutch developer, English is not my native language so this article could contain any grammatical errors.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store