问题描述
我是使用 TCPDF 的新手。我已经成功地单独输出了一页,但是当我尝试使用循环遍历所有发票信息的 for 循环在一页中打印许多时,没有任何反应。没有输出任何内容,我逐行调试并且没有弹出错误。我感觉就像大海捞针。
我在每次迭代开始时使用 $pdf->AddPage();
并且:
$pdf->writeHTML($html,true,false,'');
$pdf->lastPage();
在迭代完成之前。最后,在循环之外,我用 $pdf->Output
输出所有内容。
这不是正确的逻辑吗?
如果您需要查看代码:
require_once('tcpdf/tcpdf.PHP');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION,PDF_UNIT,PDF_PAGE_FORMAT,'UTF-8',false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('Factura CLIKEA');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF,PDF,example,test,guide');
$pdf->SetHeaderData(PDF_HEADER_logo,PDF_HEADER_logo_WIDTH,PDF_HEADER_TITLE,PDF_HEADER_STRING);
$pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN,'',PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(array(PDF_FONT_NAME_DATA,PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT,PDF_MARGIN_TOP,PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutopageBreak(TRUE,PDF_MARGIN_BottOM);
$pdf->setimageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('dejavusans',10);
$count = count($array_facturas);
for($i=0; $i<$count; $i++){
$pdf->AddPage();
$factura = $array_facturas[$i];
$lineas_factura = $array_lineas_facturas[$i];
$lineas_de_factura_cadena = "";
$precio_total = 0;
$iva_total = 0;
foreach ($lineas_factura as $linea) {
$elPrecio = $linea["precio"];
$laCantidad = $linea["cantidad"];
$elPvp = $elPrecio * $laCantidad;
$elDescuento = $linea["descuento"];
$total_linea = $elPvp - ($elPvp * $elDescuento / 100);
$iva_total += $total_linea * $linea["iva"] / 100;
$precio_total += $total_linea;
$lineas_de_factura_cadena .= '<tr>
<td style="text-align: center; font-size: 10px;"><img src="' . $linea["ruta_imagen"] . '"></td>
<td style="text-align: center; font-size: 10px;">' . $linea["nombre"] . '</td>
<td style="text-align: center; font-size: 10px;">' . number_format($elPrecio,2,",".") . '€' . '</td>
<td style="text-align: center; font-size: 10px;">' . $laCantidad . '</td>
<td style="text-align: center; font-size: 10px;">' . $linea["iva"] . '%' . '</td>
<td style="text-align: center; font-size: 10px;">' . number_format($elPvp,".") . '€' . '</td>
<td style="text-align: center; font-size: 10px;">' . $elDescuento . '€' . '</td>
<td style="text-align: center; font-size: 10px;">' . number_format($total_linea,".") . '€' . '</td></tr>';
}
$subtotal = $precio_total - $iva_total;
$precio_final = $precio_total - ($precio_total * $factura["descuento_factura"] / 100);
$html = '<div><div style="text-align: center">
<img src="assets/images/logo.png" alt="CLIKEA" height="70px" width="150px">
</div>
<h4><strong>Núm. de factura: ' . $factura["cod_factura"] . '</strong></h4>
<h5>Fecha factura: ' . date("d-m-Y",strtotime($factura["fecha"])) . '</h5>
<div>
<table>
<tr>
<td>' . $factura["razon_social"] . '</td>
</tr>
<tr>
<td>' . $factura["cif_dni"] . '</td>
</tr>
<tr>
<td>' . $factura["domicilio_social"] . '</td>
</tr>
<tr>
<td>' . $factura["ciudad"] . '</td>
</tr>
</table>
</div>
<table style="margin-bottom: 15px !important;">
<thead class="thead-dark">
<tr>
<th style="text-align: center; font-weight: bold; font-size: 10px; background-color: #ccad9f;" scope="col">IMAGEN</th>
<th style="text-align: center; font-weight: bold; font-size: 10px; background-color: #ccad9f;" scope="col">NOMBRE ARTÍCULO</th>
<th style="text-align: center; font-weight: bold; font-size: 10px; background-color: #ccad9f;" scope="col">PRECIO UNITARIO</th>
<th style="text-align: center; font-weight: bold; font-size: 10px; background-color: #ccad9f;" scope="col">CANTIDAD</th>
<th style="text-align: center; font-weight: bold; font-size: 10px; background-color: #ccad9f;" scope="col">IVA</th>
<th style="text-align: center; font-weight: bold; font-size: 10px; background-color: #ccad9f;" scope="col">PVP</th>
<th style="text-align: center; font-weight: bold; font-size: 10px; background-color: #ccad9f;" scope="col">DESCUENTO</th>
<th style="text-align: center; font-weight: bold; font-size: 10px; background-color: #ccad9f;" scope="col">TOTAL</th>
</tr>
</thead>
<tbody id="tbody_factura"><tr><td class="height: 5px;"></td></tr>' . $lineas_de_factura_cadena . '
<tr><td class="height: 5px;"></td></tr></tbody>
</table>
<div style="width: 1500px; text-align: center;">
<table style=" margin: 30px auto;">
<thead class="thead-dark">
<tr >
<th style="text-align: center; font-weight:bold; background-color: #ccad9f;" scope="col">SUBTOTAL</th>
<th style="text-align: center; font-weight:bold; background-color: #ccad9f;" scope="col">IVA TOTAL</th>
<th style="text-align: center; font-weight:bold; background-color: #ccad9f;" scope="col">PRECIO TOTAL</th>
<th style="text-align: center; font-weight:bold; background-color: #ccad9f;" scope="col">DESCUENTO GLOBAL</th>
<th style="text-align: center; font-weight:bold; background-color: #ccad9f;" scope="col">PRECIO FINAL</th>
</tr>
</thead>
<tbody>
<tr><td class="height: 2px;"></td></tr>
<tr>
<td style="text-align: center;">' . number_format($subtotal,".") . '€' . '</td>
<td style="text-align: center;">' . number_format($iva_total,".") . '€' . '</td>
<td style="text-align: center;">' . number_format($precio_total,".") . '€' . '</td>
<td style="text-align: center;">' . $factura["descuento_factura"] . '%' . '</td>
<td style="text-align: center;">' . number_format($precio_final,".") . '</td>
</tr>
</tbody>
</table>
</div>
<div style="margin-top: 20px; text-align: center">
<h4>Gracias por confiar en nosotros.</h4>
<p>Nos mantenemos a su entera disposición por teléfono en el <strong>965437656</strong> y a través del correo <strong>hola@clikea.com</strong> para ayudarle en todo lo posible.</p>
</div></div>';
$pdf->writeHTML($html,'');
$pdf->lastPage();
}
$pdf->Output('Factura CLIKEA.pdf','I');
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)