DEV Community

Mubashar Ahmad
Mubashar Ahmad

Posted on

How to Create Laravel PDF file and Download?

Laravel PDF: PDF generator for Laravel 5.x | 6.x | 7.x | 8.x | 9.x

Easily generate PDF documents from HTML right inside of Laravel using this PDF wrapper.

Contents

Installation Guide

Require this package in your composer.json or install it by running:

composer require zanysoft/laravel-pdf
Enter fullscreen mode Exit fullscreen mode

To start using Laravel, add the Service Provider and the Facade to your config/app.php:

'providers' => [
    // ...
    ZanySoft\LaravelPDF\PdfServiceProvider::class
]
Enter fullscreen mode Exit fullscreen mode
'aliases' => [
    // ...
    'PDF' => ZanySoft\LaravelPDF\Facades\PDF::class
]
Enter fullscreen mode Exit fullscreen mode

Configuration

The defaults configuration settings are set in config/pdf.php. Copy this file to your own config directory to modify the values. You can publish the config using this command:

php artisan vendor:publish --provider="ZanySoft\LaravelPDF\PdfServiceProvider"
Enter fullscreen mode Exit fullscreen mode

Basic Usage

To use Laravel PDF add something like this to one of your controllers. You can pass data to a view in /resources/views.

use PDF;

function generate_pdf() {
    $data = [
        'foo' => 'bar'
    ];
    $pdf = PDF::::Make();
    $pdf->loadView('pdf.document', $data);
    return $pdf->stream('document.pdf');
}
Enter fullscreen mode Exit fullscreen mode

or

use ZanySoft\LaravelPDF\PDF;

function generate_pdf() {
    $data = [
        'foo' => 'bar'
    ];
    $pdf = new PDF();
    $pdf->loadView('pdf.document', $data);
    return $pdf->stream('document.pdf');
}

Enter fullscreen mode Exit fullscreen mode

If you want to generate from html content:

    $content = "Hello this is first pdf file."
    $pdf->loadHTML($content);
    return $pdf->stream('document.pdf');
Enter fullscreen mode Exit fullscreen mode

If you want to generate from files:

    $file = "file.txt"
    $pdf->loadFile($file);
    return $pdf->stream('document.pdf');
Enter fullscreen mode Exit fullscreen mode

If you want download pdf file:

    return $pdf->embed('document.pdf');
Enter fullscreen mode Exit fullscreen mode

If you want to save pdf to server:

    return $pdf->save('with-complete-path/document.pdf');
Enter fullscreen mode Exit fullscreen mode

If you want add pdf file as attachment to email:

    return $pdf->embed('document.pdf');
Enter fullscreen mode Exit fullscreen mode

Headers and Footers

If you want to have headers and footers that appear on every page, add them to your <body> tag like this:

<htmlpageheader name="page-header">
    Your Header Content
</htmlpageheader>

<htmlpagefooter name="page-footer">
    Your Footer Content
</htmlpagefooter>
Enter fullscreen mode Exit fullscreen mode

Now you just need to define them with the name attribute in your CSS:

@page {
    header: page-header;
    footer: page-footer;
}
Enter fullscreen mode Exit fullscreen mode

Inside of headers and footers {PAGENO} can be used to display the page number.

Included Fonts

By default you can use all the fonts shipped with mPDF.

Custom Fonts

You can use your own fonts in the generated PDFs. The TTF files have to be located in one folder, e.g. /resources/fonts/. Add this to your configuration file (/config/pdf.php):

    return [
        'custom_font_path' => base_path('/resources/fonts/'), // don't forget the trailing slash!
    ];
Enter fullscreen mode Exit fullscreen mode

And then:

    $font_data = array(
        'examplefont' => [
            'R' => 'ExampleFont-Regular.ttf',      // regular font
            'B' => 'ExampleFont-Bold.ttf',         // optional: bold font
            'I' => 'ExampleFont-Italic.ttf',       // optional: italic font
            'BI' => 'ExampleFont-Bold-Italic.ttf', // optional: bold-italic font
        ]
        // ...add as many as you want.
    );

    $pdf->addCustomFont($font_data, true);
    // If your font file is unicode and "OpenType Layout" then set true. Default value is false.
Enter fullscreen mode Exit fullscreen mode

Now you can use the font in CSS:

body {
    font-family: 'examplefont', sans-serif;
}
Enter fullscreen mode Exit fullscreen mode

Set Protection

To set protection, you just call the SetProtection() method and pass an array with permissions, an user password and an owner password.

The passwords are optional.

There are a few permissions: 'copy', 'print', 'modify', 'annot-forms', 'fill-forms', 'extract', 'assemble', 'print-highres'.

use PDF;

function generate_pdf() {
    $data = [
        'foo' => 'bar'
    ];
    $pdf = PDF::Make();
    $pdf->SetProtection(['copy', 'print'], 'user_pass', 'owner_pass')
    $pdf->loadView('pdf.document', $data);

    return $pdf->stream('document.pdf');
}
Enter fullscreen mode Exit fullscreen mode

Find more information to SetProtection() here: https://mpdf.github.io/reference/mpdf-functions/setprotection.html

Documentation

Visit this link for more options and settings: https://mpdf.github.io/

Top comments (0)