0%

How To Generate PDF with Laravle Dompdf

If you want to generate PDF file in your Laravel project, you may choose this package barryvdh/laravel-dompdf. Its a laravel wrapper for the most famous PDF generater packeage dompdf. With this package, you can easily generate PDF file with a view with just 3 lines of code.

Here is an example of how to generate PDF file.

Install the package

First use composer to install the package.

1
composer require barryvdh/laravel-dompdf

After updating composer, add the ServiceProvider to the providers array in config/app.php

1
2
3
4
'providers' => [
...
Barryvdh\DomPDF\ServiceProvider::class,
],

You can optionally use the facade for shorter code. Add this to your facades:

1
2
3
4
'aliases' => [
...
'PDF' => Barryvdh\DomPDF\Facade::class,
],

PDF view blade file

You can make a view file with plain html, in this example we generate a pdf file with a table.

  1. Create a view file purchase_order_pdf.blade.php
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Purchase Control System</title>
    <style>
    body {
    font-family: yahei;
    font-size: 12px;
    margin-top: 0 !important;
    padding-top: 0 !important;
    }

    </style>
    </head>

    <body>
    @if (Auth::guest())
    <div>You need to login first.</div>
    @else
    <div class="main">
    <table style="border: 1px solid black;border-collapse: collapse;" width="100%">
    <thead>
    <tr>
    <th style="border: 1px solid black; width:5%">ID</th>
    <th style="border: 1px solid black; width:50%">Item Name/Description</th>
    <th style="border: 1px solid black; width:10%">Quantity</th>
    <th style="border: 1px solid black; width:10%">Price</th>
    <th style="border: 1px solid black; width:5%">Discount</th>
    <th style="border: 1px solid black; width:5%">Tax</th>
    <th style="border: 1px solid black; width:15%">Amount</th>
    </tr>
    </thead>
    <tbody>
    @foreach ($items as $item)
    <tr align="center">
    <td style="border: 1px solid black">{{ $loop->iteration }}</td>
    <td style="border: 1px solid black ;" align="left">
    <strong>{{ $item->name }}</strong><br><span
    style="white-space: pre-wrap;">{{ $item->description }}</span>
    </td>
    <td style="border: 1px solid black">{{ $item->quantity }}</td>
    <td style="border: 1px solid black">{{ $item->price }}</td>
    <td style="border: 1px solid black">{{ $item->discount }}</td>
    <td style="border: 1px solid black">{{ $item->tax }}</td>
    <td style="border: 1px solid black">{{ $item->sub_total }}</td>
    </tr>
    @endforeach
    </tbody>
    </table>
    </div>
    @endif
    </body>

    </html>

Generate PDF code

You can use the following code to generate PDF file, just put this in your controller or service, then call the function to start the download from the blade file where you want.

1
2
3
4
5
6
7
8
9
public function generate_pdf(){
//load the view
$html = view('purchase_order_pdf')->with('items', $items)->render();
//generate PDF
PDF::setOptions(['dpi' => 96, 'isFontSubsettingEnabled' => true]);
$pdf = PDF::loadHTML($html);
return $pdf->download('purchase_order.pdf');
}

Tips

  1. set “isFontSubsettingEnabled” to true, otherwise the font will be embedded and the PDF file size will be huge, only contains plain text html page can get a PDF file around 9MB and with “isFontSubsettingEnabled” set to true, the PDF file size will be around 8KB. Use this method you no need to upload the font to the server.

  2. If your data contain some non-English characters like Chinese character, make sure you use the most common font for your target users, this sample is for the Chinese windows users, most of their system contains Yahei font by default.

    Add font-family: yahei; to your css.

    Otherwise the non-English characters will be shown as a blank sqare mark □ or totally can not be displayed.