Laravel & laravel-dompdf page breaking shows only 1st page - php

Trying to create a PDF invoice using barryvdh's laravel dompdf wrapper. I want the invoice to begin on a new page when a certain number of rows is reached. However I'm unsure on how to do it since I don't get the expected results. Here is my controller:
function invoice() {
$resellerId = Input::get('resellerId');
$startdate = Input::get('invoiceStartDate');
$enddate = Input::get('invoiceEndDate');
$orders = \App\order::where('reseller',1)->orderBy('date', 'desc')->where('customer_id' , $resellerId )->where('date', '>=',$startdate)->where('date', '<=',$enddate)->get();
$pdf = PDF::loadView('pdf.invoicePrint',['orders' => $orders]);
// $PDFOutput = $pdf->output();
return $pdf->stream("trestaki.pdf");
}
And here is the relevant portion of the blade:
<table style="border:1px solid #000;width: 100%;">
<tr>
<td>
<span style="font-size: 14px;font-weight: 900;">Art. Nr.</span>
</td><td>
<span style="font-size: 14px;font-weight: 900;">Benämning</span>
</td><td>
<span style="font-size: 14px;font-weight: 900;">Antal</span>
</td><td>
<span style="font-size: 14px;font-weight: 900;">Pris</span>
</td><td>
<span style="font-size: 14px;font-weight: 900;">Summa</span>
</td></tr>
<?php $i=0; ?>
#foreach($orders as $order)
<tr>
<td>{{ $order->order_id }}</td>
<td>{{ $order->getProduct->mobile }} {{ $order->getProduct->model }}</td>
<td>1</td>
<td>{{ $order->price }}</td>
<td>{{ $order->price }}</td>
</tr>
<?php
$i++;
if($i % 25 == 0)
echo "<div style='page-break-after: always;'></div>";
?>
#endforeach
<tr>
<td colspan=5 style="padding-top: 20px;">
<table style="width: 45%;">
<tr>
<td>
<span style="font-size: 14px;font-weight: 900;">Moms %</span>
</td>
<td>
<span style="font-size: 14px;font-weight: 900;">Netto</span>
</td>
<td>
<span style="font-size: 14px;font-weight: 900;">Moms</span>
</td>
<td>
<span style="font-size: 14px;font-weight: 900;">Summa</span>
</td>
</tr>
<tr>
<td>25%</td>
<td>639,20</td>
<td>159,80</td>
<td>799,00</td>
</tr>
</table>
</td>
</tr>
</table>
The thing is that if I remove all the closing tags from the echo, the webpage times out.

The problem was the fixed position of the main div. After removing it the page did break correctly.

Related

How I can avoid this error on DOMPDF with laravel 8 when working with large tables?

I am working with DOMPDF in Laravel 8, when I create a table with many records and jumps to the next page, it does not perform the page break as it should, Could someone help me to know what I am doing wrong?. I was reading many articles on the internet and none of them worked for me.
Here is my code:
<div class="box-products-table">
<table class="table table-striped table-bordered">
<tbody>
<tr>
<td class="text-center roboto">{{ trans('image') }}</td>
<td class="text-center roboto">{{ trans('just_name') }}</td>
<td class="text-center roboto">{{ trans('quantity') }}</td>
<td class="text-center roboto">{{ trans('unit_price') }}</td>
<td class="text-center roboto">{{ trans('taxes') }}</td>
<td class="text-center roboto">{{ trans('subtotal') }}</td>
<td class="text-center roboto">{{ trans('total_price') }}</td>
</tr>
#for($i = 0; $i < 100; $i++)
<tr>
<td class="text-center"><img src="{{ public_path('storage/uploads/products/d6d74f351d482bb41787e86350ace02e.jpg') }}" height="70px"></td>
<td class="text-center">PORTATIL DELL 4HWRT</td>
<td class="text-right">526</td>
<td class="text-right">$ 4.985.650</td>
<td class="text-right">$ 947.273,5</td>
<td class="text-right">$ 99.713.000</td>
<td class="text-right">$ 2.622.451.900</td>
</tr>
#endfor
</tbody>
</table>
</div>
Result:
Go to screenshot (https://ibb.co/Rj5ZPTW)
.page_break {
page-break-before: always;
}
<!-- adjust $n as needed. $n = number of rows that fit in a page -->
#foreach(collect(range(1,100))->chunk($n) as $chunk)
<div class="box-products-table">
<table class="table table-striped table-bordered">
<tbody>
<tr>
<td class="text-center roboto">{{ trans('image') }}</td>
<td class="text-center roboto">{{ trans('just_name') }}</td>
<td class="text-center roboto">{{ trans('quantity') }}</td>
<td class="text-center roboto">{{ trans('unit_price') }}</td>
<td class="text-center roboto">{{ trans('taxes') }}</td>
<td class="text-center roboto">{{ trans('subtotal') }}</td>
<td class="text-center roboto">{{ trans('total_price') }}</td>
</tr>
#foreach($chunk as $i)
<tr>
<td class="text-center"><img src="{{ public_path('storage/uploads/products/d6d74f351d482bb41787e86350ace02e.jpg') }}" height="70px"></td>
<td class="text-center">PORTATIL DELL 4HWRT</td>
<td class="text-right">526</td>
<td class="text-right">$ 4.985.650</td>
<td class="text-right">$ 947.273,5</td>
<td class="text-right">$ 99.713.000</td>
<td class="text-right">$ 2.622.451.900</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<div class="page-break"></div> <!-- break page -->
#endforeach

how to send collection by compact to pdf function

I am trying to send an array per compact but when viewing it it throws an error.
SaleController.php
public function generarRecibo($id)
{
$sales = Sale::with('client', 'products')->where('id', '=', $id)->get();
$pdf = PDF::loadView('pdf.sales-recibo', compact(['sales']) );
return $pdf->stream('Recibo N° '.$id.'.pdf');
}
sales-recibo.blade.php
<table class="table">
<tbody>
#foreach ($sales->products as $product)
<tr>
<td style="border: 0px;" colspan="3">{{ $product->name }}</td>
</tr>
<tr>
<td style="border: 0px;">
<ul>
<li>{{ $product->quantity }} X ${{ number_format($product->price, 0,',','.') }}</li>
</ul>
</td>
<td style="border: 0px;" width="150px"></td>
<td style="border: 0px;" width="150px">${{ number_format($product->price, 0,',','.') }}</td>
</tr>
#endforeach
</tbody>
</table>
Instead of get() use find() method
$sales = Sale::with('client', 'products')->find($id);
and also you have issue with compact
$pdf = PDF::loadView('pdf.sales-recibo', compact('sales') );

Display Multiple Images with Single Answer

I am working on the below code, where the same question can have multiple images as an answer.
So, they are displaying properly but, with every image, the same question is repeating multiple times.
So, I want the question to display only once and below that the answers should be displayed. Below is the code. I have created a table and the images will be displayed after the question. Same has been highlighted below. I have done some POC, please have a look.
<!--Question and Answer Section Open-->
#if($submited_documents->count() !=0)
<table class="table table-bordered">
<tbody>
#if(count($submited_documents) > 0)
#foreach($submited_documents as $documents)
<tr>
<th style="width:50% !important; border:1px solid lightgray;"><strong>Question{{$documents->sequence}} : </strong></th>
<th style="border:1px solid lightgray; "><strong>Answer : </strong></th>
</tr>
<tr>
<td>{{ $documents->question }}</td>
<td>{{ $documents->comments }}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
<table>
<tbody>
#foreach($submited_documents as $documents)
<tr>
<td style="border-radius: 2px;">
<strong>Question{{$documents->sequence}} : </strong>{{ $documents->question }}
</td>
</tr>
#if(count($documents->pictures) > 0 )
#foreach($documents->pictures as $document_picture)
<tr>
<td class="td_class">
#if($document_picture->filename != null)
<a style="" href="{{ URL::to('public/uploads/pickups/'.$document_picture->filename) }}" target="new">
<img src="{{ URL::to('public/uploads/pickups/'.$document_picture->filename) }}"
alt="{{ $document_picture->filename }}" title="{{ $document_picture->filename }}"
style="width: 50% !important;height: 50% !important;" ></a><br>
#endif
</td>
</tr>
#endforeach
#endif
#endforeach
</tbody>
</table>
#endif
<!--Question and Answer Section End-->
Any help is appreciated..

Invoice pdf in php with wkhtmltopdf snappy Laravel

Hi every body i have to make an invoice pdf in my LARAVEL project.
I succeeed to generate a pdf with this code :
$path_name_file = '../ArchivePdf/'. $name_file . '.pdf';
if ($document->type->type == 'A'){
$pdf = SnappyPdf::loadView('ribbon.report.pdf-achat', ['document' =>$document])
->save($path_name_file);
}else{
$pdf = SnappyPdf::loadView('ribbon.report.pdf-new', ['document' =>$document])->save($path_name_file);
}
My problem is in the blade view (HTML template) that i want to put an header and a footer and a main table with items
I want that the footer will be on all pages
How can i do that?
Thanks
That is my actual html view:
<!DOCTYPE html>
<html>
<header>
<meta charset="utf-8">
<title>PDF Devis</title>
{{--<link href="{{ asset("/css/style_pdf.css")}}" rel="stylesheet" type="text/css" />--}}
<style>
.header-1{
border-top-left-radius: 7px;
border-top-right-radius: 7px;
/*left: 5%;*/
width: 100%;
border-style: solid;
font-size: 13px;
border-width: 2px;
}
th {
background-color: rgba(158, 158, 158, 0.22);
text-align: center;
}
.line{
width: 100%;
}
.line tr{
padding: 0;
}
.td-20{
width:20%
}
.td-30{
width:30%
}
.td-70{
width: 70%;
}
.td-40{
width: 40%;
}
.td-50{
width: 50%;
}
.img img{
width:100%; height:auto;
}
.name-soc{
font-size: 25px;
color: orangered;
direction: rtl;
font-family: arial;
font-style: bold;
}
.space{
padding-top: 20px;
}
.title{
font-weight: bold;
height: 50px;
color: white;
background-color: rgba(255, 87, 34, 0.89);
font-size: 25px;
border-radius: 5px;
bottom: 15px;
text-align: center;
}
.items{
}
.more_line{
height: 17px;
}
/*.items-table{*/
/*page-break-after: always;*/
/*page-break-inside: avoid;*/
/*}*/
thead { display: table-header-group }
tfoot { display: table-row-group }
.items-row { page-break-inside: avoid }
table { border-collapse: separate }
.note-table{
text-align: right;
width: 100%;
}
</style>
</header>
<body>
<table class="line">
<tr>
<td rowspan="4" class="td-30">
<div class="img"> <img src="{{ public_path('/ribbon/pdf/fre.jpg') }}" alt="logo"/></div>
</td>
<td class="td-30">
<div></div>
</td>
<td class="td-40">
<div class="name-soc">
דקופלוס פרקט בע״מ
</div>
</td>
</tr>
<tr>
<td class="td-30">
<div></div>
</td>
<td class="td-40">
<div class="name-soc">
</div>
</td>
</tr>
<tr>
<td class="td-20">
<div></div>
</td>
<td class="td-20">
<div></div>
</td>
</tr>
<tr>
<td class="td-20">
<div></div>
</td>
<td class="td-20">
<div></div>
</td>
</tr>
</table>
<table class="line">
<tr class="entete">
<td class="logo">
</td>
<td class="donnee_soc">
<div dir="rtl">{{trans('dcp_pdf.num_doc')}} : {{ $document->code }} </div>
</td>
</tr>
<tr>
<td class="td-50">
<div>
<table class="header-1" dir="rtl">
<tr>
<th colspan="2">{{trans('dcp_pdf.coord_cust')}}</th>
</tr>
<tr class="" >
<td>
<div style="width: 20%">{{trans('dcp_pdf.name')}}</div>
</td>
<td>
<div class="encadre_data">{{ $document['customer']['name'] }} {{ $document['customer']['surname'] }}</div>
</td>
</tr>
<tr class="" >
<td>
<div style="width: 20%">{{trans('dcp_pdf.adress')}}</div>
</td>
<td>
<div class="encadre_data">{{ $document['adress']['street'] }} {{ $document->adress->city->cityT[0]->texte }}</div>
</td>
</tr>
<tr class="" >
<td>
<div style="width: 20%">{{trans('dcp_pdf.telephone')}}</div>
</td>
<td>
<div class="encadre_data">{{ $document['customer']['phone'] }} / {{ $document['customer']['phone2'] }}</div>
</td>
</tr>
<tr class="" >
<td>
<div style="width: 20%">{{trans('dcp_pdf.mail')}}</div>
</td>
<td>
<div class="encadre_data">{{ $document['customer']['email'] }}</div>
</td>
</tr>
</table>
</div>
</td>
<td class="td-50">
<div>
<table class="header-1" dir="rtl">
<tr>
<th colspan="2">{{trans('dcp_pdf.coord_fact')}}</th>
</tr>
<tr class="" >
<td>
<div style="width: 20%">{{trans('dcp_pdf.name')}}</div>
</td>
<td>
<div class="encadre_data">{{ $document['customer']['name'] }} {{ $document['customer']['surname'] }}</div>
</td>
</tr>
<tr class="" >
<td>
<div style="width: 20%">{{trans('dcp_pdf.adress')}}</div>
</td>
<td>
<div class="encadre_data">{{ $document['adress']['street'] }} {{ $document->adress->city->cityT[0]->texte }}</div>
</td>
</tr>
<tr class="" >
<td>
<div style="width: 20%">{{trans('dcp_pdf.telephone')}}</div>
</td>
<td>
<div class="encadre_data">{{ $document['customer']['phone'] }} / {{ $document['customer']['phone2'] }}</div>
</td>
</tr>
<tr class="" >
<td>
<div style="width: 20%">{{trans('dcp_pdf.mail')}}</div>
</td>
<td>
<div class="encadre_data">{{ $document['customer']['email'] }}</div>
</td>
</tr>
</table>
</div>
</td>
</tr>
<tr >
<td colspan="2" class="space">
<div>
<table class="header-1" dir="rtl">
<tr>
<th colspan="2">{{trans('dcp_pdf.coord_livraison')}}</th>
</tr>
<tr class="" >
<td>
<div style="width: 20%">{{trans('dcp_pdf.name')}}</div>
</td>
<td>
<div class="encadre_data">{{ $document['customer']['name'] }} {{ $document['customer']['surname'] }}</div>
</td>
</tr>
<tr class="" >
<td>
<div style="width: 20%">{{trans('dcp_pdf.adress')}}</div>
</td>
<td>
<div class="encadre_data">{{ $document['adress']['street'] }} {{ $document->adress->city->cityT[0]->texte }}</div>
</td>
</tr>
<tr class="" >
<td>
<div style="width: 20%">{{trans('dcp_pdf.telephone')}}</div>
</td>
<td>
<div class="encadre_data">{{ $document['customer']['phone'] }} / {{ $document['customer']['phone2'] }}</div>
</td>
</tr>
<tr class="" >
<td>
<div style="width: 20%">{{trans('dcp_pdf.mail')}}</div>
</td>
<td>
<div class="encadre_data">{{ $document['customer']['email'] }}</div>
</td>
</tr>
</table>
</div>
</td>
</tr>
<tr >
<td colspan="2" class="space">
<div class="title" dir="rtl">
{{ $document->type->typeT[0]->texte }} - {{ $document->code }}
</div>
</td>
</tr>
<tr >
<td colspan="2" class="space">
<table class="items header-1 items-table" dir="rtl">
<thead>
<tr class="more_line">
<th class="Désignation" >{{trans('dcp_pdf.desc_article')}}</th>
<th class="other">{{trans('dcp_pdf.conditionnement')}}</th>
<th class="other">{{trans('dcp_pdf.quantity_m2')}}</th>
<th class="other">{{trans('dcp_pdf.nb_colis')}}</th>
<th class="other">{{trans('dcp_pdf.pu_ht')}}</th>
<th class="other">{{trans('dcp_pdf.discount')}}</th>
<th class="other">{{trans('dcp_pdf.total_ht')}}</th>
</tr>
</thead>
#foreach($document->items as $item)
<tr class="1 items-row">
<td class="Designation" dir="rtl"><div dir="rtl">{{ $item->material->textes[0]->texte }}</div></td>
<td class="Cdt"></td>
<td class="Qtem2">{{ $item->quantity }}</td>
<td class="Nb_colis">{{ $item->qte_col }}</td>
<td class="PU">{{ number_format($item->price, 2, '.', ',') }}</td>
<td class="Rem">{{ number_format($item->discount, 2, '.', ',') }} {{ $item->typeRemise->code }}</td>
<td class="Total"> {{ number_format($item->price_wo_vat, 2, '.', ',') }}</td>
</tr>
#endforeach
#for ($i = $document->items->count(); $i < 20; $i++)
<tr class="more_line">
<td class="Designation" dir="rtl"><div dir="rtl"></div></td>
<td class="Cdt"></td>
<td class="Qtem2"></td>
<td class="Nb_colis"></td>
<td class="PU"></td>
<td class="Rem"></td>
<td class="Total"></td>
</tr>
#endfor
</table>
</td>
</tr>
<tr >
<td colspan="2" >
<table class="note-table" dir="rtl">
<tr class="" >
<td>
<div class="encadre_data">{{ $document['note'] }} </div>
</td>
</tr>
<tr class="" >
<td >
<div class="encadre_data">{{ $document['comment'] }} </div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="td-30">
<div>
<table class="header-1" dir="rtl">
<tr>
<td>{{trans('dcp_pdf.montant_ht')}}</td>
<td>{{ number_format($document->amount_wo_taxe, 2, '.', ',') }}</td>
</tr>
<tr>
<td>{{trans('dcp_pdf.dont_remise')}}</td>
<td></td>
</tr>
#if ($document['customer']['code_tva'] === 1)
<tr>
<td>{{trans('dcp_pdf.tva')}} {{ number_format($document->taxe, 2, '.', ',') }} %</td>
<td>{{ number_format($document->amount - $document->amount_wo_taxe, 2, '.', ',') }}</td>
</tr>
#endif
<tr>
<td>{{trans('dcp_pdf.montant_ttc')}}</td>
<td>{{ number_format($document->amount, 2, '.', ',') }}</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
</div>
</td>
<td class="td-70">
<div>
<table class="header-1" dir="rtl">
<tr>
<th colspan="4">{{trans('dcp_pdf.reglements')}}</th>
</tr>
<tr>
<th>{{trans('dcp_pdf.date')}}</th>
<th>{{trans('dcp_pdf.description')}}</th>
<th>{{trans('dcp_pdf.montant')}}</th>
<th>{{trans('dcp_pdf.mode_paiement')}}</th>
</tr>
#foreach($document->reglements as $reglement)
<tr>
<td>{{ $reglement->date }}</td>
<td>{{ $reglement->description }}</td>
<td>{{ number_format($reglement->amount, 2, '.', ',') }}</td>
<td>{{ $reglement->mode->modePaiementT[0]->texte }}</td>
</tr>
#endforeach
</table>
</div>
</td>
</tr>
<tr>
<td>
<div></div>
</td>
<td>
<div></div>
</td>
</tr>
<tr>
<td>
<div></div>
</td>
<td>
<div></div>
</td>
</tr>
<tr>
<td>
<div></div>
</td>
<td>
<div></div>
</td>
</tr>
<tr>
<td>
<div></div>
</td>
<td>
<div></div>
</td>
</tr>
<tr>
<td>
<div></div>
</td>
<td>
<div></div>
</td>
</tr>
</table>
</body>
</html>
Here is the documentation about header and footer with Wkhtmltopdf support.
You can try
For Header
setOption('header-html', 'Your Header');
For Footer
setOption('footer-html', 'Your Footer');
Just make sure that you need to add <!DOCTYPE HTML><html lang='en-US'> in the beginning of your header/footer HTML!

domPDF Nested Tables Issue (Ractangular Whitespace in the bottom-right corcer)

I'm using Laravel to create a PDF report that has a big table which has 3 other tables nested inside for the sake of side-by-side viewing. I know domPDF won't support Floating so I had to do this. I couldn't also use Positioning since there will be repeatations of the same element on page so this seemed like the best idea.
However, whenever I have multiple nested tables in PDF, this weird thing happens:
I have been able to find any solutions to this online.
Source Code:
#foreach ($employees as $employee)
<table style="width: 100%; margin-top: 5px; border-bottom: 1px solid grey; margin-bottom: 5px;" border="0" cellspacing="0" cellpadding="5">
<tbody>
<tr>
<td width="25%" style="background: lightgrey;">Name: <strong>{{$employee['info']->name}}</strong></td>
<td width="25%" style="background: lightgrey;">Mobile: <strong>{{$employee['info']->mobile}}</strong></td>
<td width="25%">Date: <strong>{{$inputs['date']}}</strong></td>
<td width="25%">Remarks:</td>
</tr>
<tr>
<td colspan="2">
<table style="width: 100%;" border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th colspan="3">Receive</th>
</tr>
</thead>
<tbody>
#if ($employee['given']->isEmpty())
<tr>
<td colspan="2"></td>
</tr>
#else
#foreach ($employee['given'] as $tr)
<tr>
<td>{{ strtoupper($tr->category) }}</td>
<td style="text-align: right;">{{ App\Transaction::formatMoney($tr->amount) }}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</td>
<td colspan="2">
<table style="width: 100%;" border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th colspan="3">Return</th>
</tr>
</thead>
<tbody>
#if ($employee['returned']->isEmpty())
<tr>
<td colspan="2"></td>
</tr>
#else
#foreach ($employee['returned'] as $tr)
<tr>
<td>{{ strtoupper($tr->category) }}</td>
<td style="text-align: right;">{{ App\Transaction::formatMoney($tr->amount) }}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table style="width:100%" border="1" cellpadding="2" cellspacing="0">
<tbody>
<tr>
<td>Total Receive</td>
<td style="text-align: right;">{{ App\Transaction::formatMoney($employee['given']->sum('amount')) }}</td>
</tr>
<tr>
<td>Total Return</td>
<td style="text-align: right;">{{ App\Transaction::formatMoney($employee['returned']->sum('amount')) }}</td>
</tr>
<tr style="background-color: {{ $employee['returned']->sum('amount') - $employee['given']->sum('amount') < 0 ? 'lightgrey' : '' }}">
<td>Balance</td>
<td style="text-align: right;">{{ App\Transaction::formatMoney($employee['returned']->sum('amount') - $employee['given']->sum('amount')) }}</td>
</tr>
<tr>
<td>B2B</td>
<td style="text-align: right;">{{ App\Transaction::formatMoney($employee['b2b']) }}</td>
</tr>
<tr>
<td>Sale</td>
<td style="text-align: right;">{{ App\Transaction::formatMoney($employee['sale']) }}</td>
</tr>
</tbody>
</table>
</td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
#endforeach
It looks like your table headers ("Receive" and "Return") are set to colspan="3" while the actual number of columns in that table are only 2. It's obviously some buggy behavior around rendering the table, but if you set the colspan to 2 it should render as expected.

Categories