convert html to pdf laravel 5.4

convert html to pdf laravel 5.4

In this post we will give you information about convert html to pdf laravel 5.4. Hear we will give you detail about convert html to pdf laravel 5.4And how to use it also give you demo for it if it is necessary.

Today online share with you how to convert html to pdf in laravel and how to download pdf file which generate fron html layout. in many big laravel project like e-commerce, erp and industrial relations related project you required this type functionality in laravel.


In this tutorials we are using on of the best package barryvdh/laravel-dompdf it provide very easy way to generate html to pdf in laravel.


We are in this tutorials create one static html pdf with table. but you are create one also dynamic data. like you can get data from your database and also make some action on it. we are also explain in this tutorials how to padd data in pdf view it is very simple and stupid way.


We are explain with all step by step.


Step : 1 Install package


First we need to install package in our laravel application using following command




composer require barryvdh/laravel-dompdf



Step : 2 Configure package


After install pdf package for laravel then we must be configure it. just following littel thing. oprn your config/app.php file and set following value for providers array and aliases array




'providers' => [
	....
	BarryvdhDomPDFServiceProvider::class,
],
'aliases' => [
	....
	'PDF' => BarryvdhDomPDFFacade::class,
],



After configure app.php file then open your bootstrap/app.php file and add following line like that.


Please make sure add following line above the return $app;




$app->singleton(BarryvdhDomPDFServiceProvider::class);



After adding above line then run following command in your terminal/cmd




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



The defaults configuration settings are set in config/dompdf.php. Copy this file to your own config directory to modify the values. You can publish the config using this command:


Step : 3 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 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')){
        	// Set extra option
        	PDF::setOptions(['dpi' => 150, 'defaultFont' => 'sans-serif']);
        	// pass view file
            $pdf = PDF::loadView('pdfview');
            // download pdf
            return $pdf->download('pdfview.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>



Note : if you show following error when generate pdf




ErrorException in AdobeFontMetrics.php line 45:
fopen(/var/www/Laravel/LaraDemo/storage/fonts//c47afe5539ba1b2094563d54dce2def7.ufm): failed to open stream: No such file or directory



Solution : please create one empty fonts folder in storage derectory and give it permision 777.


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/generate-pdf


I hope it can help you…

Hope this and post will helped you for implement convert html to pdf laravel 5.4. 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 Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

5  +  4  =  

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