Skip to content

AhemdHegazy/laravel-mpdf-with-arabic-support

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Mpdf: Using Mpdf in Laravel 5 for generate Pdfs

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

Important Note

Currently supported mpdf version 8.0 with FPDF version 2 and PHP version >= 7.0

Installation

Require this package in your composer.json

"require": {
	carlos-meneses/laravel-mpdf: "2.1.3"
}

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. We add some configrations to support Arabic.

//....
use PDF;
class ReportController extends Controller {
	public function generate_pdf() 
	{
	    $data = [
		'foo' => 'bar'
	    ];
	    $pdf = PDF::loadView('pdf');
	    $pdf->autoScriptToLang = true;
	    $pdf->autoArabic = true;
	    $pdf->autoLangToFont = true;
	    return $pdf->download('pdf.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'    => '25',
    'default_font'         => 'Arial',
    'margin_left'          => 10,
    'margin_right'         => 10,
    'margin_top'           => 10,
    'margin_bottom'        => 10,
    'margin_header'        => 0,
    'margin_footer'        => 0,

    'orientation'          => 'L',
    'title'                => 'RCAT',
    'author'               => 'RCAT',
    'watermark'            => '',
    'show_watermark'       => false,
    'watermark_font'       => 'sans-serif',
    'display_mode'         => 'fullpage',
    'watermark_text_alpha' => 0.1,
    'custom_font_dir' => base_path('resources/fonts/'), // don't forget the trailing slash!
    'custom_font_data' => [
        'cairo' => [
            'R'  => 'Cairo-Regular.ttf',    // regular font
            'B'  => 'Cairo-Bold.ttf',       // optional: bold font
            'I'  => 'Cairo-Italic.ttf',     // optional: italic font
            'BI' => 'Cairo-BoldItalic.ttf', // optional: bold-italic font,
            'useOTL' => 0xFF,
            'useKashida' => 75,
        ]
        // ...add as many as you want.
    ],
    'auto_language_detection'  => true,
    'temp_dir'               => '',
];

Don't Forget

In resources put a fonts folder,, I use cairo font you can find all fonts that I use here.

https://drive.google.com/file/d/105axtIbTkfYtyeu9R-xN2sNuO5hjR-Be/view?usp=sharing

you can use your own font but don't forget put it in resources/fonts and add it to custom_font_data array

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>

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 {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_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.
	]
];

Now you can use the font in CSS:

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

Full Example

you can find a full example here

https://github.com/AhemdHegazy/laravel-mpdf-example

License

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

About

Generate PDFs in Laravel with Mpdf.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%