My concern is that I am generating pdf with dompdf (barryvdh/laravel-snappy) and laravel and I want to know how I can generate a header and footer set for all generated pages since more than one page can be generated. With only one I have no problems because I am adding them with the header and footer structure in the view (pdf/vista.blade.php), but it is not repeated in the other sheets.
$pdf = \PDF::loadView('pdf.vista',['entidad'=>$request->dirigidoa,
'contacto'=>$request->persona,
'ciudad'=>$request->ciudad,
'referencia'=>$request->referencias,
'nombres'=>$nombres,
'descripcion'=>$descripcion,
'condicion'=>$condicion,
'created_at'=>$datetime]);
return $pdf->stream('archivo.pdf');
In the example image of what I have at the moment without the error that it is lengthened.
I think I understood your concern or need. If you look closely, you use Laravel's built-in view template blade system to work. Therefore, my recommendation is very simple.
Just as you have a "layout.pdf" or template for all your pdfs, do exactly the same with the header and footer.
This is done in the following way.
In the place of the header and footer you put:
@include('partials.pdf.header')
@include('partials.pdf.footer')
Now you generate a folder called "partials" or partial views, which are pieces of blade code that you are going to use dynamically or on the other hand, simply to organize the code.
Within the folder
partials
, you generate another one to be organized, calledpdf
(As I have been able to see the structure of your project "layout.pdf", I assumed that within "templates you have pdf" because within partials, another pdf. Well that is already you organize.)Now wherever you go to store your "header and footer partial snippets", you generate their blade templates.
header.blade.php
andfooter.blade.php
.These two will only contain that little bit that you want to put in all your pdfs.
Now lastly, in your PDF view, if you remember, we put
@include('partials.pdf.header')
@include('partials.pdf.footer')
which means that it will look for the path of your partial chunk there and include it. Therefore, you can already modify the rest of the pdf, that this template will always execute that header.
Now, if what you want is a dynamic header, you can also use the blade logic so that some templates or others are executed.
For example,
@if($headerPdf == 'empresa1') @include('partials.pdf.headerEmpresa1') @else @include('partials.pdf.header') @endif
Let's say here we have a header in the "else" that in case of not finding any previous case that matches, puts a "by default called header".
And if the variable that you passed to the if condition, which would come from the controller, is positive, there you would have another different header.
I should add about this, that if your view or pdf is run from a controller that doesn't have or know the variable you sent, "$headerPdf", it's logically possible that you'll get an error that the variable doesn't exist.
In my case when this situation occurs, you can solve it with
isset()
@if(isset($headerPdf)) @if($headerPdf == 'empresa1') @include('partials.pdf.headerEmpresa1') @endif @else @include('partials.pdf.header') @endif
What it does is check if the variable exists or contains something. but let go
Although it is true that there are more ways to make it dynamic, such as sending the variables directly from the controller and executing them in the header that you have there in your image, such as the image path, and in this way it would be shown dynamic.
I hope it helps you. Greetings =)
PS: I recommend you to read the documentation about blade, since it has some really interesting ways to manage your views, group them, organize them and execute them depending on many factors, variables or however you really like. So I'll give you a link.
https://laravel.com/docs/5.6/blade
Remember to rate positively if it is your answer to help other users find it faster. Cheers