Home » Laravel PDF: Generate PDF Files Superfast

Laravel PDF: Generate PDF Files Superfast

Loved it? Please Share on Social Media

Introduction to Laravel PDF

Hey! What’s up? Everybody hopes you are doing & coding well; today, in this tutorial, we will learn about the Laravel PDF package installation process. Now, please follow the step-by-step guide.

Installing Laravel PDF Package

To install the Laravel PDF package on your local machine, download/clone the package from GitHub. Follow the command below to download the package.

composer require spatie/laravel-pdf

Well, our package download is complete. Now, we will see some primary usage of this package. Follow the code below on your end.

Sample Code Snippet for Laravel PDF

use Spatie\LaravelPdf\Facades\Pdf;
Pdf::view('pdf.order-invoice', ['order-invoice' => $order_invoice])
     ->format('a4')
     ->name('your-invoice.pdf');
     ->save('/assets/orders/invoice.pdf');

As you can see, the Laravel PDF package provides tons of features on their package. You need to follow the requirements of your project.

Using JavaScript in your PDF file

Sometimes, you must show your charts or something on your pdf file. So, in this case, you must use the following snippet to initiate JavaScript on your PDF file and get it up and running.

<div id="target"></div>

<script>
    document.getElementById('target').innerHTML = 'hello';
</script>

use Spatie\LaravelPdf\Facades\Pdf;
Pdf::view('orders')->save('/assets/orders/invoice.pdf');

Saving PDFs to your Cloud Disks

Additionally, you can save your PDF files in your preferred location. I’m using Amazon S3/Cloud to store my PDF files.

use Spatie\LaravelPdf\Facades\Pdf;

Pdf::view('order_invoice')
   ->disk('s3')
   ->save('order-invoice-april13th.pdf');

Setting Defaults of Laravel PDF package

use Spatie\LaravelPdf\Facades\Pdf;
use Spatie\LaravelPdf\Enums\Format;

Pdf::default()
    ->headerView('pdf.header')
    ->paperFormat(Format::A4);

We can customize the default options given by the Laravel PDF package provided.

// You can use HTML in your pdf. For that, you have to use the HTML function.
Pdf::html('<div>Your HTML Content</div>')
    ->save('order-invoice.pdf')

// You can modify your paper format using the bellow code snippet.
Pdf::html('<div>Your HTML Content</div>')
   ->paperFormat(PaperFormat::A4)
   ->save('order-invoice.pdf')

Creating PDFs with Multiple Pages with the Laravel PDF Package

// You must use the @pageBreak blade component for the page break.
<div>
    Page 1
</div>

@pageBreak

<div>
    Page 2
</div>

use Spatie\LaravelPdf\Facades\Pdf;
Pdf::view('your-content-with-multiple-pages')->save('pdfs/');

Adding Page Numbers to Your PDF Document

Another essential feature is using page numbers in our PDF document. We must use our document’s @pageNumber or @totalPages blade component for this. You can see the snippet below.

<div>This is page @pageNumber of @totalPages</div>

use Spatie\LaravelPdf\Facades\Pdf;
Pdf::view('yourcontent-with-multiple-pages')->footerView('your-footer-view')->save('pdfs');

Generating PDFs on AWS Lambda Cloud Hosting

If you generate your PDF files using your local server resources, that can be very unusual sometimes because of the heavy server load. Instead, you can use the AWS lambda function to generate your PDF files in a cloud environment.

use Spatie\LaravelPdf\Facades\Pdf;
Pdf::view('order.invoice', $order)->onLambda()->save('order-invoice.pdf');

You can create all your PDFs in your app on Lambda. For this, you have to set the lambda function globally for your application. You can follow the below snippet. You can checkout the package documentation for more.

use Spatie\LaravelPdf\Facades\Pdf;
Pdf::default()->onLambda();

Stylize your Order Invoice Using Tailwind CSS

You can stylize your invoice file using the excellent Tailwind CSS framework. You may use other CSS frameworks for your work. I will use the Tailwind CSS framework because its design is charming. So, now you have to copy and paste the snippet into the code editor.

<html lang="en">
<head>
    <title>Order Invoice - Laravel PDF Tutorial</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body>

<div class="px-2 py-8 max-w-xl mx-auto">
    <div class="flex items-center justify-between mb-8">
        <div class="flex items-center">
            <div class="text-gray-700 font-semibold text-lg">Company Name</div>
        </div>
        <div class="text-gray-700">
            <div class="font-bold text-xl mb-2 uppercase">Invoice</div>
            <div class="text-sm">Date: 02/08/2024</div>
            <div class="text-sm">Invoice #: {{ $invoiceNumber }}</div>
        </div>
    </div>
    <div class="border-b-2 border-gray-300 pb-8 mb-8">
        <h2 class="text-2xl font-bold mb-4">Invoice To:</h2>
        <div class="text-gray-700 mb-2">{{ $customerName }}</div>
        
        <div class="text-gray-700 mb-2">123 Martin Blvd St.</div>
        <div class="text-gray-700 mb-2">Baltimore, Maryland, USA 12345</div>
        <div class="text-gray-700">cris@example.com</div>
    </div>
    <table class="w-full text-left mb-8">
        <thead>
        <tr>
            <th class="text-gray-700 font-bold uppercase py-2">Description</th>
            <th class="text-gray-700 font-bold uppercase py-2">Quantity</th>
            <th class="text-gray-700 font-bold uppercase py-2">Price</th>
            <th class="text-gray-700 font-bold uppercase py-2">Total</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td class="py-4 text-gray-700">Awesome Product 1</td>
            <td class="py-4 text-gray-700">7</td>
            <td class="py-4 text-gray-700">$130.00</td>
            <td class="py-4 text-gray-700">$170.00</td>
        </tr>
        <tr>
            <td class="py-4 text-gray-700">Awesome Product 2</td>
            <td class="py-4 text-gray-700">6</td>
            <td class="py-4 text-gray-700">$80.00</td>
            <td class="py-4 text-gray-700">$700.00</td>
        </tr>
        <tr>
            <td class="py-4 text-gray-700">Awesome Product 3</td>
            <td class="py-4 text-gray-700">9</td>
            <td class="py-4 text-gray-700">$95.00</td>
            <td class="py-4 text-gray-700">$925.00</td>
        </tr>
        </tbody>
    </table>
    <div class="flex justify-end mb-8">
        <div class="text-gray-700 mr-2">Subtotal:</div>
        <div class="text-gray-700">$1145.00</div>
    </div>
    <div class="text-right mb-8">
        <div class="text-gray-700 mr-2">Tax:</div>
        <div class="text-gray-700">$35.50</div>

    </div>
    <div class="flex justify-end mb-8">
        <div class="text-gray-700 mr-2">Total:</div>
        <div class="text-gray-700 font-bold text-xl">$3460.50</div>
    </div>
    <div class="border-t-2 border-gray-300 pt-8 mb-8">
        <div class="text-gray-700 mb-2">Payment is due within 30 days. Late payments are subject to fees.</div>
        <div class="text-gray-700 mb-2">Please make checks payable to Your Company Name and mail to:</div>
        <div class="text-gray-700">123 Martin Blvd St.Baltimore, Maryland, USA 12345</div>
    </div>
</div>

</body>
</html>

You must add the lines of codes into your controller.

namespace App\Http\Controllers;
use function Spatie\LaravelPdf\Support\pdf;
class DownloadOrderInvoiceController
{
    public function __invoke()
    {
        return pdf('order.invoice', [
            'invoiceNumber' => '5566',
            'customerName' => 'Chris Haris',
        ]);
    }
}

Conclusion:

Finished! That’s all about our Laravel PDF package tutorial. Although this tutorial cannot make you a super expert, it will give you some fundamental knowledge needed in the future. Thank you so much for reading the whole tutorial from the beginning. If this tutorial helps you a little bit, then remember to share this post on social media. You can Contact Us if you have any queries.


Loved it? Please Share on Social Media

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.