Collect multiple files into single PDF

Viewed 651

I use in my project barryvdh laravel-dompdf package.
Users might generate a PDF with some personal information, till here everything is ok:

$pdf = PDF::loadView('personalInfo', $userData);
$pdfFileName = "personal-info-{$userData['username']}.pdf";
return $pdf->download($pdfFileName);

Admin should be able to download information of users, and in case of selecting several users, instead of generating several files I would like to collect all users PDFs into a single file.

How can I do it?

1 Answers

You can create like this below

For example you have personalInfo blade template which is now for particular user .So loop that view and store it in array like below

$pages=[];
foreach($userDataList as $key=>$userData){

$pages[]=(string)view('personalInfo', $userData);

}

$pdf = App::make('dompdf.wrapper');
    $pdf->loadView('index', ['pages' => $pages]);
    return $pdf->stream();

then you can create seperate blade template to show these views.Here i will name it as index.blade.php

<style type="text/css" media="print">
    div.page
    {
        page-break-after: always;
        page-break-inside: avoid;
    }
</style>

@foreach($pages as $page)
    <div class="page">
        {!! html_entity_decode($page) !!}
    </div>
@endforeach

Method:2

loop all user detail inside personalInfo blade template and at the end of each loop add below page break style

 <p style="page-break-before: always;"></p>

Example inside personalinfo blade

@foreach($pages as $page)
  personal information content
<p style="page-break-before: always;"></p>
@endforeach

Also i personally prefer laravel snappy for multiple page because it render faster than laravel dompdf.Only thing is to server configuration bit head ache if you are using windows Ref:https://github.com/barryvdh/laravel-snappy

Related