how to generate a pdf file in laravel,pdf in laravel 5.7, pdf in laravel 5.6, pdf in laravel 5.4, pdf in laravel 5, generate pdf in laravel, download pdf in laravel, upload pdf in laravel, export pdf in laravel, view pdf in laravel, laravel in action pdf, download as pdf in laravel, generate a pdf in laravel, save as pdf in laravel, generate and download pdf in laravel, add watermark to pdf in laravel, html to pdf in laravel, export to pdf in laravel, image to pdf in laravel, export to pdf in laravel 5.5, html to pdf in laravel 5.4, docx to pdf in laravel, open pdf in browser laravel, pdf builder laravel, pdf laravel create, pdf creator laravel, pdf com laravel, create pdf in laravel 5.6, create pdf in laravel 5.5, create pdf in laravel 5.7, create pdf in laravel 5.2, pdf laravel documentation, pdf laravel download, pdf laravel design patterns and best practices, store pdf in database laravel, laravel dompdf, display pdf in laravel, download pdf in laravel 5.4, pdf in edit laravel, generate pdf in laravel example, pdf laravel example, pdf en laravel, pdf export laravel, pdf en laravel 5.4, pdf en laravel 5.6, export data to pdf in laravel, pdf laravel file, pdf for laravel, pdf for laravel 5.4, pdf for laravel 5.6, pdf file validation in laravel, pdf file upload in laravel, pdf file download in laravel, fpdf in laravel, fpdf in laravel 5.5, fpdf in laravel 5.4, use fpdf in laravel, use fpdf in laravel 5, fpdf class in laravel, pdf generator laravel, pdf generator laravel 5, pdf generator laravel 5.6, pdf generator laravel 5.4, pdf generator laravel 5.5, generate pdf in laravel 5.7, get pdf in laravel, pdf html laravel, laravel tutorial pdf in hindi, html to pdf in laravel 5.7, how to generate pdf in laravel, how to upload pdf in laravel, how to download pdf in laravel, convert html to pdf in laravel, how to export pdf in laravel, how to print pdf in laravel, pdf invoice laravel, pdf install in laravel, export in pdf in laravel, convert image to pdf in laravel, pdf laravel indonesia, pdf laravel js, pdf laravel khoa pham, pdf laravel loadview, pdf library laravel, pdf landscape laravel, load pdf in laravel, pdf merge laravel, pdf maker laravel, pdf mime laravel, make pdf in laravel, send pdf attachment in mail laravel, mpdf in laravel, mpdf in laravel 5.4, mpdf in laravel 5, use mpdf in laravel, use mpdf in laravel 5, install mpdf in laravel, laravel open pdf in new tab, pdf no laravel, pdf on laravel, open pdf in laravel, pdf laravel package, pdf laravel php, generate pdf in php laravel, pdf parser laravel, pdf preview laravel, pdf print laravel, pdf laravel render, pdf reader laravel, pdf report laravel, pdf response laravel, pdf laravel size, save pdf in storage laravel, pdf stream laravel, pdf snappy laravel, pdf save laravel, pdf storage laravel, pdf set laravel, store pdf in laravel, pdf laravel tutorial, pdf template laravel, open pdf in new tab laravel, pdf to image in laravel, pdf laravel up and running, pdf upload laravel, pdf use laravel, pdf viewer laravel, pdf validation laravel, pdf view laravel, laravel show pdf in view, download view as pdf in laravel, pdf with laravel, pdf watermark laravel, convert word to pdf in laravel, pdf zip laravel, pdf laravel 4.2, pdf laravel 4, create pdf in laravel 5.4, generate pdf in laravel 5.2, pdf package laravel 5, upload pdf file in laravel 5.4, generate pdf in laravel 5, laravel in 60 minuten pdf

How to generate a pdf file in laravel

In this article, we are going to use laravel-dompdf package to generate pdf file from laravel blade template. In this example, we are going to fetch users from the database and show them on pdf in a table form. You can also add your own CSS on Laravel view file for pdf.

Follow the guidelines step by step describe below:-

Install and Config DomPdf in your application


composer require barryvdh/laravel-dompdf

Now add the service provider to your config/app.php file


'providers' => [

Barryvdh\DomPDF\ServiceProvider::class,

],

'aliases' => [

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

],

Make A Route

Create a route for pdf view in your app/Http/web.php


Route::get('generatepdf','GeneratePdfController@generatepdf'));

Create A Controller To Generate and Save Pdf File

Also, create a folder where you want to save your PDF file. In this example we have created a generatepdf folder in our public directory.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use PDF;
use Auth;
use Redirect;
use Storage;

class GeneratePdfController extends Controller
{

public function generatepdf()
{

$users = User::get();

$usersdata = ['users'=>$users];

$pdf = PDF::loadView('generatepdf', array('usersdata'=>$usersdata));

$pdf->setOptions(['isPhpEnabled' => true,'isRemoteEnabled' => true]);

$filename = "generatepdf.pdf";

// Save file to the directory

$pdf->save('generatepdf/'.$filename);

//Download Pdf

return $pdf->download('generatepdf.pdf');

// Or return to view pdf

//return view('pdfview');

}

}

Create View File With User Registration Form

Remember you need to add all CSS internal or inline


<style>
table {  font-family: arial, sans-serif;  border-collapse: collapse;  width: 100%;}

td, th {  border: 1px solid #dddddd;  text-align: left;  padding: 8px;}

tr:nth-child(even) {  background-color: #dddddd;}</style>
<h2>Users List</h2>
@php

$user_detail = $usersdata['users'];

@endphp

<table>
<tbody>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
</tr>
@foreach($user_detail as $user)

<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
</tr>
@endforeach
</tbody>
</table>

Now you can run your code and your pdf will look like:-

Post Created 38

Leave a Reply

Related Posts

Begin typing your search above and press enter to search. Press ESC to cancel.

Back To Top