Blog Detail

1

Jul
Generate PDF in Laravel with DOMPDF Wrapper cover image

Generate PDF in Laravel with DOMPDF Wrapper

In the age of open-source software development platforms like GitHub and Bitbucket, Developers adore using already built-in open-source solutions more than producing their own. For a custom Laravel based CRM project, you always required the dynamic PDF generation feature for printing the information on pdf. After searching for some time, I tripped upon a very handy DOMPDF Wrapper for Laravel at GitHub.

DOMPDF is a wrapper for Laravel, and It offers robust performance for HTML to PDF conversion in Laravel applications spontaneously. You can generate a pdf file from a view utilizing DomPDF. To export into PDF, We require the view file. We will write the HTML code in the view file and load data dynamically from the database as per the demand. Then, we will export this view as a PDF file.

Installation

Run this command to install DomPDF in Laravel. It will download the package and the dompdf + fontlib libraries.

composer require barryvdh/laravel-dompdf

After updating the composer, append the ServiceProvider to the providers array in config/app.php

Barryvdh\DomPDF\ServiceProvider::class,

You can freely use the facade for more compressed code. Append this to your facades:

'PDF' => Barryvdh\DomPDF\Facade::class,

Usage

You can generate a new DOMPDF instance and load an HTML string, file, or view name. You can store it in a file or download it.

$pdf = App::make('dompdf.wrapper');

$pdf->loadHTML('<h1>Test</h1>');

return $pdf->stream();

Or utilize the facade:

$pdf = PDF::loadView('pdf.invoice', $data);

return $pdf->download('invoice.pdf');

You can chain the methods:

return PDF::loadFile(public_path().'/myfile.html')->save('/path-to/my_stored_file.pdf')->stream('mydownload.pdf');

You can modify the orientation and paper size, moreover, you can hide or show the errors.

PDF::loadHTML($html)->setPaper('a4', 'landscape')->setWarnings(false)->save('myfile.pdf');

Configuration

The defaults configuration settings are placed in config/dompdf.php. Copy this file to your own config directory to alter the values. Execute this command to publish the assets from the vendor.

php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

Later, we can discover a PDF class and use its API for further magnifying the generated PDF file.

For more detailed documentation, you can click the link below.

barryvdh / laravel-dompdf

Published at : 01-07-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

web application development client
web application development client
web application development client
web application development client
web application development client

Launch project