create account

How to create PDFs in Laravel with DomPDF by angelggomz

View this thread on: hive.blogpeakd.comecency.com
· @angelggomz · (edited)
$0.08
How to create PDFs in Laravel with DomPDF
#### 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.  <br>
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: 
<br>
**"barryvdh/laravel-dompdf": "^0.8.2",**
<center>
![tutorial2.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1521931786/twqwvhqnv3ulxlre1zhx.png)
<h6> You could use another DomPdf version greater than 0.6 </h6>
</center>
 <br> 
Then, open terminal or console and go to the root directory of project. In my case:  
![tutorial1.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1521931747/qwqhlguyccg0wcqmdlpq.png)
<h6> cd C:\xampp\htdocs\consultoria </h6> 
<br>
 Run the following command 
<h5> $ composer update </h5>

![tutorial5.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1521931941/mt1nkq5kpfznooqbtavr.png)
<h6> $ means instructions to run in console. </h6>
> This command checks if there are new dependencies in the file composer.json and update your system/application.

Go to <strong>“config” </strong> folder, later open <strong> “app.php”  </strong> file and write in service “providers”
 <h6> Barryvdh\DomPDF\ServiceProvider::class, </h6>
![tutorial3.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1521932350/sqy6fuk2mpnm2uzztuvk.png)
>Providers enables that new class in application's service container.

<br> 
In the same file <strong> “app.php” </strong> in <strong> “aliases" </strong> section, add this line about facade: 
<h6>  'PDF' => Barryvdh\DomPDF\Facade::class, </h6> 
![tutorial4.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1521932965/bbynxizarynifwbtak7f.png)
### Creating PDFs:
After to prepare the environment, your system is ready to generate pdfs. 
#### Using a controller:
1. Create a route to a function that generates the pdfs, remember 
<h6>Route::get('name_of_your_route/{your_parameters}','name_of_controller@controllers_method'); </h6>

![tutorial10.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1521933395/szbtq6lmwsd29rdrumjm.png)

 The method in your controller should be

![tutorial11.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1521933710/hdkc72psjabjhexfr9xq.png)
<h6> public function pdf($your_parameters) <br>
{ <br>
$variables=$your_parameters; <br>
$pdf=PDF::loadView(‘views_name’,compact(‘$variables)); <br>
return $pdf->stream(‘pdf’); <br>
} </h6>
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. <br>
In last setence is used the method <strong>stream</strong> to encodes the display of PDF in your browser without download. If you prefer download replace stream for download.  
 <h6>
#### 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. 
![tutorial17.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1521937470/j6imdmh9zoh4qtx8fpxu.png)
<h6>My view </h6>

#### Result PDF:
- The access to the pdf since browser using your route:
**localhost/name_of_your_project/public/name_of_your_route**
![tutorial20.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1521939251/v3ldd0opzunpocoihdpo.png)











<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@angelggomz/how-to-create-pdfs-in-laravel-with-dompdf">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , , , , , ,
properties (23)
authorangelggomz
permlinkhow-to-create-pdfs-in-laravel-with-dompdf
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":1863329,"name":"laravel","full_name":"laravel/laravel","html_url":"https://github.com/laravel/laravel","fork":false,"owner":{"login":"laravel"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","programming","php","laravel","technology"],"users":["controllers","angelggomz"],"links":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1521931786/twqwvhqnv3ulxlre1zhx.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521931747/qwqhlguyccg0wcqmdlpq.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521931941/mt1nkq5kpfznooqbtavr.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521932350/sqy6fuk2mpnm2uzztuvk.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521932965/bbynxizarynifwbtak7f.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521933395/szbtq6lmwsd29rdrumjm.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521933710/hdkc72psjabjhexfr9xq.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521937470/j6imdmh9zoh4qtx8fpxu.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521939251/v3ldd0opzunpocoihdpo.png"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1521931786/twqwvhqnv3ulxlre1zhx.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521931747/qwqhlguyccg0wcqmdlpq.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521931941/mt1nkq5kpfznooqbtavr.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521932350/sqy6fuk2mpnm2uzztuvk.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521932965/bbynxizarynifwbtak7f.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521933395/szbtq6lmwsd29rdrumjm.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521933710/hdkc72psjabjhexfr9xq.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521937470/j6imdmh9zoh4qtx8fpxu.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1521939251/v3ldd0opzunpocoihdpo.png"],"moderator":{"account":"miguepersa","time":"2018-03-25T17:32:53.287Z","flagged":true,"reviewed":false,"pending":false},"questions":[],"score":null}
created2018-03-25 03:07:30
last_update2018-03-25 17:32:54
depth0
children5
last_payout2018-04-01 03:07:30
cashout_time1969-12-31 23:59:59
total_payout_value0.073 HBD
curator_payout_value0.010 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,186
author_reputation675,554,169,735
root_title"How to create PDFs in Laravel with DomPDF"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id46,431,497
net_rshares42,234,799,775
author_curate_reward""
vote details (14)
@mario-trivelli ·
Excelente explicación mi estimado Angel. Te felicito por dominar estos temas. Un fuerte abrazo desde Chile!!!!
properties (22)
authormario-trivelli
permlinkre-angelggomz-how-to-create-pdfs-in-laravel-with-dompdf-20180329t143125294z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-03-29 14:31:27
last_update2018-03-29 14:31:27
depth1
children0
last_payout2018-04-05 14:31:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length110
author_reputation10,127,226,324,505
root_title"How to create PDFs in Laravel with DomPDF"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id47,287,472
net_rshares0
@miguepersa · (edited)
$0.93
Your contribution cannot be approved because it does not follow the [Utopian Rules](https://utopian.io/rules).

- Your post is a machine translated version of <a href="https://styde.net/generar-pdf-en-laravel-5-1-con-dompdf/">THIS</a> tutorial.

You can contact us on [Discord](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
👍  ,
properties (23)
authormiguepersa
permlinkre-angelggomz-how-to-create-pdfs-in-laravel-with-dompdf-20180325t173518063z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-03-25 17:35:27
last_update2018-03-26 19:22:39
depth1
children3
last_payout2018-04-01 17:35:27
cashout_time1969-12-31 23:59:59
total_payout_value0.696 HBD
curator_payout_value0.229 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length363
author_reputation295,179,405,283
root_title"How to create PDFs in Laravel with DomPDF"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id46,542,758
net_rshares361,016,584,582
author_curate_reward""
vote details (2)
@utopian.tip ·
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!
👍  
properties (23)
authorutopian.tip
permlinkre-re-angelggomz-how-to-create-pdfs-in-laravel-with-dompdf-20180325t173518063z-20180325t194236
categoryutopian-io
json_metadata""
created2018-03-25 19:42:36
last_update2018-03-25 19:42:36
depth2
children2
last_payout2018-04-01 19:42:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length160
author_reputation238,310,597,885
root_title"How to create PDFs in Laravel with DomPDF"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id46,560,954
net_rshares566,489,736
author_curate_reward""
vote details (1)
@angelggomz ·
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.
properties (22)
authorangelggomz
permlinkre-utopiantip-re-re-angelggomz-how-to-create-pdfs-in-laravel-with-dompdf-20180325t194236-20180326t215850207z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-03-26 21:58:51
last_update2018-03-26 21:58:51
depth3
children1
last_payout2018-04-02 21:58:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length139
author_reputation675,554,169,735
root_title"How to create PDFs in Laravel with DomPDF"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id46,775,640
net_rshares0