Libreria html2pdf.js
Publicado por Sergio (112 intervenciones) el 14/03/2022 20:52:39
Buenas noches. Estoy intentando crear un pdf con la librería html2pdf.js con php y html5. El problema viene cuando hace el salto de página que ya no respeta los márgenes. A veces, incluso, corta la frase por la mitad, pero literal. Es como si fuera una imagen y la corta. Me gustaría tambien saber cuando tiene que hacer el salto de página sin que ocurra ésto. Gracias. Os pongo el código:
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
192
193
194
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if(isset($_GET['id_pre'])){
// conectare la base de datos
include "../clases/conexion.php";
require "../clases/scripts.php";
if($_SESSION['conectar'] == "conectar"){
$obj= new conectar();
$conexion = $obj->conexion();
}else{
$obj= new conectar();
$conexion = $obj->conexion_1();
}
}
//Necesitamos recuperar los datos del presupuesto
$id_pre = $_GET['id_pre'];
$sql_pre = "SELECT * FROM presupuestos WHERE id_presupuesto = '".$id_pre."'";
$result_pre = mysqli_query($conexion, $sql_pre);
$fila_pre = mysqli_fetch_assoc($result_pre);
$numero_presupuesto = $fila_pre['numero_presupuesto'];
$nombre_archivo = "Presupuesto " .$numero_presupuesto.".pdf";
//Necesitamos los datos del cliente
$sql_cli = "SELECT * FROM clientes WHERE id_cliente = '".$fila_pre['id_cliente_pre']."'";
$result_cli = mysqli_query($conexion, $sql_cli);
$fila_cli = mysqli_fetch_assoc($result_cli);
//También los datos de la empresa
$sql_empresa = "SELECT * FROM empresa";
$resul_empresa = mysqli_query($conexion, $sql_empresa);
$fila_empresa = mysqli_fetch_assoc($resul_empresa);
$ruta_logo ="../img/";
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Imprimir presupuesto</title>
</head>
<body>
<!-- Comenzamos diseñando el prespuesto -->
<div class="container-fluid" id="pdf_presupuesto" style="font-size: 12px;">
<div class="row ml-4">
<img style="width: 300px; height: 100px;" src="<?php echo $ruta_logo.$fila_empresa['logo'];?>">
</div>
<div class="row ml-4 mr-2">
<div class="col-12">
<table class="table mt-3" style="border-style: solid;">
<tr>
<td>
<span><?php echo $fila_empresa['nombre'];?></span>
<br>
<span>Cif: <?php echo $fila_empresa['cif'];?></span>
<br>
<span><?php echo $fila_empresa['direccion'];?></span>
<br>
<span><?php echo $fila_empresa['tel'];?></span>
<br>
<span><?php echo $fila_empresa['email'];?></span>
</td>
<td class="text-right">
<span><?php echo $fila_cli['nombre'];?></span>
<br>
<span><?php echo $fila_cli['direccion'];?></span>
<br>
<span><?php echo $fila_cli['telefono'];?></span>
</td>
</tr>
</table>
</div>
</div>
<!-- Aquí va el número de presupuesto y la fecha -->
<div class="row ml-4 text-center">
<div class="col-6 ml-4" style="border-style: solid; text-align:center;">
<span>Número presupuesto: <span class="font-weight-bold"><?php echo $fila_pre['numero_presupuesto'];?></span></span>
<br>
<span>Fecha Presupuesto: <span class="font-weight-bold"><?php echo $fila_pre['fecha_presupuesto'];?></span></span>
</div>
</div>
<!-- Primero los encabezados -->
<div class="row mt-2 ml-4 mr-4">
<div class="col-12">
<table class="table table-bordered">
<thead style="background-color: blue; color: white; font-weight: bold;">
<tr>
<th class="col-md-10">Descripción</th>
<th class="text-center">Precio</th>
</tr>
</thead>
<tbody>
<?php
$sql_detalles = "SELECT * FROM detalles_presupuesto WHERE id_presupuesto = '".$fila_pre['id_presupuesto']."'";
$resul_detalles = mysqli_query($conexion, $sql_detalles);
while($fila_detalle = mysqli_fetch_assoc($resul_detalles)){
?>
<tr>
<td class="col-md-10"><pre style="font-family: Arial, Helvetica, sans-serif; line-height: 20px;"><?php echo $fila_detalle['descripcion'];?></pre></td>
<td class="text-center"><?php echo number_format($fila_detalle['precio'], 2, ',', '.').'€'?></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
<tr>
<td class="text-right font-weight-bold">Total presupuesto</td>
<td class="text-danger font-weight-bold"><?php echo number_format($fila_pre['total_presupuesto'], 2,',', '.').'€';?></td>
</tr>
<tr>
<td class="text-right font-weight-bold">Iva</td>
<td class="text-danger font-weight-bold"><?php echo $fila_pre['iva'].'%';?></td>
</tr>
<tr>
<td class="text-right font-weight-bold">Total iva</td>
<td class="text-danger font-weight-bold"><?php echo number_format($fila_pre['total_iva'], 2,',', '.').'€';?></td>
</tr>
<tr>
<td class="text-right font-weight-bold">Total con Iva</td>
<td class="text-danger font-weight-bold"><?php echo number_format($fila_pre['total_con_iva'], 2,',', '.').'€';?></td>
</tr>
</tfoot>
</table>
</div>
</div>
<div class="row">
<div class="col-4 ml-4">
<table class="table table-bordered">
<tr>
<td>
Forma de pago:
</td>
</tr>
<tr>
<td>
50% al empezar la obra
</td>
</tr>
<tr>
<td>
40% a mitad de obra
</td>
</tr>
<tr>
<td>
10% al finalizar la obra
</td>
</tr>
</table>
</div>
</div>
</div>
</body>
<script>
$(document).ready(function(){
var presupuesto_pdf = document.getElementById('pdf_presupuesto').innerHTML;
var opt = {
margin: 1, //margin for pages
filename: '<?php echo $nombre_archivo;?>',
image: { type: 'png'},
html2canvas: { scale: 1, logging: true, dpi: 192, letterRendering: true },
jsPDF: { unit: 'mm', format: 'A4', orientation: 'portrait' },
pagebreak: { mode: ['avoid-all', 'css', 'legacy'] } //It determines how HTML elements should be split.
};
html2pdf().from(presupuesto_pdf).set(opt).toPdf().get('pdf').then(function(pdf) {
var totalPages = pdf.internal.getNumberOfPages();
for (i = 1; i <= totalPages; i++) {
pdf.setPage(i);
pdf.setFontSize(10);
pdf.text('Page ' + i + ' of ' + totalPages, (pdf.internal.pageSize.getWidth() / 2.3), (pdf.internal.pageSize.getHeight() - 0.8));
}
}).save();
});
</script>
</html>
Valora esta pregunta


0