Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
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
.
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
Laravel PDF
Package With ComposerInstall 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. The following command create pdf.php
in config
folder, in this pdf.php file you can make changes to PDF package.
config/pdf.php
php artisan vendor:publish
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.
Use the above GITHUB GIST code for quickly creating the template for PDF inside view/invoices/pdf-invoice.blade.php
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.
If you would like to learn more on Route Model Binding then I have written an article on it Route Model Binding In Laravel & Change Default Column id To Another Column
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.
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));
}
}
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));
}
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');
}
config
folderWhen 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.
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.
In this article you learnt how to download the Laravel PDF package, how to create routes, how to download & save PDF.
Install Apache Web Server On Ubuntu 20.04 / Linux & Manage It
Upload File From Frontend Server {GuzzleHttp} To REST API Server In PHP {Laravel}
Cache Static Files With NGINX Server
Composer Install v/s Composer Update
Create A Composer Package? Test It Locally And Add To Packagist Repository
Move Uploaded Files From Local Computer Or Server To Amazon S3 Bucket In PHP
Stripe Payment Integration With Laravel
Send SMS With Amazon SNS (Simple Notification Service) Using PHP
Setup Docker for NodeJs, MongoDB, MongoDB Compass
SummerNote WYSIWYG Text Editor
Send Email In PHP With PHPMailer
Localization In Laravel REST API