How do you correctly output several pages in same PDF using TCPDF?

Viewed 195

I am new to using TCPDF. I have successfully outputted one page by itself, but when I try to print many in one page using a for loop that loops through the info of all invoices nothing happens. Nothing is outputted and I debug line by line and no error pops up. I feel like looking for a needle in a haystack.

I use $pdf->AddPage(); at the beginning of each iteration and: $pdf->writeHTML($html, true, false, true, false, ''); $pdf->lastPage(); before the iteration finishes. Finally, OUTSIDE the loop, I output everything with $pdf->Output.

Is this not the correct logic?

In case you need to have a look at the code:

require_once('tcpdf/tcpdf.php');


$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, '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, 2, ",", ".")  . '€' . '</td>
                                <td style="text-align: center; font-size: 10px;">' . $elDescuento . '€' . '</td>
                                <td style="text-align: center; font-size: 10px;">' . number_format($total_linea, 2, ",", ".")  . '€' . '</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, 2, ",", ".")  . '€' . '</td>
                                <td style="text-align: center;">' . number_format($iva_total, 2, ",", ".")  . '€' . '</td>
                                <td style="text-align: center;">' . number_format($precio_total, 2, ",", ".")  . '€' . '</td>
                                <td style="text-align: center;">' . $factura["descuento_factura"] . '%' . '</td>
                                <td style="text-align: center;">' . number_format($precio_final, 2, ",", ".") . '</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, true, false, true, false, '');
    $pdf->lastPage();
}

$pdf->Output('Factura CLIKEA.pdf', 'I');

Basically, what I'm trying to do is to print several invoices in the same PDF.

0 Answers
Related