How To Convert HTML To PDF In Laravel 5 Using Dompdf Library

By : PHPErrorCode October 13, 2017 396 Views laravel

In this post, we will tell you how to create pdf file from HTML view in Laravel web aplication by using barryvdh/laravel-dompdf package. The barrevdh/laravel-dompdf package is a open sorce and you can easily set the beautiful layout for pdf file. so this is a packahe very easy way to generate and download PDF file in laravel 5.5 web application.

Generally, a PDF is better than an HTML stylesheet, because it is useful for saving large data and sending email in your laravel wen application.

Dompdf Library doesn't require anything else as its codebase. only install this package in your website and Follow the steps below.

Steo 1 Install Dompdf Package

To use Dompdf Library in Laravel, we are installed to the barryvdh/laravel-dompdf Library in our web application. You can install this Library to your web application adding the following command in your terminal.

composer require barryvdh/laravel-dompdf

Step 2: Configuration

After install, Go to this path config/app.php and set following value for providers array and aliases array

'providers' => [
    //------------------

    Barryvdh\DomPDF\ServiceProvider::class,

    //------------------
],
'aliases' => [
    //------------------

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

    //-----------------
],

After configure app.php file then Go to bootstrap/app.php and open app.php file than add following below line.

$app->singleton(\Barryvdh\DomPDF\ServiceProvider::class);

After add above line then You can publish the config dompdf.php file so using command below:

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

Than Go to this link config/dompdf.php and defaults configuration settings are set

Step 3: Create route

Go to routes/web.php file than open web.php file and add below route

Route::get('pdf',array('as'=>'pdfDownload','uses'=>'BlogController@PDF'));

Step 4: Create controller

Go To app/Http/Controller/ create BlogController.php file and add below code

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;
use PDF;

class BlogController extends Controller
{

	public function PDF(Request $request)
    {
    	$data = DB::table("blogs")->get();
    	view()->share('blog',$data);

        if($request->has('download'))
        {
        	// Set more option
        	PDF::setOptions(['defaultFont' => 'sans-serif','dpi' => 100]);

        	// pass view file
            $pdf = PDF::loadView('pdfshow');

            // download pdf file
            return $pdf->download('phperrorcode.pdf');
        }

        return view('pdfshow');
    }
}

Step : 5 Create view file

Now we are Go to this path resources/views/ than create a pdfshow.blade.php file and here we are generate pdf file layout, so like below layout

<html>
<head>
	<title>HTML to PDF Using Dompdf</title>
	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
	<a href="{{ route('pdfDownload',['download'=>'pdf']) }}">Download PDF</a>
	<table class="table table-bordered">
		<thead>
			<th>ID</th>
			<th>Title</th>
			<th>Description</th>
		</thead>
		<tbody>
			@foreach ($blog as $key => $value)
			<tr>
				<td>{{ $key + 1 }}</td>
				<td>{{ $value->title }}</td>
				<td>{{ $value->description }}</td>
			</tr>
			@endforeach
		</tbody>
	</table>
</div>
</body>
</html>

If you get a bug like the image below img

html to pdf In Laravel 5.5 using Dompdf error Solution
Solution : go to /storage/ derectory and if not faund fonts derectory, so please create one empty fonts folder ane set the permision-777.

Run :

now you can open terminal, after add follow below command

php artisan serve

Now you can go to url below.

http://localhost:8000/search

I hope you have found this post useful,

thanks....