Reputation: 53
I am working on a product management system and there is a module called purchase.
I have wrote the store()
method the controller and the model part too.
Now I want to generate the invoice when I click on submit button. I used PDF generator plugin as barryvdh/laravel-dompdf
. I'm new to PHP & Laravel and just wants to know instructions to do the PDF generation process.
Upvotes: 0
Views: 3804
Reputation: 11
Step 1 :
composer require barryvdh/laravel-dompdf
Step 2. Update config/app.php
Open config/app.php
file. Add the following line to ‘providers’ array:
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
]
Step 3. Add routes Now open your web.php file and add following route.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PDFController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('generate-pdf','PDFController@generatePDF');
Step 4. Add Link In Blade File
<a href="route('generate-pdf')">Generate PDF</a>
Step 5. Create Controller Add your controller below code.
<?PHP
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class PDFController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function generatePDF()
{
$data = [
'title' => 'First PDF ',
'heading' => 'Hello Bro',
'content' => 'Data content'
];
$pdf = PDF::loadView('generate_pdf', $data);
return $pdf->download('Laravel.pdf');
}
}
Step 6. Create Blade File
Let’s create generate_pdf.blade.php
for layout of pdf file and put
following code:
<!DOCTYPE html>
<html>
<head>
<title>Generate Pdf</title>
</head>
<body>
<h1>{{ $heading}}</h1>
<div>
<p>{{$content}}</p>
</div>
</body>
</html>
Upvotes: 0
Reputation: 501
1- Install the DOMPDF according to this Github Link
2- Form page
<form id="FOO" method="POST" action="/pdf">
<input type="text" name="object" >
<button type="submit">Generate</button>
</form>
3- Route :Route::post('pdf' , 'EventsController@PDFGenerator');
Please notice,here,that you will either need to include a hidden input,with csrf token,or simply exclude this route in the VerifyCSRFToken.php
middleware,otherwise it will not let you post(if you haven't worked with Auth yet,don't mind this notice)
4- Controller page now
public function PDFGenerator (Request $request){
$html = <<<ENDHTML
<!DOCTYPE html>
<html>
<head>
<title>Foo</title>
<meta charset="utf-8">
</head>
<body style="margin: 30px;">
<center style="text-decoration: underline;">{$request->object}</center>
</body>
</html>
ENDHTML;
$dompdf = App::make('dompdf.wrapper');
$dompdf->loadHTML($html);
$dompdf->stream("hello.pdf");
return $dompdf->stream();
}
This is the Heredoc documentation in case you needed more information about how to use it.
Don't forget to include the library in your controller,waiting for your feedback.
Upvotes: 0
Reputation: 16359
You would follow the install steps for Laravel 5:
Barryvdh\DomPDF\ServiceProvider::class,
to your config/app.php
file as a ServiceProvider'PDF' => Barryvdh\DomPDF\Facade::class,
to your facades in your config/app.php
fileAnd then in your store method, after you save all the information, you could generate a PDF like so:
$pdf = \PDF::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');
This would load the invoice
view stored in resources/views/pdf
with the values stored in $data
, and then return a downloadable file called invoice.pdf
.
This all comes directly from the Using section of the DomPDF docs.
Upvotes: 1