Control when a pdf has more than one page

0

The following code creates and exports to me a PDF with an invoice, the problem is that when I have more than one page I should jump to the next page, however what causes me is a collapse between the second table and my last table with the two tables. Could you give me a hand?

<?php
require_once('../../lib/dompdf/dompdf_config.inc.php');
include_once '../../lib/own/php/loadAllPhp.php';

setlocale(LC_TIME,"es_ES.UTF-8");

if(isset($_POST["factura"])){

    $factura = Doctrine_core::getTable('Facturas')->find($_POST['id']);
    $lineas=Doctrine_Query::create()->from('LineaFactura')->where('idFactura = ?',$factura->id)->orderBy('id')->execute();
    $IVA = str_pad(number_format(($factura->IVA * $factura->baseImponible) / 100,2),2, "0", STR_PAD_RIGHT);

    $tipo = "FACTURA";
    $num = $factura->factura_num;
	$metodosDePago = Doctrine_Core::getTable('MetodosDePago')->find($factura->metodo_pago);

}elseif(isset($_POST["albaran"])){

    $factura = Doctrine_core::getTable('Albaranes')->find($_POST['id']);
    $lineas=Doctrine_Query::create()->from('LineaAlbaran')->where('idAlbaran = ?',$factura->id)->orderBy('id')->execute();
    $IVA = str_pad(number_format(($factura->IVA * $factura->baseImponible) / 100,2),2, "0", STR_PAD_RIGHT);

    $tipo = "ALBARAN";
    $num = $factura->albaran_num;

    $metodosDePago = Doctrine_Core::getTable('MetodosDePago')->find($factura->metodoPago);

}elseif(isset($_POST["presupuesto"])){

    $factura = Doctrine_core::getTable('Presupuestos')->find($_POST['id']);
    $lineas=Doctrine_Query::create()->from('LineaPresupuesto')->where('idPresupuesto = ?',$factura->id)->orderBy('id')->execute();
    $IVA = str_pad(number_format(($factura->IVA * $factura->baseImponible) / 100,2),2, "0", STR_PAD_RIGHT);

    $tipo = "PRESUPUESTO";
    $num = $factura->presupuesto_num;

    $metodosDePago = Doctrine_Core::getTable('MetodosDePago')->find($factura->metodoPago);
}

$clientes=Doctrine_Core::getTable('Clientes')->find($factura->idCliente);
$margen = 17;
$codigo = "
    <html>
        <head>
            <title>" . $tipo . "</title>
            <style>
            .parent {
                padding: 10px;
                color: black;
                position: relative;
                width: 100%;
                clear:both;
            }
            .parent-table {
                padding-top: 20px;
                color: #0A416B;
                position: relative;
                width: 100%;
                clear:both;
            }
            .right h4 {
                text-align:left !important;
                margin-left: 5px !important;
                margin-top: 2px !important;
            }
            .left, .center, .right {
                float:left;
                width:32%;
                border: 1px solid #5883B0;
                margin-right: 20px;
                text-align: center;
                background:#dfedf4;

            }
            .left h4, .center h4{
                border-bottom: 1px solid #5883B0;
                background:#dfedf4;
                margin-top: 0px;
                padding: 5px;
            }
            .right p {
                text-align:left;
            }
            .left p, .center p, .right p {
                font-size:14px;
                padding: 0px 5px 5px 5px;
                margin-top: -5px;

            }
            table tr td, table tr th {
                text-align: left;
                color:black;
            }
            </style>
        </head>
        <body style='padding: 20px;'>
            <div style='width: 100%; position: relative; '>
                <div style='width: 75%; float: left;'>
                    <h2 style='border-bottom:3px solid #5883B0;'>LAS 3-J PUERTO REAL S.L.</h2>
                    <p> B-11466828<br>
                        Pol. Ind. El Portal, Conjunto Piscis, C/ Mauritania N&#176; 25<br>
                        11408 Jerez de la Frontera (Cádiz)<br>
                        Tfno: 956 14 28 38<br>
                        [email protected]<br>
                        www.lucicolor.com<br>
                    </p>
                </div>
                <div style='width: 25%; float: right; margin-top: 20px;'>
                    <img style='width: 100%;' src='" . $_SERVER['DOCUMENT_ROOT'] . "/intranet/logo_factura.png'/>
                </div>
            </div>
            <div class='parent'>
                <div class='left' style='width:20%;'>
                    <h4>" . $tipo . "</h4>
                    <p>N&#176;" . $num . "</p>
                </div>

                <div class='center' style='width:26%;'>
                    <h4>Fecha</h4>
                    <p>".strftime("%d de %B de %Y (%A)", strtotime($factura->fecha))."</p>
                </div>
                <div class='right' style='width:47.5%;'>
                    <h4>$clientes->codigo &nbsp;&nbsp;&nbsp; $clientes->nombreFac</h4>
                    <p>
                        NIF/CIF $clientes->cifFac &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Tfno: $clientes->telefonoFac<br>
                        $clientes->direccionFac<br>
                        $clientes->codigoPostalFac &nbsp;&nbsp;&nbsp;&nbsp; $clientes->ciudadFac<br>
                        $clientes->email
                    </p>
                </div>
            </div>";
            if(isset($_POST["factura"])){
                if($factura->albaran != NULL /*|| $factura->numPedido != NULL*/){
                    $codigo.="
                        <div style='text-align:center;
                            border: 1px solid #5883B0;
                            width:18%;
                            clear:left;
                            position:relative;
                            top:-25px;
                            background:#dfedf4;
                            border-radius:10px !important;
                            margin-right:20px;
                            padding-top:-10px;
                            padding-bottom:-10px;'>

                            <p style='font-size:14px; margin-left:10px; text-align:left'>";
                            $albaran_realnum = Doctrine_core::getTable('Albaranes')->find($factura->albaran);
                            //if($factura->albaran != NULL){
                                $codigo.="<strong>Alb&#225ran N&#186:</strong>$albaran_realnum->albaran_num<br>";
                            //}
                            /*if($factura->numPedido != NULL){
                                $codigo.="<strong>Pedido N&#186:</strong> $factura->numPedido<br>";
                            }*/
                         //$tieneAlbaran=5;
                    $codigo.="
                            </p>
                        </div>";
                }
            }
            $codigo .= "
            <div class='parent-table'>
                <table style='border-collapse: collapse;font-size:11; border:2px solid #5883B0; width: 100%;' cellpadding=0;'>
                    <tr style='background:#dfedf4;border-bottom:1px solid #5883B0;'>
                        <th style=' border-bottom:2px solid #5883B0'>C&oacute;digo</th>
                        <th style='padding:3px; border-bottom:2px solid #5883B0' align='left'>Descripci&oacute;n</th>
                        <th style='padding:3px; border-bottom:2px solid #5883B0'>Cant</th>
                        <th style='padding:3px; border-bottom:2px solid #5883B0'>Precio</th>
                        <th style='padding:3px; border-bottom:2px solid #5883B0'>Dto</th>
                        <th style='padding:3px; border-bottom:2px solid #5883B0'>Precio Unitario Dto. %</th>
                        <th style='padding:3px; border-bottom:2px solid #5883B0'>Importe</th>
                    </tr>";
                $contadorFilas = 0;
                foreach($lineas as $linea){
                    $contadorFilas++;
                    $margen = $margen - 1;
                    $articulo = Doctrine_Core::getTable('Articulos')->find($linea->idArticulo);
                    $precioUnitarioDescuento = str_pad(number_format($articulo->precio - ($articulo->precio * $linea->descuento / 100),2),2, "0", STR_PAD_RIGHT);
                    $importeTotal = str_pad(number_format($precioUnitarioDescuento * $linea->unidad,2),2, "0", STR_PAD_RIGHT);
                    $codigo.="
                    <tr>
                        <td style='border-right:2px solid #5883B0;border-bottom:2px solid #ccc;' align='center !important;'>$articulo->codigo</td>
                        <td style='border-right:2px solid #5883B0;border-bottom:2px solid #ccc; font-size:13px; '>$articulo->nombre</td>
                        <td style='border-right:2px solid #5883B0;border-bottom:2px solid #ccc; ' align='center !important'>$linea->unidad</td>
                        <td style='border-right:2px solid #5883B0;border-bottom:2px solid #ccc;' align='center !important'>$articulo->precio</td>
                        <td style='border-right:2px solid #5883B0;border-bottom:2px solid #ccc;' align='center !important'>".number_format($linea->descuento,0)." %</td>
                        <td style='border-right:2px solid #5883B0;border-bottom:2px solid #ccc;' align='center !important'>$precioUnitarioDescuento</td>
                        <td style='border-bottom:2px solid #ccc;' align='right !important'>$importeTotal</td>
                    </tr>";

                };

                $codigo.="
                    <tr>
                        <td width=6%; style='border-right:1px solid #5883B0;border-bottom:1px solid #ccc;'>&nbsp;</td>
                        <td width=28%; style='border-right:1px solid #5883B0;border-bottom:1px solid #ccc;'>&nbsp;</td>
                        <td width=6%; style='border-right:1px solid #5883B0;border-bottom:1px solid #ccc;'>&nbsp;</td>
                        <td width=6%; style='border-right:1px solid #5883B0;border-bottom:1px solid #ccc;'>&nbsp;</td>
                        <td width=6%; style='border-right:1px solid #5883B0;border-bottom:1px solid #ccc;'>&nbsp;</td>
                        <td width=6%; style='border-right:1px solid #5883B0;border-bottom:1px solid #ccc;'>&nbsp;</td>
                        <td width=6%;style='border-bottom:1px solid #ccc;'>"; for($i = 1; $i < $margen; $i++){
                            $codigo.= "<br>";  /*Saltos de linea para crear una tabla que llegue hasta el final de una pagina*/
                        };$codigo.="</td>
                    </tr>
                    <tr>
                        <td style='color:blue; font-size:13px;' colspan='7'>
                            $factura->pie<br><br>
                            En virtud de Ley de Protección de Datos Personales, le informamos que sus datos personales son confidenciales y que forman parte de un fichero de Gestión con la finalidad de gestionar la relación contractual, y remitirle información comercial, inclusive por medios electrónicos. Puede ejercer sus derechos de acceso, rectificación, cancelación y oposición dirigiéndose a Las 3-J Puerto Real S.L., Pol. Ind. El Portal, C/Mauritania Nº 25, 11408 Jerez de la Frontera (Cádiz), adjuntando una copia de su DNI o CIF.
                        </td>
                    </tr>
                </table>
                <table style='border-collapse:collapse;position:fixed;top:895px;left:20px;margin-top:15px;width:70%;background:#dfedf4;border:2px solid #5883B0;font-size:15px;'>
                    <tr>
                        <th style='padding:3px;border:2px solid #5883B0;'>Otros Datos</th>
                        <th style='padding:3px;border:2px solid #5883B0;'>Base imponible</th>";
                        if($factura->descuento > 0){
                            $codigo.="<th style='padding:3px;border:2px solid #5883B0;'>Otro Dto.</th>";
                        }
                    $codigo.="
                        <th style='padding:3px;border:2px solid #5883B0;'>IVA</th>
                    </tr>
                    <tr style='background:#dfedf4;  '>
                        <td style='border:1px solid #5883B0;' rowspan='2'>";
                        if($factura->numPedido){
                            $codigo.="<strong>Nº de Pedido: </strong>".$factura->numPedido."<br>";

                        }
                        $codigo.="Forma Pago: $clientes->metodoPago<br>";
                        /*if(isset($metodosDePago)){
                            $codigo.="Forma Pago: $metodosDePago->nombre<br>";*/

                            if($clientes->metodoPago == "Transferencia bancaria"){
                                $codigo.="N&#176; cuenta: ES59 2100 7438 1723 0013 5236 La Caixa<br>";
                            }

                            $codigo.="Vencimiento:$factura->vencimiento";
                        //}

                        $codigo.="</td>
                        <td align='right !important' style='border:2px solid #5883B0;'>$factura->baseImponible</td>";
                        if($factura->descuento > 0){
                            $codigo.="<td align='right !important' style='border:2px solid #5883B0;'>".$factura->descuento." %</td>";
                        }
                        $codigo.="<td align='right !important' style='border:2px solid #5883B0;'>".number_format($factura->IVA,0)." %</td>";
                    $codigo.="
                    </tr>
                    <tr style='background:#dfedf4;  '>
                        <td align='right !important' style='border:2px solid #5883B0;'><strong>$factura->baseImponible</strong></td>";
                        if($factura->descuento > 0){
                            $precioDto = (($factura->subTotal + ($factura->subTotal * 21 / 100)) - $factura->total)*(-1);
                            $codigo.="<td align='right !important' style='border:2px solid #5883B0;'><strong>".number_format($precioDto,2)." &#8364;</strong></td>";
                        }
                    $codigo.="
                        <td align='right !important'style='border:2px solid #5883B0;'>+ <strong>$IVA</strong></td>
                    </tr>
                </table>

                <table style='margin-top:15px;position:fixed;top:895px;left:565px;width:140px;padding-left:8px;background:#dfedf4;font-size:1.2em;border:2px solid #5883B0;'>
                    <tr>
                        <th>TOTAL</th>
                    </tr>
                    <tr>
                        <td align='center' style='padding-left:40px !important;'><strong>$factura->total</strong></td>
                    </tr>
                </table>
            </div>
        </body>
    </html>
";




$codigo=utf8_decode($codigo);
/*var_dump($codigo);
die;*/
$dompdf=new DOMPDF();
$dompdf->load_html($codigo);
ini_set("memory_limit","256M");
$dompdf->render();

$nombrec=str_replace(".", "", $clientes->nombre);
$facturac=str_replace(".", "", $factura->fecha);

//$dompdf->stream($tipo." - ".$nombrec."-".$facturac.".pdf");

$dompdf->stream("mipdf.pdf", array("Attachment" => 0));


?>
    
asked by Miguel C 18.04.2018 в 11:18
source

1 answer

0

When I dynamically generated pdf invoices with a php class, what I did was to control the number of lines that fit inside the space that I have delimited, then every time I print a line, I control if it is still within the range, and if it passes, I generate a new page with its headers, etc ... and I continue printing with the corresponding iterator

    
answered by 18.04.2018 / 11:49
source