# Laravel Packages

# DataTable

datatables.yajrabox.com (opens new window)

# Install Laravel Datatables Package

# Install using composer.

composer require yajra/laravel-datatables-oracle
1

# Add Datatables Service Provider and Facade on config/app.php.

Yajra\Datatables\DatatablesServiceProvider::class,
1
'Datatables' => Yajra\Datatables\Facades\Datatables::class,
1

config/app.php

# Lastly, publish the configuration file.

php artisan vendor:publish
1

# Install DataTables Assets

# Css
//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css
1
# jQuery
//code.jquery.com/jquery-1.10.2.min.js
1
# DataTables
//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js
1

# Spatie Permission

saptie.be (opens new window)

# Install Laravel Spatie Permission Package

# Install using composer.

 composer require spatie/laravel-permission
1

# You may manually add the service provider in your config/app.php file:

'providers' => [
    // ...
    Spatie\Permission\PermissionServiceProvider::class,
];
1
2
3
4

config/app.php

# You should publish the migration and the config/permission.php config file with:

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
1

# Clear your config cache.

This package requires access to the permission config. Generally it's bad practice to do config-caching in a development environment. If you've been caching configurations locally, clear your config cache with either of these commands:

php artisan optimize:clear
# or
php artisan config:clear
1
2
3

# Run the migrations

After the config and migration have been published and configured, you can create the tables for this package by running:

php artisan migrate
1

# MPDF

Github (opens new window)

# Install Laravel MPDF Package

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

# Installation.

Require this package in your composer.json

"require": {
  carlos-meneses/laravel-mpdf: "2.1.8"
}
1
2
3

or install it by running:

composer require carlos-meneses/laravel-mpdf
1

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

'providers' => [
  // ...
  Meneses\LaravelMpdf\LaravelMpdfServiceProvider::class
]
1
2
3
4
'aliases' => [
  // ...
  'PDF' => Meneses\LaravelMpdf\Facades\LaravelMpdf::class
]
1
2
3
4

config/app.php

# Basic Usage

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

//....
use PDF;
class ReportController extends Controller {
  public function generate_pdf()
  {
    $data = [
      'foo' => 'bar'
    ];
    $pdf = PDF::loadView('pdf.document', $data);
    return $pdf->stream('document.pdf');
  }
}
1
2
3
4
5
6
7
8
9
10
11
12

# Config

You can use a custom file to overwrite the default configuration. Just execute php artisan vendor:publish --tag=mpdf-config or create config/pdf.php and add this:

return [
  'mode'                       => '',
  'format'                     => 'A4',
  'default_font_size'          => '12',
  'default_font'               => 'sans-serif',
  'margin_left'                => 10,
  'margin_right'               => 10,
  'margin_top'                 => 10,
  'margin_bottom'              => 10,
  'margin_header'              => 0,
  'margin_footer'              => 0,
  'orientation'                => 'P',
  'title'                      => 'Laravel mPDF',
  'author'                     => '',
  'watermark'                  => '',
  'show_watermark'             => false,
  'show_watermark_image'       => false,
  'watermark_font'             => 'sans-serif',
  'display_mode'               => 'fullpage',
  'watermark_text_alpha'       => 0.1,
  'watermark_image_path'       => '',
  'watermark_image_alpha'      => 0.2,
  'watermark_image_size'       => 'D',
  'watermark_image_position'   => 'P',
  'custom_font_dir'            => '',
  'custom_font_data'           => [],
  'auto_language_detection'    => false,
  'temp_dir'                   => rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR),
  'pdfa'                       => false,
  'pdfaauto'                   => false,
  'use_active_forms'           => false,
];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

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

PDF::loadView('pdf', $data, [], [
  'title'      => 'Another Title',
  'margin_top' => 0
])->save($pdfFilePath);
1
2
3
4

# 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>
1
2
3
4
5
6
7

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

@page {
  header: page-header;
  footer: page-footer;
}
1
2
3
4

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

# 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_dir'  => base_path('resources/fonts/'), // don't forget the trailing slash!
  'custom_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
    ]
  	// ...add as many as you want.
  ]
];
1
2
3
4
5
6
7
8
9
10
11
12

Now you can use the font in CSS:

body {
  font-family: 'examplefont', sans-serif;
}
1
2
3

# Get instance your Mpdf

You can access all mpdf methods through the mpdf instance with getMpdf().

use PDF;

$pdf = PDF::loadView('pdf.document', $data);
$pdf->getMpdf()->AddPage(...);
1
2
3
4
Last Updated: 6/2/2022, 9:53:32 PM