Skip to content

SGLMS/pdf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SglmsPdf (Laravel Mpdf Wrapper)

Simple Laravel (^12.0) wrapper for Mpdf, using Laravel's view components.

Installation

composer require sglms/pdf

Laravel's auto-discovery features will register the service provider and facade.

Usage

For the impatient:

use Sglms\Pdf;

$pdf = Pdf::view('pdf.filename')->output('filename.pdf');

View Parameters

use Sglms\Pdf;

$pdf = Pdf::view(
	'pdf.filename',
	['param' => 'value']	/* [Optional */
);
$pdf->output('filename.pdf');

/* Or, ... */

$pdf->save('filename');

/* Or, store it 'the Laravel way' */

$pdf->storeAs('filename', 'disk');

Configuration and Header/Footer

$pdf = Pdf::init(
	config: ['format' => 'letter'],
	header: 'pdf.header',
	footer: 'pdf.footer'
	stylesheet: 'path/to/stylesheet.css'
);

Add your logo (available as 'var:logo' in your views):

$pdf->logo('path/to/logo.svg');

Remember to add your logo before header/footer setup, if you plan to use it there.

You can override the setup if you need parameters:

$pdf->header('pdf.header', ['param' => 'value']);
$pdf->footer('pdf.footer', ['param' => 'value']);

Include your header before adding views to the pdf. This is a limitation of mPDF in that it calls AddPage() when you include a view (or any other html), and if the header is not set, mpdf will render the header blank.

Concatenate multiple views:

$pdf = Pdf::init();
$pdf->view('pdf.one')->view('pdf.two')->output('filename.pdf');

Sign your document:

$pdf->sign('pdf.signature', ['name' => 'John Doe']);

Or, ...

$pdf->sign(
	signature: 'pdf.signature',
	data: ['name' => 'John Doe'],
	x: 50,		/* [mm] */
	y: 100,		/* [mm] */
	width: 100	/* [mm] */
);

You can also load and sign an existing PDF file:

$pdf->signFile('pdf.signature, 'path/to/pdf');

If you need the base mPDF to work on it further:

$pdf->get();

Use it in your controller

use Sglms\Pdf;

class CustomController extendes Controller
{
	// ...
	public function render() {
        $pdf = Pdf::view('pdf.filename')->sign('pdf.signature');
        return response($pdf->output(), 200)
        	->header('Content-Type', 'application/pdf')
         	->header('Content-Disposition', 'inline; filename="document.pdf"');
    }
    //...
}

Limitations

As mentioned, very simple (but efficient) wrapper, that works with Laravel 12 (php >=8.3; mpdf >= 8.x).

It's better suited for single page document generation.

Rationale

This package is part of a larger project in which we needed to generate a large number of one-page documents and (digitally) sign them.

License

Sglms/Pdf is licensed under the The MIT License.

About

Simple (but effective) Laravel (12) wrapper for mPDF.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published