HTML To PDF In Laravel Using barryvdh/laravel-snappy

HTML To PDF In Laravel Using barryvdh/laravel-snappy

In this post we will give you information about HTML To PDF In Laravel Using barryvdh/laravel-snappy. Hear we will give you detail about HTML To PDF In Laravel Using barryvdh/laravel-snappyAnd how to use it also give you demo for it if it is necessary.

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

[ADDCODE]

Step : 3 Configure Package

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


'providers' => [
	....
	BarryvdhSnappyServiceProvider::class,
],
'aliases' => [
	....
	'PDF' => BarryvdhSnappyFacadesSnappyPdf::class,
	'SnappyImage' => BarryvdhSnappyFacadesSnappyImage::class,
],

Then after publish vendor folder run by this command


php artisan vendor:publish --provider="BarryvdhSnappyServiceProvider"

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', '[email protected]')->name('generate-pdf');

Step : 4 Create controller


namespace AppHttpControllers;

use AppHttpRequests;
use IlluminateHttpRequest;
use DB;
use PDF;

class PdfGenerateController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return IlluminateHttpResponse
     */
    public function pdfview(Request $request)
    {
        $users = DB::table("users")->get();
        view()->share('users',$users);

        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>
<html>
<head>
	<title>User list - PDF</title>
	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div >
	<a href="{{ route('generate-pdf',['download'=>'pdf']) }}">Download PDF</a>
	<table >
		<thead>
			<th>Name</th>
			<th>Email</th>
		</thead>
		<tbody>
			@foreach ($users as $key => $value)
			<tr>
				<td>{{ $value->name }}</td>
				<td>{{ $value->email }}</td>
			</tr>
			@endforeach
		</tbody>
	</table>
</div>
</body>
</html>

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:


http://localhost:8000

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

Hope this code and post will helped you for implement HTML To PDF In Laravel Using barryvdh/laravel-snappy. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US