I have 100+ items, items have diff data.I want to print n number of pdf with single click, each pdf can have n number of pages. I am using foreach and getting results but when item have 2000+ rows (1 page have 25 rows)then its getting timeout. Here is my code:
$finalItems = array('1,2,3,4',5,6,..............);
foreach($finalItems as $item){
$i++;
$getGrp = DB::table('item_master')->select('group')->where('item_name', $item)->get();
$rs = json_decode(json_encode($getGrp), true);
$getGP = call_user_func_array('array_merge', $rs);
$saleData = DB::table('sale_data')->where('item_name', $item)->where('site_id', $site_id)->whereBetween('bill_date',[$_POST['fromDate'],$_POST['toDate']])->get();
$purchaseData= DB::table('purchase_data')->where('item_name', $item)->where('site_id', $site_id)->whereBetween('bill_date',[$_POST['fromDate'],$_POST['toDate']])->get();
$stock_trf = DB::table('stock_transfer')->where('item_name',$item)->where('site_id', $site_id)->whereBetween('bill_date',[$_POST['fromDate'],$_POST['toDate']])->get();
$sales = json_decode(json_encode($saleData), true);
$purchase = json_decode(json_encode($purchaseData), true);
$stock = json_decode(json_encode($stock_trf), true);
$res = array_merge($sales, $purchase, $stock);
$groupName = $getGP['group'];
$pdf = PDF::loadView('myPDF', compact('res'));
$pdf->setPaper('a3', 'landscape');
$pdf->save(public_path().'/pdf/item_'.$item.'.pdf')>stream('item_'.$item.'.pdf');
$pdf_name[] = 'item_'.$item.'.pdf';
}
/****************** HTML View File ***************************************/
@$dlr = array_chunk($res, 100);
$loopCount = count($dlr);
@for($i=0; $i<$loopCount; $i++)
@foreach ($dlr[$i] as $sldata[$i])
<tr >
<td style="width:2%"></td>
<td style="width:5.10%"></td>
<td style="width:9.10%"></td>
<td style="width:7.10%"></td>
<td style="width:5.10%">{{ $sldata[$i]['batch_no'] }}</td>
<td style="width:5.10%">{{ $sldata[$i]['mfg_date'] }}</td>
<td style="width:5.10%">{{ $sldata[$i]['exp_date'] }}</td>
<td style="width:5.10%"></td>
<td style="width:5.10%"></td>
<td style="width:5.10%"></td>
<td style="width:3.10%">{{ $last_balance }}</td>
<td style="width:5.10%"></td>
<td style="width:3.10%">{{ $sldata[$i]['quantity_in_kgltr'] }}</td>
<td style="width:4.10%"></td>
<td style="width:4.10%"></td>
<td style="width:5.10%">
<?php
$tocl = (int)$sldata[$i]['quantity_in_kgltr'];
echo @$last_balance -= $tocl; ?>
</td>
<td style="width:5.10%">{{ $sldata[$i]['bill_no'] }}</td>
<td style="width:5.10%">{{ date('d-m-Y', strtotime($sldata[$i]['bill_date'])) }}</td>
<td style="width:8.10%">{{ $sldata[$i]['sales_to_customer_name'] }}</td>
<td style="width:3.10%"></td>
</tr>
@endforeach
@endfor
Task achieved with array_chunk():
$flag = 0;
$chunks = array_chunk($res, 100);
foreach($chunks as $listitems){
$flag++;
$groupName = @$getGP['group'];
view()->share('group',$groupName);
view()->share('fromdt',$fromDate);
view()->share('siteid',$site_id);
view()->share('inm',$inm);
$pdf = PDF::loadView('myPDF', compact('listitems'));
$pdf->setPaper('a3', 'landscape');
$pdf->save(public_path().'/pdf/item_'.$item.''.$flag.'.pdf')->stream('item_'.$item.''.$flag.'.pdf');
$pdf_name[] = 'item_'.$item.''.$flag.'.pdf';
}