Skip to content

📄 Easily generate PDF documents from HTML right inside of Laravel 5

Notifications You must be signed in to change notification settings

hugo8xx/laravel-pdf

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel PDF: mPDF wrapper for Laravel 5

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

Installation

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

composer require niklasravnsborg/laravel-pdf

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

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

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::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:

<?php

return [
	'mode'               => '',
	'format'             => 'A4',
	'defaultFontSize'    => '12',
	'defaultFont'        => 'sans-serif',
	'marginLeft'         => 10,
	'marginRight'        => 10,
	'marginTop'          => 10,
	'marginBottom'       => 10,
	'marginHeader'       => 0,
	'marginFooter'       => 0,
	'orientation'        => 'P',
	'title'              => 'Laravel PDF',
	'author'             => '',
	'watermark'          => '',
	'showWatermark'      => false,
	'watermarkFont'      => 'sans-serif',
	'SetDisplayMode'     => 'fullpage',
	'watermarkTextAlpha' => 0.1
];

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):

<?php

return [
	'custom_font_path' => 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;
}

License

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

About

📄 Easily generate PDF documents from HTML right inside of Laravel 5

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%