Laravel & mPDF

mPDF with Laravel quick Tutorial

From: tristan | Comments: 0

MPDF is such a cool library for creating PDF documents by using HTML. Headers, content and footers are made by just some lines of codes and you have the full control by every element with HTML and CSS. For a while I start using this library and I fell in love with mPDF. Here is a quick tutorial about how to use mPDF in Laravel 8 or higher.

  • Step 1 – Install mPDF with Composer
  • Step 2 – Create a new Controller and get access to mPDF library
  • Step 3 – Use Blade as Template

Install mPDF with Composer

Open you command prompt and go to your project folder.

$ composer require mpdf/mpdf

Now you are ready to use mPDF everywhere in your app.

Create a new Controller and get access to mPDF library

First let us insert a new route in our routes/web.php file. So we can access the Controller with a valid URL.

Route::get('/fun', [App\Http\Controllers\FunController::class, 'document']);

Open your command prompt again and create a new Controller with Artisan like this:

$ php artisan make:controller FunController

Open the FunController.php file and add the mPDF library and storage facade to the top of our Controller.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \Mpdf\Mpdf as PDF; 
use Illuminate\Support\Facades\Storage;

Now we can add some code to create our first PDF document.

public function document()
    {
        // Setup a filename 
        $documentFileName = "fun.pdf";

        // Create the mPDF document
        $document = new PDF( [
            'mode' => 'utf-8',
            'format' => 'A4',
            'margin_header' => '3',
            'margin_top' => '20',
            'margin_bottom' => '20',
            'margin_footer' => '2',
        ]);     

        // Set some header informations for output
        $header = [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'inline; filename="'.$documentFileName.'"' 
        ];

        // Write some simple Content
        $document->WriteHTML('<h1 style="color:blue">TheCodingJack</h1>');
        $document->WriteHTML('<p>Write something, just for fun!</p>');
        
        // Save PDF on your public storage 
        Storage::disk('public')->put($documentFileName, $document->Output($documentFileName, "S"));
        
        // Get file back from storage with the give header informations
        return Storage::disk('public')->download($documentFileName, 'Request', $header); //
    }

First we created a variable with the filename of the document. Then we create a new mPDF object with some initial setup data. In the $header variable we setup the HTTP header informations to output the PDF from storage into our browser. With $document->WriteHtml() we insert some HTML Code into the document. You can use almost any HTML Tag, just play a bit around to get a feeling! After that we store the file into our Laravel public storage (of course you can use any storage you want) and output it again.

Type into your browser the domain of your app with the before selected path in our route file, ie. testapp.test/fun. You should see your first mPDF generated PDF now.

Use Blade as Template

As we use simple HTML to create the PDF, we can also generate the PDF with Blade as our Template engine. This is so simple, check the code!

$document->WriteHTML(view('fun.testtemplate'));

Isn’t it easy? You can use blade as usual with all the tools and helpers and even create components etc…

Here again the entire code of the controller:

Now it’s up to you to change the code for your needs. Checkout the website of mPDF https://mpdf.github.io/ for further informations.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \Mpdf\Mpdf as PDF;
use Illuminate\Support\Facades\Storage;

class FunController extends Controller
{
    
    public function document()
    {
        // Setup a filename 
        $documentFileName = "fun.pdf";

        // Create the mPDF document
        $document = new PDF( [
            'mode' => 'utf-8',
            'format' => 'A4',
            'margin_header' => '3',
            'margin_top' => '20',
            'margin_bottom' => '20',
            'margin_footer' => '2',
        ]);     

        // Set some header informations for output
        $header = [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'inline; filename="'.$documentFileName.'"' 
        ];

        // Write some simple Content
        $document->WriteHTML('<h1 style="color:blue">TheCodingJack</h1>');
        $document->WriteHTML('<p>Write something, just for fun!</p>');

        // Use Blade if you want
        //$document->WriteHTML(view('fun.testtemplate'));
        
        // Save PDF on your public storage 
        Storage::disk('public')->put($documentFileName, $document->Output($documentFileName, "S"));
        
        // Get file back from storage with the give header informations
        return Storage::disk('public')->download($documentFileName, 'Request', $header); //
    }
}

Please leave a comment and let me know what you did in your Laravel App. So, happy Coding!

Leave a Reply

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