Tech Magazine

Laravel 5 Export to PDF using laravel-dompdf

42 86,848

Get real time updates directly on you device, subscribe now.

In this article, we will discuss Laravel 5 Export to PDF. I will show an example, so you can easily generate a pdf file for your Laravel App. We will create pdf from a view, in 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, Im assuming you are familiar with the basics of the Laravel framework. If no then you can start with given articles.

# Setup laravel-dompdf package through the composer

Firstly, we need to install the barryvdh/laravel-dompdf package. Its 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,]

Let publish the assets from the vendor using the given artisan command.

php artisan vendor:publish

After executing the above command, 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.

# Setup blade view template for PDF

We are creating the simple HTML table for this example. You can update this HTML as per your requirement.

Customer List

@foreach($data as $customer) @endforeach
ID Name Email Phone
{{ $customer->id }} {{ $customer->name }} {{ $customer->email }} {{ $customer->phone }}

In this, we are creating just HTML template where just generate a table. This table contains the customer details such as customer name, email, phone and etc. You can change the view as per your PDF required.

# Setup blade view to display all the listed records

In this view, we create a view which displays our customer list with the export button. Export button is used to trigger the export pdf functionality.


Customer List

Export PDF @foreach($data as $customer) @endforeach
ID Name Email Phone
{{ $customer->id }} {{ $customer->name }} {{ $customer->email }} {{ $customer->phone }}

# Setup Model

# Register routes for our app

Register the route for export pdf. When an URL customer/pdf hits the report generate and store on the server then download start. You can control the storage path as per your need.


# Update controller with export pdf function

Heres our controller function where all logics performed.

save(storage_path().'_filename.pdf');    // Finally, you can download the file using download function    return $pdf->download('customers.pdf');  }}

Finally, everythings ready now when we click on the export pdf button then download start. Feel free to comment if any query.

If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee

Get real time updates directly on you device, subscribe now.

  1. heshan says

    ErrorException (E_NOTICE)
    tempnam(): file created in the system’s temporary directory

    1. Code Briefly says

      Hi Heshan

      Add more details to explain your issue. It’s hard to regenerate the similar situation.

      1. heshan says

        public function generatePDF()


        $students = Student::all();

        $pdf = PDF::loadView(‘admin.index’, compact(‘students’));

        return $pdf->download(‘page.pdf’);


        this is my code when i run the error shows i try to change the temp directory in php.ini but nothing changes

  2. rassloff says

    how can I made the PDF more dynamic. so that the table ends on each side with a summary how mach data were printed on this page. and then on the new page: the tmp summary of the last page as first line of the table….

  3. Andrei says

    Hello! I am using your example but I get this: fopen(/home/sites/public_html/victorylive/storage/fonts//c20bfe36614e3b5d60369198deb5d1ad.ufm): failed to open stream: No such file or directory

    Laravel 5.6.2 and PHP 7.2.0

    1. Code Briefly says

      Hi Andrei

      This example is working fine at my end. Not able to regenerate this issue.
      Please check once at your end.

      Thanks 🙂

    2. Jason says

      You just need to go to /storage and create a /fonts folder with the right permission settings. chmod 777 fonts

      1. Shraddha says

        Thankyou so much

  4. ali reza says

    can we use customized table with external css including it??

    1. Code Briefly says

      Hello Ali,
      Yes, you can use 🙂

  5. Emmanuelle says

    how about in specific ID how can i print it out ?

    1. Code Briefly says

      Hi Emmanuele

      Will you please describe your issue. So I can help you. 🙂

  6. Waseem says

    I am Waseem Asghar and New laravel web developer. Can you please suggest me book for laravel pdf creator.


  7. Ax3 says

    Hi, I need some help. When I genetare the pdf, it does not display any style form the css. I have it on a partial view, and call it using blade’s @include() method to a complete view. But, after that, when I click the button to generate the pdf, it does it but whitout styling it. Any ideas?

  8. Miley Cyrus says

    Your article Awesome Thanks for this information, very informative as well as Modern.

  9. jeco says

    I have an issue, Undefined variable: agencies (View: C:\xampp\htdocs\ecoteneo\resources\views\agencypdf.blade.php)

    i follow your code:


    public function export_pdf()

    $agencies = Agency::get();

    $pdf = PDF::loadView(‘agencypdf’, $agencies);


    return $pdf->download(‘agencies.pdf’);

    my blade for pdf:

    Agency List


    @foreach($agencies as $Agency)

    {{ $Agency->name }}
    {{ $Agency->email }}
    {{ $Agency->phone }}


    1. Pankaj Sood says

      Hi Jeco, Use “compact()” as given below.

      $pdf = PDF::loadView(‘agencypdf’, compact(‘agencies’));

      1. jeco says

        Thank you. I have another problem. That code works. I use it in another form and I just copy and paste that code. Now the problem for the another is it returns the blank page.

        if(Auth::user()->campus_id == 0){
        $processes = Process::join(‘bags’,’processes.bag_id’, ‘=’, ‘’)
        ->join(‘stations’,’processes.station_id’, ‘=’, ‘’)
        ->where(‘stations.campus_id’, Session::get(‘test’))
        ->orderBy(‘’, ‘desc’)

        $pdf = PDF::loadView(‘collectionReportspdf’, compact(‘processes’));


        return $pdf->download(‘collection-reports.pdf’);


        $processes = Process::join(‘bags’,’processes.bag_id’, ‘=’, ‘’)
        ->join(‘stations’,’processes.station_id’, ‘=’, ‘’)
        ->where(‘stations.campus_id’, Auth::user()->campus_id)
        ->orderBy(‘’, ‘desc’)

        $pdf = PDF::loadView(‘collectionReportspdf’, compact(‘processes’));


        return $pdf->download(‘collection-reports.pdf’);

        1. Jereh Tejano says

          Hi, are you sure that the query is returning any value?

          1. Code Briefly says

            Hi Jereh, please explain your issue.

        2. SalkinRah says

          Hi Jeco,

          I’ve got the same problem as you because I’ve added the laravel expressions “@section” and “@endsection” to my blade view template for PDF.

          These expression tells laravel, that the file is about a template file which should get included into your layout file (you added “@yield(‘content’) here, which always includes the html markup between the expressions mentioned above”).

          If you remove them (mentioned content-expressions), it works fine. 🙂

  10. Muhammad yasir says

    Thanks you very much keep it up!!

  11. Appurva Ambagade says

    Hey, nice tutorial but I’m getting this error Undefined variable: data

    here’s my controller method

    $data = DummyCreate::get();

    $pdf = PDF::loadView(‘DummyCreate.auditlist’,$data);


    return $pdf->download(‘dynamic_pdf.pdf’);

  12. TommyD says

    Hello i have problem, my view template is not showing dynamic variables from database.

    This is my controller:

    public function printPDF()
    { $users = \App\User::get();
    $pdf = PDF::loadView(‘users_pdfview’, compact(‘users’));
    return $pdf->download(‘users.pdf’); }

    This is my PDF view blade:

    Nr telefonu

    @foreach($users as $user)

    @php if ($user->is_admin==’1′) {
    @php }
    else {
    @php }


    I got only HTML code, for example “{{$user->id}}” is showing as “{{$user->id}}”, it looks like it are not downloading data from database. Can You help me?

    Thanks, Tom.

    1. mohamed says

      same eror are you fix it plz help me

      1. mohamed says

        same error do you fix it please help me

  13. nikhil garg says

    i am getting this error (Class ‘App\Http\Controllers\Customer’ not found)

  14. shaqayq says

    my persian data dont show in pdf
    what is the problem?

    1. Code Briefly says

      Did you add the fonts?

  15. Rvi says

    How to add font in storage/fonts folder please help me

  16. Arturas says

    file_put_contents(/var/www/biblioteka/storage_filename.pdf): failed to open stream: Permission denied

    lamp linux mint

  17. Sohanur Rahman says

    Invalid characters passed for attempted conversion, these have been ignored

  18. basheer alam says

    many many thanks for help code briefly

  19. Aleem Ali says

    Thanks a lot for sharing the code of PDFDOM..
    I need a help:
    return $pdf->download(‘welcome.pdf’);
    above code is download the pdf but i just want to display the generated pdf in new tab..
    I tried the below code but it’s not working for me..

    $pdf->stream(“welcome.pdf”, array(“Attachment” => 0));

  20. Andy says

    Hello, I am facing an issue the pdf that is created, is not opening and the viewer is showing an error of damaged or corrupted PDF.

    1. Pankaj Sood says

      Share more details, it’s hard to reproduce the same issue.

  21. Nitha Shivan says

    can u help me.I need only to generate pdf after execution of query not download .save it in server and mail the same to someone’s mail

  22. Manjeet says

    How can we disable copy text from created pdf. ?

  23. Pankaj says

    how to add watermark on pdf in laravel. if my value get from table? I am using dompdf.

  24. Grace says

    Hi Pankaj,
    Thank you for the help. I want to download only the values that have been selected in the database with a where clause. In the view my link “export to pdf” will have href=”mypage/{{$test->id}} but I want the $test->id to be an array of all the ids that have been selected in the database and are hold in the $test variable. I am new and don’t understand how I can proceed this. Could help on this.

    Thank you,

  25. Grace says

    Hi Pankaj,
    I wanted to rephrase my question. What if $data hold the result from a select * from table with where clause in the query and I want to pass all its return id in the url to be exported to pdf.

    Thank you,

  26. Charles Taylor says

    Thank you for this information, however i am working in a huge system and i need to generate user cvs in pdf.
    the user have profile and cv table separate, however i join them in one page called details.
    The details.blade.php has @extends(‘layouts.master’) and many more…

    I can generate the page but it comes with all other html stuff such as buttons, links, footer, menu… please help me on how to just generate the content of the details page without links or buttons or footer?

    Thanks in advanced

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More