Need help with laravel-pdf?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

565 Stars 145 Forks 54 Commits 21 Opened issues


:page_facing_up: Easily generate PDF documents from HTML inside of Laravel 5

Services available


Need anything else?

Contributors list

Laravel PDF: mPDF wrapper for Laravel 5

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


Require this package in your

or install it by running:
composer require niklasravnsborg/laravel-pdf

Note: This package supports auto-discovery features of Laravel 5.5+, You only need to manually add the service provider and alias if working on Laravel version lower then 5.5

To start using Laravel, add the Service Provider and the Facade to your

'providers' => [
    // ...
'aliases' => [
    // ...
    'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class

Now, you should publish package's config file to your config directory by using following command:

php artisan vendor:publish

Basic Usage

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

use PDF;

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

Other methods

It is also possible to use the following methods on the


: Outputs the PDF as a string.
: Save the PDF to a file
: Make the PDF downloadable by the user.
: Return a response with the PDF to show in the browser.


If you have published config file, you can change the default settings in

return [
    'format'           => 'A4', // See
    'author'           => 'John Doe',
    'subject'          => 'This Document will explain the whole universe.',
    'keywords'         => 'PDF, Laravel, Package, Peace', // Separate values with comma
    'creator'          => 'Laravel Pdf',
    'display_mode'     => 'fullpage'

To override this configuration on a per-file basis use the fourth parameter of the initializing call like this:

PDF::loadView('pdf', $data, [], [
  'format' => 'A5-L'

You can use a callback with the key 'instanceConfigurator' to access mpdf functions: ```php $config = ['instanceConfigurator' => function($mpdf) { $mpdf->SetImportUse(); $mpdf->SetDocTemplate(/path/example.pdf, true); }]

PDF::loadView('pdf', $data, [], $config)->save($pdfFilePath); ```

Headers and Footers

If you want to have headers and footers that appear on every page, add them to your

 tag like this:
    Your Header Content

    Your Footer Content

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

@page {
    header: page-header;
    footer: page-footer;

Inside of headers and footers

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.

. Add this to your configuration file (
return [
    // ...
    'font_path' => base_path('resources/fonts/'),
    'font_data' => [
        '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
            //'useOTL' => 0xFF,    // required for complicated langs like Persian, Arabic and Chinese
            //'useKashida' => 75,  // required for complicated langs like Persian, Arabic and Chinese
        // ...add as many as you want.
    // ...

Note: If you are using

for producing PDF documents in a complicated language (like Persian, Arabic or Chinese) you should have
indexes in your custom font definition array. If you do not use these indexes, your characters will be shown dispatched and incorrectly in the produced PDF.

Now you can use the font in CSS:

body {
    font-family: 'examplefont', sans-serif;

Set Protection

To set protection, you just call the

method and pass an array with permissions, an user password and an owner password.

The passwords are optional.

There are a fews permissions:

use PDF;

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

Find more information to



To use the testing suite, you need some extensions and binaries for your local PHP. On macOS, you can install them like this:

brew install imagemagick ghostscript
pecl install imagick


Laravel PDF is open-sourced software licensed under the MIT license

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.