Laravel 5 Export To PDF Using Laravel-Dompdf

In this article, we will discuss Laravel 5 Export to PDF. I will show an example on the invoice, so you can easily generate a pdf file for your Laravel App and export view into PDF.  This view, we will write the HTML code and load data dynamically from the database as per the requirement. After that, we will export this view as a PDF file.

Before proceeding, I’m assuming you are familiar with the basics of the Laravel framework. If no then you can start with given articles.

Laravel Setup
Laravel Default Authentication Setup

Setup laravel-dompdf package through the composer

Firstly, we need to install the barryvdh/laravel-dompdf package. It’s easy to install using given composer command:

composer require barryvdh/laravel-dompdf

Configure package with the Laravel App

After executing the above command, time to update Service Provider to the providers and facade to the aliases array in config/app.php.

'providers' => [
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
'PDF' => Barryvdh\DomPDF\Facade::class,
]

Publish the assets from the vendor using the given artisan command.

php artisan vendor:publish

After executing the above command, the list of available packages shown. You can select as per your need. Now, we are selecting the Provider: Barryvdh\DomPDF\ServiceProvider and the new file is copied to the config/dompdf.php. This file contains the global settings for the dompdf package. Here are my settings most important for me is where we can save the invoice file.

<?php
return array(
'show_warnings' => false, // Throw an Exception on warnings from dompdf
'orientation' => 'portrait',
'defines' => array(
"font_dir" => storage_path('invoices/'), // advised by dompdf (https://github.com/dompdf/dompdf/pull/782)
"font_cache" => storage_path('invoices/'),
"temp_dir" => sys_get_temp_dir(),
"chroot" => realpath(base_path()),
"pdf_backend" => "CPDF",
"default_media_type" => "screen",
"default_paper_size" => "a4",
"default_font" => "serif",
"dpi" => 96,
"enable_php" => false,
"enable_javascript" => true,
"enable_remote" => true,
"font_height_ratio" => 1.1,
"enable_html5_parser" => false,
),
);

Setup blade view template for PDF

We are creating the simple HTML view invoice blade file: invoice.blade.php for this example. You can update this HTML as per your requirement.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Invoice - 1000</title>
    <style>
        .clearfix:after {
            content: "";
            display: table;
            clear: both;
          }
          .place {
            text-align: right;
            margin-top: 30px;
          }
          .bank {
            width: 100%;
            height: 70%;
            background: #ffff;
            border: none;
            font-size: 14px;
            font-family: Arial;
            color: black;
            overflow:hidden; 
            resize:none;
          }
          a {
            color: #5D6975;
            text-decoration: underline;
          }
          
          body {
            position: relative;
            width: 19cm;  
            height: 26cm;
            margin: 0 auto; 
            color: black;
            background: #FFFFFF; 
            font-family: DejaVu Sans, sans-serif; 
            font-size: 12px;
          }
          header {
            padding: 10px 0;
          }
          
          #logo {
            float: left;
            margin-bottom: 10px;
          }
          
          #logo img {
            height: 80px;
          }
          
          h1 {
            border-top: 1px solid  #5D6975;
            border-bottom: 1px solid  #5D6975;
            color: black;
            font-size: 2.4em;
            line-height: 1.4em;
            font-weight: normal;
            text-align: left;
            margin: 0 0 20px 0;
          }
          h2 {
            color: black;
            font-size: 1.4em;
            line-height: 1.4em;
            font-weight: normal;
            margin: 10px 10px 20px 0;
            text-align: center;
          }
          .inv {
          }
          .users {
            display: block;
            margin-right: 30px;
            margin-left: 30px;
          }
          .seller {
            float: left;
            font-size: 16px;
            margin-bottom: 30px;
          }
          .company {
            float: right;
            font-size: 16px;
            margin-bottom: 30px;
          }
          
          #seller div,
          #company div {
            white-space: nowrap;        
          }
          table {
            width: 100%;
            border-collapse: collapse;
            border-spacing: 0;
            margin-bottom: 20px;
          }
          table tr:nth-child(2n-1) td {
            background: #F5F5F5;
          }
          table th,
          table td {
            text-align: right;
          }
          table th {
            padding: 5px 5px;
            color: #5D6975;
            border-bottom: 1px solid #C1CED9;
            white-space: nowrap;        
            font-weight: normal;
          }
          table .service,
          table .desc {
            text-align: left;
          }
          table td {
            padding: 5px;
            text-align: right;
          }
          table td.service,
          table td.desc {
            vertical-align: top;
          }
          table td.unit,
          table td.qty,
          table td.total {
            font-size: 1.2em;
          }
          table td.grand {
            border-top: 1px solid #5D6975;;
          }
          #notices .notice {
            color: #5D6975;
            font-size: 1.2em;
            margin-bottom: 20px;
          }
          footer {
            color: #5D6975;
            width: 100%;
            height: 30px;
            position: absolute;
            bottom: 0;
            border-top: 1px solid #C1CED9;
            padding: 8px 0;
            text-align: center;
          }
          .total {
            font-family: Arial; 
            font-size: 14px;
          }
    </style>
  </head>
  <body>
    <header class="clearfix">
      <div id="logo">
        <img src="/logo.png">
      </div>
      <div class="place">
        <div>Place of issue: Country, City</div>
        <div>Date of issue: 2019-05-06 12:00:00</div>
        <div>Date of sale: 2019-05-06 12:00:00</div>
      </div>
      <div class="inv">
          <h2 style="float: right"><strong>ORYGINAL/COPY</strong></h2>
          <h1>INVOICE Nr - <strong>1000</strong></h1>
          <div class="users">
            <div class="company">
              <div><strong>SELLER: </strong></div>
              <div>Company name</div>
              <div>Address</div>
              <div>Country, City</div>
              <div>Tax ID</div>
            </div>
            <div class="seller">
              <div><strong>BUYER: </strong></div>
              <div>Company Name</div>
              <div>Address</div>
              <div>Country, City</div>
              <div>Tax ID</div>
            </div>
          </div>
      </div>
    </header>
    <main>
      <table>
        <thead>
          <tr>
            <th>PRODUCT</th>
            <th>NET</th>
            <th>VAT %</th>
            <th>VAT AMOUNT</th>
            <th>PST %</th>
            <th>PST AMOUNT</th>
            <th>GROSS</th>
          </tr>
        </thead>
        <tbody>
          @foreach ($items as $item)
          <tr>
            <td class="desc">{{ $item->service }}</td>
            <td class="total">{{ $item->net }}</td>
            <td class="qty total">24</td>
            <td class="total">{{ $item->vat }}</td>
            <td class="unit total">5</td>
            <td class="total">{{ $item->pst }}</td>
            <td class="total">{{ $item->gross }}</td>
          </tr>
          @endforeach
          <tr>
            <td colspan="6" class="grand total">TOTAL NET</td>
            <td class="grand total">USD {{ $invoice->total_net }}</td>
          </tr>
          <tr>
            <td colspan="6">VAT 24%</td>
            <td class="total">USD {{ $invoice->total_vat }}</td>
          </tr>
          <tr>
            <td colspan="6">PST 5%</td>
            <td class="total">USD {{ $invoice->total_pst }}</td>
          </tr>
          <tr>
            <td colspan="6" class="grand total"><strong>TOTAL DUE</strong></td>
            <td class="grand total"><strong>USD {{ $invoice->total_gross }}</strong></td>
          </tr>
        </tbody>
      </table>
      <!--Payment Terms -->
      <div id="notices">
          <div class="notice"><strong>Terms of Payment: payment terms</strong></div>
      </div>
      <!--Bank details -->
      <div>
        <div><strong>Bank Details:</strong></div>
        <textarea class="bank" disabled rows='17'>bank details</textarea>
      </div>
    </main>
    <footer>
      Invoice was created on a computer and is valid without the signature and seal.
    </footer>
  </body>
</html>

In this, we are creating just an HTML template where just generate a items table. This table contains invoice details.

Register routes for our app

Register the route for export pdf. When an URL /invoices_export{id} where {id} is the invoice id from database. You can control the storage path as per your need.

Route::get('/invoices_export{id}','InvoicesController@export_pdf');

Helper function to create invoice items

function invoice($id){

    $items = \DB::table('invoice_items')
        ->select('service', 'gross', 'net', 'vat', 'pst')
        ->where('id_invoice', $id)
        ->get();
    
    $pdf = PDF::loadView('invoice', compact('items'));
    return [$pdf, $inv_number];
}

Update controller with export pdf function

Here’s our controller function where all logics performed.

    public function export_pdf($id)
    {
        $pdf = invoice($id);
        return $pdf[0]->download('inv_'.$pdf[1].'.pdf');
    }

Finally, everything’s ready now when you execute GET https://localhost//invoices_export1000 to download the PDF file with the invoice. Feel free to comment if any query.

,

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu