HTML To PDF In Laravel Using barryvdh/laravel-snappy

Today, laravel code share with you how to convert HTML to PDF in laravel ussing barryvdh/laravel-snappy package. recently we are working on one laravel application and we are use barryvdh/laravel-snappy package for generate HTML to PDF but we have face some problem when we are integrate this package in our laravel application. it is produce some errors. after lots of google search and change some configuration it is finaly generate PDF. so we are write this tutorials for helping purpose.

Which type problem facing in our developing mode i also share with you ahead. if you are using this package for generate HTML to PDF? simply follow this step.

Step : 1 Install Require Package's Dependencies

Before install barryvdh/laravel-snappy package. we have must be required some dependencies for it. we have required two dependencies, run following command for install dependencies

Note : If you have 32-bit system then install this dependencies

$ composer require h4cc/wkhtmltopdf-i386 0.12.x
$ composer require h4cc/wkhtmltoimage-i386 0.12.x

Note : If you have 64-bit system then install this dependencies

$ composer require h4cc/wkhtmltopdf-amd64 0.12.x
$ composer require h4cc/wkhtmltoimage-amd64 0.12.x

Step : 2 Install Require Package

After install both are dependencies then let's move on install our required package for generate HTML to PDF. just run following command for install.

composer require barryvdh/laravel-snappy

Step : 3 Configure Package

After install package successfully then must be configure it's service providers and aliaces in config/app.php file.

'providers' => [
'aliases' => [
	'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
	'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,

Then after publish vendor folder run by this command

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

Step : 4 Some Required Changes

After install package, configure and publish it vendor then we have required some most important changes. if you don't change it is and forgot then you got following error message when you try to generate HTML to PDF. so, it must be required for all.

The exit status code '127' says something went wrong:
stderr: "sh: 1: /usr/local/bin/wkhtmltopdf: not found

You all are remember we have also install two dependancy for it. ones you check in your vendor folder then you found this path vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 and another is vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 so, we have required copy wkhtmltoimage-amd64 and wkhtmltopdf-amd64 folder in this path /usr/local/bin/ simply run following command for it.

Note : After copy both folder then give 777 permission

cp vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/
cp vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin/

Now open your config/snappy.php file and changes in it bith of 'binary' array key following way

return array(
    'pdf' => array(
        'enabled' => true,
        'binary'  => '/usr/local/bin/wkhtmltopdf-amd64',
        'timeout' => false,
        'options' => array(),
        'env'     => array(),
    'image' => array(
        'enabled' => true,
        'binary'  => '/usr/local/bin/wkhtmltoimage-amd64',
        'timeout' => false,
        'options' => array(),
        'env'     => array(),

Step : 5 Create Route

After done configuration then create one route in your routes/web.php file

Route::get('generate-pdf', 'PdfGenerateController@pdfview')->name('generate-pdf');

Step : 4 Create controller

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use PDF;

class PdfGenerateController extends Controller
     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function pdfview(Request $request)
        $users = DB::table("users")->get();

        if($request->has('download')) {
        	// pass view file
            $pdf = PDF::loadView('pdfview');
            // download pdf
            return $pdf->download('userlist.pdf');
        return view('pdfview');

Step : 5 Create view

Now we are create one simple blade file in resources/views/pdfview.blade.php file and here we are make very simple html layout for generate pdf file.

<!DOCTYPE html>
	<title>User list - PDF</title>
	<link rel="stylesheet" type="text/css" href="">
<div class="container">
	<a href="{{ route('generate-pdf',['download'=>'pdf']) }}">Download PDF</a>
	<table class="table table-bordered">
			@foreach ($users as $key => $value)
				<td>{{ $value->name }}</td>
				<td>{{ $value->email }}</td>

Now we are ready to run our example so run bellow command ro quick run:

php artisan serve

Now you can open bellow URL on your browser:


If you face any problem then please write a comment or give some suggestions for improvement. Thanks...

 Prev post

Next post