How To Create/Save/Download PDF From Blade Template In PHP Laravel

How To Create/Save/Download PDF From Blade Template In PHP Laravel

https://ift.tt/3b8hkLL

Hola! you amazing people, I am back with a new article. In this article I will explain you how to create a PDF from a blade template with niklasravnsborg/laravel-pdf package. This package is a wrapper around mPDF.


Prerequisites

You have a composer installed in your computer. If not then you should definitely do it.

Have a fresh copy of Laravel project or a working project. If you want to create a new Laravel project with composer then use the following command.

composer create-project --prefer-dist laravel/laravel blog 

Step 1 – Install Laravel PDF Package With Composer

Install the LaravelPdf package with the following command

composer require niklasravnsborg/laravel-pdf 

This package is having auto discovery feature after Laravel 5.5+. If your still using the Laravel version below 5.5 then carry on with the following

Paste the following code in config/app.php

'providers' => [ // ... niklasravnsborg\LaravelPdf\PdfServiceProvider::class ] 

'aliases' => [ // ... 'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class ] 

After adding the above code run the following command

php artisan vendor:publish 

Step 2 – Create PDF Invoice Sample Blade Template

Following is the sample image of how it looks. Feel free to use the code and implement it in your project.

NOTE: Use the link to copy the code from GITHUB GIST.

PDF Invoice HTML Template Sample

PDF Invoice HTML Template Sample

Use the above GITHUB GIST code for quickly creating the template for PDF inside view/invoices/pdf-invoice.blade.php


Step 3 – Creating Routes

The following in the route, I have secured it within the user_auth middleware. Its not a mandatory but I have shown it so that if anyone who want to implement it securely they can do it.

/** User Related URL's */ Route::middleware(['user_auth'])->group(function () { /** Other URL's */ Route::get('/invoices/{project}/pdf-invoice', 'InvoicesController@savePdfInvoice'); }); 

The above Route URL points to InvoicesController Class and savePdfInvoice method & accepts {project} as parameter for route model binding.


Step 4 – Controller Code To Save PDF To Public Path

I am saving the PDF to my public/uploads/invoices/ folder. Following is the code to implement the PDF in your controller to Save PDF.

Overview Example (Check below for full implementation example)

Using $pdf->save('path_to_save') to save the file

use PDF; class InvoicesController { public static function savePdfInvoice(Project $project) { $pdf = PDF::loadView('invoices.pdf-invoice', [ /** Data required for view */ ]); $invoiceName = 'Name of the invoice to store'; /** Here you can use the path you want to save */ $pdf->save(public_path('uploads/invoices/'. $invoiceName)); } } 

Full Implementation Example

Using $pdf->save('path_to_save') to save the file

public static function savePdfInvoice(Project $project) { $priceDetails = ProjectsController::getProjectAmountDetails($project); $amount = $priceDetails['amount']; $gstAmount = $priceDetails['gstAmount']; $totalAmount = $priceDetails['totalAmount']; /** first param is the blade template, second param is the array of data need for the invoice */ $pdf = PDF::loadView('invoices.pdf-invoice', [ 'project' => $project, 'userDetails' => self::loggedInUserDetails(), 'amount' => $amount, 'gstAmount' => $gstAmount, 'totalAmount' => $totalAmount, ]); /** Creating the unique name for pdf */ $invoiceName = $project->unique_id.'-'.time().'_'.date('Y-m-d').'.pdf'; /** Save the PDF to /public/uploads/invoices/ folder */ $pdf->save(public_path('uploads/invoices/'. $invoiceName)); } 


Step 5 – Controller Code To Download PDF

In Step 4 you saw how to save the file to particular location. In this step you will learn how to download the PDF file

Use $pdf->download('invoice_name') to download the file

Full Implementation Of the Same. The code remains same as in that of Step 4, only $pdf->download will be used

public static function savePdfInvoice(Project $project) { $priceDetails = ProjectsController::getProjectAmountDetails($project); $amount = $priceDetails['amount']; $gstAmount = $priceDetails['gstAmount']; $totalAmount = $priceDetails['totalAmount']; $pdf = PDF::loadView('invoices.pdf-invoice', [ 'project' => $project, 'userDetails' => self::loggedInUserDetails(), 'amount' => $amount, 'gstAmount' => $gstAmount, 'totalAmount' => $totalAmount, ]); $invoiceNumber = $project->id + 1000; $pdf->download('Invoice#'. $invoiceNumber .'.pdf'); } 


Step 6 – PDF Configurations in config folder

When you do php artisan vendor:publish you will get pdf.php inside config folder where you can do global settings for your PDF.

The following is the default configuration

return [ 'mode' => 'utf-8', 'format' => 'A4', 'author' => '', 'subject' => '', 'keywords' => '', 'creator' => 'StackCoder', 'display_mode' => 'fullpage', 'tempDir' => public_path('temp') ]; 

As you can see in the above settings I have set the format of PDF to A4 sheet. And creator to StackCoder. For more details on the same visit the package in GITHUB repo.


Step 7 – Other PDF Methods

You have seen how to download & save the PDF, now lets see the method offered by PDF

output(): Outputs the PDF as a string. save($filename): Save the PDF to a file download($filename): Make the PDF downloadable by the user. stream($filename): Return a response with the PDF to show in the browser. 

For more details on the PDF option view this Laravel PDF package.


Conclusion

In this article you learnt how to download the Laravel PDF package, how to create routes, how to download & save PDF.

WHAT NEXT?

You might be interested in reading few of my other articles

How To Add Free SSL Certificate In cPanel With ZeroSSL & Certbot

How To Securely SSH Your Server & Push Files With FileZilla

How To Push Files To CPanel / Remote Server using FTP Software FileZilla

How To Install Linux, Apache, MYSQL, PHP (LAMP Stack) on Ubuntu

How To Cache Static Files With NGINX Server

Redirect www to a non-www website or vice versa

How To Create Free SSL Certificate With Lets Encrypt/Certbot In Linux (Single / Multiple Domains)

How To Install Linux, NGINX, MYSQL, PHP (LEMP Stack) on Ubuntu

PHP Built-In Web Server & Testing Your Development Project In Mobile Without Any Software

How To Do Google reCAPTCHA Integration In PHP Laravel Forms

Happy Coding 🙂

programming

via Laravel News Links https://ift.tt/2dvygAJ

May 6, 2020 at 09:03PM