16. Mpdf in Laravel for generate Pdfs - mzm-dev/laravel-latihan GitHub Wiki
Laravel Mpdf: Using Mpdf in Laravel for generate Pdfs
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.4"
}
or install it by running:
composer require carlos-meneses/laravel-mpdf
To start using Laravel, add the Service Provider and the Facade to your config/app.php:
'providers' => [
// ...
Meneses\LaravelMpdf\LaravelMpdfServiceProvider::class
]
'aliases' => [
// ...
'PDF' => Meneses\LaravelMpdf\Facades\LaravelMpdf::class
]
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');
}
}
Config
You can use a custom file to overwrite the default configuration. Just 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,
'watermark_font' => 'sans-serif',
'display_mode' => 'fullpage',
'watermark_text_alpha' => 0.1,
'custom_font_dir' => '',
'custom_font_data' => [],
'auto_language_detection' => false,
'temp_dir' => rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR),
'pdfa' => false,
'pdfaauto' => false,
];
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);