[英]Multi-page pdf using barryvdh dompdf Laravel-4 and loadView rather than loadHtml
I'm trying to create batched pdf invoices (using: barryvdh/laravel-dompdf ) that can be downloaded and printed. 我正在尝试创建可下载和打印的批量pdf发票(使用: barryvdh / laravel-dompdf )。 Sending the invoices via email is easy because the 'send' command works perfectly inside a loop and will attach the currently created pdf, but $pdf->download() requires a return response -- my attempts to download without "return" get an error message, and "return" of course breaks the loop. 通过电子邮件发送发票很容易,因为'send'命令在循环中可以完美运行,并且可以附加当前创建的pdf,但是$ pdf-> download()需要返回响应-我尝试不带“ return”的下载就得到了错误消息,“返回”当然会中断循环。 Since my invoices are complicated, I'm using 由于我的发票很复杂,因此我正在使用
$pdf->loadView('invoices.show',$data);
to create the invoice using a view, rather than trying to write all of the html in the controller. 使用视图创建发票,而不是尝试在控制器中编写所有html。 I'm fine with combining all of the invoices into a single, multi-page pdf. 我可以将所有发票合并为一个多页pdf文件。 I understand the trick of using css style="page-break-after" to create different pages, but I can't figure out how to do that when I'm using loadView in order to send page-specific $data, rather than loadHtml -- loadView always results in only the last invoice being included. 我了解使用css style =“ page-break-after”创建不同页面的技巧,但是当我使用loadView来发送特定于页面的$ data而不是我时,我不知道该怎么做loadHtml-loadView总是只包含最后一张发票。
For example, controller: 例如,控制器:
public function invoices()
{
$pdf = App:make('dompdf');
for($i=0; $i<4; $i++) {
$data = array('i'=>$i);
}
$pdf->loadView('invoices.show',$data);
return $pdf->download("Invoice");
}
(I'm only including a single variable in the example $data array here; the real invoices obviously have a lot of $data variables.) (我在这里的示例$ data数组中只包含一个变量;实际的发票显然有很多$ data变量。)
View, wrapped in: 查看,包裹在:
<div style="page-break-after:always;">
Only returns, where the Invoice # = $i 仅在发票号= $ i的情况下返回
Invoice #3
Of course, putting $pdf->loadView inside of the loop does exactly the same thing. 当然,将$ pdf-> loadView放入循环中完全是一样的。 And since $pdf is not a string, trying something like $pdf .= ... within the loop doesn't work. 而且由于$ pdf不是字符串,因此在循环中尝试$ pdf。= ...之类的操作不起作用。
Thanks for any suggestions. 感谢您的任何建议。
The answer, as I discovered, is simply to put any required loop in the view, not in the controller. 正如我发现的,答案只是在视图中而不是在控制器中放置任何所需的循环。 That works fine. 很好
We need generate a PDF file with multiple pages, in laravel 5 and with blade template. 我们需要在laravel 5和刀片模板中生成具有多个页面的PDF文件。
In this post, we found a solution of how dompdf create document: convert multiple html to pdf 在这篇文章中,我们找到了dompdf如何创建文档的解决方案: 将多个html转换为pdf
My solution: I create 2 blade templates, container.blade and content.blade. 我的解决方案:我创建2个刀片模板,container.blade和content.blade。 First, i send data from my controller to content.blade into a foreach loop 首先,我将数据从控制器发送到content.blade进入foreach循环
loop...
view2 = View::make('contentview', compact('data'))->render();
$viewfinal = $viewfinal.$view2;
..endloop
$viewfinal is a concatenation of all html generating code of HEADS and BODYS from blade templates. $ viewfinal是刀片模板中html生成的HEADS和BODYS的所有代码的串联。 And finally: 最后:
$view = View::make('containerview', compact('viewfinal'))->render();
containerview.blade, only have this: containerview.blade,只有这个:
<!DOCTYPE html>
<html lang="en">
{!! $viewfinal !!}
</html>
And content.blade generate pages with "head" and "body", but NOT "html" tag , one for each PDF page: content.blade生成的页面带有“ head”和“ body”,但没有“ html”标记 ,每个PDF页面一个:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Multiples pages</title>
<link rel="stylesheet" type="text/css" href="assets/css/impresion1.css">
</head><body> ...........</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.