What Will I Learn?
In this tutorial you will learn
- How to install/add a package in your laravel project.
- Create PDFs in your web system.
What do you need? Requirements
- A Laravel version > 5.0 installed.
- Basic programming and architecture MVC knowledges.
- Text editor (Atom, Sublime Text, etc)...
- XAMPP (local server).
- CSS and HTML knowledge.
Difficulty
- Intermediate
Preliminary
There are web systems that need generates documents, cards, registrations or custom templates to users.
DomPDF is a package created and developed by Barryvdh that allows generate pdfs, just display in browser or download.
Install package:
First open “composer.json” file of your project and add in section require the next line:
"barryvdh/laravel-dompdf": "^0.8.2",
You could use another DomPdf version greater than 0.6
Then, open terminal or console and go to the root directory of project. In my case:
cd C:\xampp\htdocs\consultoria
Run the following command
$ composer update
$ means instructions to run in console.
This command checks if there are new dependencies in the file composer.json and update your system/application.
Go to “config” folder, later open “app.php” file and write in service “providers”
Barryvdh\DomPDF\ServiceProvider::class,
Providers enables that new class in application's service container.
In the same file “app.php” in “aliases" section, add this line about facade:
'PDF' => Barryvdh\DomPDF\Facade::class,
Creating PDFs:
After to prepare the environment, your system is ready to generate pdfs.
Using a controller:
- Create a route to a function that generates the pdfs, remember
Route::get('name_of_your_route/{your_parameters}','name_of_controller@controllers_method');
The method in your controller should be
public function pdf($your_parameters)
{
$variables=$your_parameters;
$pdf=PDF::loadView(‘views_name’,compact(‘$variables));
return $pdf->stream(‘pdf’);
}
We need declare a variable of type PDF. This variable is responsible to generate a PDF of our view.
In the App variable (pdf) is necessary include another data that user requests since a form or to the database, compact is the word used, compact creates an array with values. Some people use “with” but in my case it doesn’t work properly.
In last setence is used the method stream to encodes the display of PDF in your browser without download. If you prefer download replace stream for download.
Considerations:
- When you create a blade view for a pdf, you should only write html and css setences. Don’t include javascript or css frameworks.
My view
Result PDF:
- The access to the pdf since browser using your route:
localhost/name_of_your_project/public/name_of_your_route
My view
localhost/name_of_your_project/public/name_of_your_route
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it does not follow the Utopian Rules.
You can contact us on Discord.
[utopian-moderator]
Hey @miguepersa, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
I was student with styde.net, but if you read my tutorial the only thing similar is the installation process and it is for obvious reasons.
And sorry if you think I did a machine translated but my english is too basic.
Excelente explicación mi estimado Angel. Te felicito por dominar estos temas. Un fuerte abrazo desde Chile!!!!