我正在使用DOMPDF生成发票。 1张发票是完美的。
但是我想在日期之间打印(从一段时间)所有发票 一切都很完美 但Dompdf只打印最后一张发票。
dompdf代码
require_once("dompdf_config.inc.php");
$date1 = "2008-01-01";
$date2 = "2009-01-01";
$user = "User_1";
$sql=mysql_query("SELECT * FROM invoices WHERE datum BETWEEN '$date1' AND '$date2' AND user='$user' ORDER BY nummer ");
while ($rows=mysql_fetch_array($sql))
{
$num=$rows[number];
$datum=$rows[data];
$pay=$rows[pay];
// Need to get the user info from other table
$Query2 = "SELECT * from user WHERE user='$user'";
while ($rows2=mysql_fetch_array($Result2))
{
$name = $rows2[name];
//ETC...
// Now we generate the html invoice
$html =
'<html><body>'.
//the invoice html
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
}
}
$dompdf->stream("Invoice.pdf");
我希望在1 pdf文件中获取所有发票(有时为5,有时为50)。 如何 ?
我在文件末尾关闭了我的时间,但没有帮助。
希望你能提供帮助。
答案 0 :(得分:0)
现在您编写代码的方式是渲染您的第一个文档,然后不对其执行任何操作。所以第二次通过while循环将覆盖第一遍。有几种方法可以实现您想要的,但最简单的方法是将所有HTML编译成单个文档,然后再渲染。
使用您的代码作为开始:
require_once("dompdf_config.inc.php");
$date1 = "2008-01-01";
$date2 = "2009-01-01";
$user = "User_1";
$invoices = array();
$sql=mysql_query("SELECT * FROM invoices WHERE datum BETWEEN '$date1' AND '$date2' AND user='$user' ORDER BY nummer ");
while ($rows=mysql_fetch_array($sql)) {
$num=$rows[number];
$datum=$rows[data];
$pay=$rows[pay];
// Need to get the user info from other table
$Query2 = "SELECT * from user WHERE user='$user'";
while ($rows2=mysql_fetch_array($Result2)) {
$name = $rows2[name];
//ETC...
// Force page break between invoices
if ( strlen( $html ) > 0 ) { $html .= '<div style="page-break-before: always;"></div>'; }
// Now we generate the html invoice
$invoices[] = '<!-- the html invoice -->';
}
}
$html = '<html><body>' . implode( '<div style="page-break-before: always;"></div>' , $invoices ) . '</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("Invoice.pdf");