PHP - Libreria fpdf

 
Vista:
sin imagen de perfil
Val: 54
Ha aumentado 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

Libreria fpdf

Publicado por Sergio (112 intervenciones) el 16/08/2021 21:52:51
Buenas noches. Estoy haciendo una aplicación que requiere hacer pdfs. Es un sistema de presupuestos y facturas. El tema del presupuesto lo hace perfecto pero tengo dos multicell al final que si se llega al final de página me los corta. Me gustaría que no lo hiciese. Os pongo el código. Gracias.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
    //require "fpdf/fpdf.php";
    include "conexion.php";
    include "pdf_mc_table.php";
 
 
    $obj= new conectar();
	$conexion = $obj->conexion();
 
    // Consulta detalles presupuesto
    $idpre = $_POST['idpre'];
    $sql_pre = "SELECT * FROM presupuestos WHERE id_presupuesto= '".$idpre."'";
    $resultado_pre = mysqli_query($conexion, $sql_pre);
    $mostrar_pre = mysqli_fetch_array($resultado_pre);
 
    // Necesito los detalles del presupuesto
    $sql_detalles = "SELECT * FROM detalles_presupuesto WHERE id_presupuesto = '".$idpre."'";
    $resultado_detalles = mysqli_query($conexion, $sql_detalles);
 
    //Los datos de la empresa
    $sql_empresa = "SELECT * FROM empresa";
    $resultado_empresa = mysqli_query($conexion, $sql_empresa);
    $mostrar_empresa = mysqli_fetch_array($resultado_empresa);
    // Fin datos de la empresa
 
    // Necesito también los datos del cliente
    $sql_cliente = "SELECT * FROM clientes WHERE id_cliente = '".$mostrar_pre['id_cliente_pre']."'";
    $resultado_cliente = mysqli_query($conexion, $sql_cliente);
    $mostrar_cliente = mysqli_fetch_array($resultado_cliente);
    $numero_presupuesto = $mostrar_pre['numero_presupuesto'];
    $nombre_archivo = "Presupuesto " .$numero_presupuesto.".pdf";
    $ruta_archivo = "img/".$mostrar_empresa['logo'];
 
    $pdf=new PDF_MC_Table();
    $pdf = new PDF_MC_Table('P', 'mm', 'A4');
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',14);
    $textypos = 5;
    $pdf->setY(12);
    $pdf->setX(10);
    // Agregamos los datos de la empresa
    // Logo
    $pdf->Image($ruta_archivo,10,8,100,30);
    $pdf->SetFont('Arial','',10);
    $pdf->setY(40);$pdf->setX(10);
    $pdf->Cell(5,$textypos, utf8_decode($mostrar_empresa['direccion']));
    $pdf->setY(45);$pdf->setX(10);
    $pdf->Cell(5,$textypos, utf8_decode($mostrar_empresa['cod_postal']) .' - '.$mostrar_empresa['poblacion']);
    $pdf->setY(50);$pdf->setX(10);
    $pdf->Cell(5,$textypos, utf8_decode("Teléfono: " .$mostrar_empresa['tel']));
    $pdf->setY(55);$pdf->setX(10);
    $pdf->Cell(5,$textypos, utf8_decode("Email: " .$mostrar_empresa['email']));
 
    // Ponemos el número de presupuesto
    $pdf->SetFont('Arial','B',12);
    $pdf->setY(60);$pdf->setX(10);
    $pdf->Ln(5);
    $pdf->Cell(5,$textypos, utf8_decode("Nº Presupuesto: " .$numero_presupuesto));
    // Ponemos la fecha del presupuesto
    $pdf->Ln(8);
    $pdf->Cell(5, $textypos, utf8_decode(("Fecha Presupuesto: " .date_format( new datetime($mostrar_pre['fecha_presupuesto']), 'd/m/Y'))));
 
    // Agregamos los datos del cliente
    $pdf->SetFont('Arial','',10);
    $pdf->setY(40);$pdf->setX(145);
    $pdf->Cell(5,$textypos, utf8_decode($mostrar_cliente['nombre']));
    $pdf->setY(45);$pdf->setX(145);
    $pdf->Cell(5,$textypos, utf8_decode($mostrar_cliente['direccion']));
    $pdf->setY(50);$pdf->setX(145);
    $pdf->Cell(5,$textypos, utf8_decode($mostrar_cliente['telefono']));
    $pdf->setY(55);$pdf->setX(145);
    $pdf->Cell(5,$textypos, utf8_decode($mostrar_cliente['email']));
 
    $pdf->Ln(25);
 
    $pdf->SetWidths(Array(115, 20, 25, 25));
    $pdf->SetLineHeight(8);
    //// Array de Cabecera
    $pdf->SetFillColor(100, 150, 255);
    $pdf->SetFont('Arial','B',12);
    $pdf->Cell(115, 8, utf8_decode("Descripción"),1,0,'',true);
    $pdf->Cell(20, 8, "Cantidad",1,0,'',true);
    $pdf->Cell(25, 8, "Precio",1,0,'',true);
    $pdf->Cell(25, 8, "Subtotal",1,0,'',true);
    $pdf->Ln();
    $pdf->SetLineHeight(6);
    $pdf->SetFont('Arial','',10);
    $pdf->SetFillColor(255, 255, 255);
    while($row = mysqli_fetch_array($resultado_detalles)) $array_detalles[] = $row;
    $json = json_encode($array_detalles);
    $data = json_decode($json, true);
    define('EURO',chr(128));
    foreach($data as $item){
        $pdf->Row(Array(
            utf8_decode($item['descripcion']),
            $item['cantidad'],
            number_format($item['precio'],2, ',', '.').' '.EURO,
            number_format($item['subtotal'],2, ',', '.').' '.EURO
 
        ));
    }
    //Los totales
    $pdf->SetFont('Arial','B',12);
    $pdf->Ln();
    $y = $pdf->GetY();
    $y_para_guardar = $pdf->GetY();
    $pdf->setY($y);$pdf->setX(135);
    $pdf->Cell(5,$textypos, "Subtotal: ".number_format($mostrar_pre['total_presupuesto'],2, ',', '.').' '.EURO);
    $y = $y + 5;
    $pdf->setY($y);$pdf->setX(135);
    $pdf->Cell(5,$textypos, "Iva %: ".$mostrar_empresa['iva']);
    $y = $y + 5;
    $pdf->setY($y);$pdf->setX(135);
    $pdf->Cell(5,$textypos, "Total Iva: ".number_format($mostrar_pre['total_iva'],2, ',', '.').' '.EURO);
    $y = $y + 5;
    $pdf->setY($y);$pdf->setX(135);
    $pdf->Cell(5,$textypos, "Total Presupuesto: ".number_format($mostrar_pre['total_con_iva'],2, ',', '.').' '.EURO );
 
    // Prueba
    $pdf->Rect(10, $y_para_guardar-2, 55, 25);
    $pdf->setY($y_para_guardar);$pdf->setX(10);
    $pdf->SetFont('Arial','BI',12);
    $pdf->Cell(5,$textypos, "Forma de pago:");
    $y_para_guardar = $y_para_guardar + 5;
    $pdf->setY($y_para_guardar);$pdf->setX(10);
    $pdf->Cell(5,$textypos, "50% al empezar la obra");
    $y_para_guardar = $y_para_guardar + 5;
    $pdf->setY($y_para_guardar);$pdf->setX(10);
    $pdf->Cell(5,$textypos, "40% a mitad de obra");
    $y_para_guardar = $y_para_guardar + 5;
    $pdf->setY($y_para_guardar);$pdf->setX(10);
    $pdf->Cell(5,$textypos, "10% al finalizar la obra");
    // Fin de la prueba
 
    $pdf->Ln(10);
    $pdf->SetDrawColor(255, 0, 0);
    $pdf->SetLineWidth(1);
    $pdf->Line(10, $pdf->GetY(), 195, $pdf->GetY());  //Set the line
    $pdf->Ln(5);
    if($pdf->GetY()>240){
        $pdf->AddPage();
        $pdf->SetX(20);
        $pdf->SetY(20);
        $pdf->SetDrawColor(0, 0, 0);
        $pdf->SetLineWidth(0);
        $texto1 =  utf8_decode("TODOS LOS IMPREVISTOS QUE NO ESTEN CONTEMPLADOS EN EL PRESUPUESTO, SE HARA UN NUEVO PRESUPUESTO Y SE ABONARÁN ANTES DE REALIZAR LOS TRABAJOS EN DICHA OBRA");
        $pdf->Multicell(170, 6, $texto1, 1, 'C', 0);
        $texto2 = utf8_decode("TODOS LOS MATERIALES DE LA OBRA/REFORMA SON PROPIEDAD DE SERGIO CON N.I.F. No: B-11222333 HASTA QUE NO SE ABONE EL TOTAL DE LA FACTURA");
        $pdf->Ln(3);
        $pdf->SetX(20);
        $pdf->Multicell(170, 6, $texto2, 1, 'C', 0);
        $pdf->Ln(6);
        $y = $pdf->GetY();
        $pdf->setY($y);$pdf->setX(10);
        $pdf->Cell(5,$textypos, "Conforme el cliente");
        $pdf->setY($y);$pdf->setX(130);
        $pdf->Cell(5,$textypos, "Reciba un cordial saludo:");
        $y = $y + 5;
        $pdf->setY($y);$pdf->setX(130);
        $pdf->Cell(5,$textypos, "Sergio");
    }else{
    $pdf->SetX(20);
    $pdf->SetDrawColor(0, 0, 0);
    $pdf->SetLineWidth(0);
    $texto1 =  utf8_decode("TODOS LOS IMPREVISTOS QUE NO ESTEN CONTEMPLADOS EN EL PRESUPUESTO, SE HARA UN NUEVO PRESUPUESTO Y SE ABONARÁN ANTES DE REALIZAR LOS TRABAJOS EN DICHA OBRA");
    $pdf->Multicell(170, 6, $texto1, 1, 'C', 0);
    $texto2 = utf8_decode("TODOS LOS MATERIALES DE LA OBRA/REFORMA SON PROPIEDAD DE SERGIO CON N.I.F. No: B-11222333 HASTA QUE NO SE ABONE EL TOTAL DE LA FACTURA");
    $pdf->Ln(3);
    $pdf->SetX(20);
    $pdf->Multicell(170, 6, $texto2, 1, 'C', 0);
    $pdf->Ln(6);
    $y = $pdf->GetY();
    $pdf->setY($y);$pdf->setX(10);
    $pdf->Cell(5,$textypos, "Conforme el cliente");
    $pdf->setY($y);$pdf->setX(130);
    $pdf->Cell(5,$textypos, "Reciba un cordial saludo:");
    $y = $y + 5;
    $pdf->setY($y);$pdf->setX(130);
    $pdf->Cell(5,$textypos, "Sergio");
    }
    $pdfContent= $pdf->Output('S', true);
 
    $salida= array(
        'name' => $nombre_archivo,
        'file' => "data:application/pdf;base64," . base64_encode($pdfContent)
    );
    header('Content-type: application/json');
    echo json_encode($salida);
 
 
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder