Laravel DomPDF无法创建多个PDF

问题描述 投票:0回答:1

我有两个数据,每个pdf文件将制作一个数据。但是我产生的输出是一个pdf,并且仅显示第一个循环数据。有什么不对?谢谢

public function emailPdf(Request $request){

        DB::beginTransaction();
        // $autocrm_class = new CRM;

        $post = $request->all();

        $post['id_brand'] = 1;
        $post['date_today'] = "2020-02-07";

        // $post['date_today'] = date('Y-m-d', strtotime($post['date_today']));

        $data = BrandOutlet::where('id_brand', $post['id_brand'])->with('brandOutletDelivery.brandOutletTujuan');
        $data->with(["brandOutletDelivery.brandOutletDeliveryItems" => function($q) use($post){
            $q->whereDate('created_at', '=', $post['date_today'])->with('brandItem')->with('unit');
        }]);

        $data->with(["brandOutletOpnamePagi.brandOutletOpnameItems" => function($q) use($post){
            $q->whereDate('created_at', [$post['date_today']])->with('brandItem')->with('unit');

        }]);

        $data->with(["brandOutletOpnameMalam.brandOutletOpnameItems" => function($q) use($post){
            $q->whereDate('created_at', [$post['date_today']])->with('brandItem')->with('unit');

        }]);


        $data = $data->get();

            $html = '';
            foreach($data as $result){
                $view = view('pdf.delivery', ['result' => $result]);
                $html .= $view->render();
            }

            // return $html //I tried to debug by displaying it in View and the result is 2 data
            $pdf = \PDF::loadHtml($html);
            $sheet = $pdf->setPaper('a4', 'landscape');
            $pdf->save('pdf/'.$result['id'].'.pdf');
            return $sheet->stream();
        }
php laravel dompdf
1个回答
0
投票
    $html = [];
        foreach($data as $i=>$result){
            $view = view('pdf.delivery', ['result' => $result]);
            $html[$i]= $view->render();
        }



foreach($html as $htm){
        $pdf = \PDF::loadHtml($htm);
        $sheet = $pdf->setPaper('a4', 'landscape');
        $pdf->save('pdf/'.$result['id'].'.pdf');
        return $sheet->stream();

}

请尝试一下

© www.soinside.com 2019 - 2024. All rights reserved.