简体   繁体   中英

How to print 500+ pdf in single click with dompdf , while each pdf can have n number of pages?

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

So you do get a timeout?

That is to be expected for a long job. Easy way out is increasing the timeout: set_time_limit

But if it takes a long time still you might encounter other timeouts (notably the webservers: Apache or IIS or whatever)

Printing so many PDF documents isn't exactly a webserver job.

I would advise you look into a solution where you use PHP without a webserver, AKA commandline.

Make sure you set set_time_limit to 0 (meaning it doesn't terminate).

The pieces of script you posted clearly show you are making a webpage. In case you need to make a selection from the webpage, find a way to transfer that selection to the script you will run commandline. (eg by writing the PDF filenames to a file. Then you read that file from your PDF printing script)

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';
           } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM