我使用laravel / dom-pdf生成发票。问题是我有500个帐户要生成,我设法将数据传递给我的视图并循环它。但我在保存pdf时遇到问题。因为保存的pdf代码在我的控制器中..
TestpdfController
$pdf = PDF::loadView('invoices.viewinvoicepdf');
$pdf->save(path/to/save'/'.$inv->accountid.'.pdf'); // problem here is that i cannot grab the accountid for each user and only the last account will be created.
我的第二个选项是内置到foreach,但我认为它效率不高,因为它因为foreach而将视图呈现500次。
foreach($invoices as $inv){
$pdf = PDF::loadView('invoices.viewinvoicepdf',compact('invoices'));
$pdf->save(path/to/save'/'.$inv->accountid.'.pdf');
}
如何解决这个问题?
如果您需要单独保存每个PDF文件,我认为您不能选择使用循环。我假设你的视图文件中有一个循环。您可以在控制器中移动该循环,并让您的视图文件只接受一个invoice
。