Recently, I had to develop a functionality in a laravel web application that allows users of the application to generate a pdf. I was clueless on how to go about it as it was something that I had never done before. My first port of call was to seek help on google.The search phrase that I used on google was "how to generate a pdf using laravel". As just any other search on google I got a lot of search results. A few of the search results that piqued my interest were :

I had a chance to use these pdf generator solutions and they all have their pros and cons. For me, the one that stood apart was laravel-snappy. It works like a charm once it has been configured in your local environment or remote server. Laravel-snappy is a wrapper for wkhtmltopdf used to convert html to pdf. It is used for server side rendering.
Wkhtmltopdf is an open source (LGPLv3) command line tool used to render HTML into PDF and using the Qt WebKit rendering engine. What I particularly like about this package is that it runs Javascript and it supports bootstrap. It was a bit of a mission to make it work on my Mac but finally I cracked it. In the next section I'm going to show you how I was able to generate a pdf on a laravel application using wkhtmltopdf.

Firstly we are going to download a suitable version of wkhtmltopdf from their website and install it. The installation method is either

NB: When installing wkhtmltopdf please ensure that you install dependencies such as libXrender if they are not installed already.

Once you are done with the installation of wkhtmltopdf you may conduct a test to check if its properly installed. You do that by opening your console terminal, type in "wkhtmltopdf -V" or "wkhtmltopdf --version" and press enter.

After that we can install the Laravel-snappy package. We do that by requiring the package the composer.json file and then executing a composer update.

composer require barryvdh/laravel-snappy
composer update


After the steps above  add the ServiceProvider to the providers array in config/app.php file.


For shorter code you can add the following to your facades inside of the config/app.php file.

'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,

After you are done with all the steps above you then move on to publish the config/app.php class.

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

Snappy config file

The major change in this file will be the path to the binaries. Since we installed wkhtmltopdf globally on our Mac we can do this by changing the path to:

'binary' => 'wkhtmltopdf',

If you have followed all the steps mentioned above you should be able to use the package to generate a pdf inside of a laravel application. Inside of a controller function you should be able to implement something like this:

namespace App\Http\Controllers;
use App\Inventory;

class MerchandiseController extends Controller
public function printInventory(){
$inventory = Inventory::get()->toArray();
$inventory_pdf = PDF::loadView('inventory_template', $inventory)
return $inventory_pdf->download('inventory.pdf');
You then have to create inventory_template, a blade template with the html to be rendered to pdf in the resources/view directory or any other path you so choose.