https://github.com/dompdf/dompdf
generarPDF:
require_once 'dompdf-master/lib/html5lib/Parser.php';
require_once 'dompdf-master/src/Autoloader.php';
Dompdf\Autoloader::register();
use Dompdf\Dompdf;
$html = file_get_contents("invoice_header.html");
$table_head = true;
for($i=1;$i<100;$i++) {
if($table_head) {
$html .= file_get_contents("invoice_table_head.html"); // la cabecera de las columnas para cada pag
$table_head = false;
}
$html .= "
<tr>
<td class='service'>aaa ".$i."</td>
<td class='desc'>bbb ".$i."</td>
<td class='unit'>ccc ".$i."</td>
</tr>";
}
$html .= "
<tr>
<td style='background: #9c6c38;' class='service'>Total: ".$i." Registros</td>
<td style='background: #9c6c38;' class='desc'>-----</td>
<td style='background: #9c6c38;' class='unit'>----</td>
</tr>";
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream();
invoice_header.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" media="all" />
</head>
<body>
<header class="clearfix">
<div id="logo">
<img src="milogo-logo.png">
</div>
<h1>FACTURA</h1>
<div id="project">
<div><span>PROJECT</span> Website development</div>
<div><span>CLIENT</span> John Doe</div>
<div><span>ADDRESS</span> 796 Silver Harbour, TX 79273, US</div>
<div><span>EMAIL</span> <a href="mailto:john@example.com">john@example.com</a></div>
<div><span>DATE</span> August 17, 2015</div>
<div><span>DUE DATE</span> September 17, 2015</div>
</div>
</header>
<main>
invoice_table_head.html:
<table>
<thead>
<tr>
<th class="service">SERVICIOS</th>
<th class="desc">DESCRIPCIÓN</th>
<th>PRECIO</th>
</tr>
</thead>
<tbody>
Si se quiere añadir imágenes sería:
require_once 'dompdf-master/lib/html5lib/Parser.php';
require_once 'dompdf-master/src/Autoloader.php';
Dompdf\Autoloader::register();
use Dompdf\Dompdf;
use Dompdf\Options;
$dompdf = new Dompdf();
$options = new Options();
$options->set('isRemoteEnabled', TRUE);
$dompdf = new Dompdf($options);
$html = file_get_contents("../prueba.html");
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', '');
$dompdf->render();
$dompdf->stream();
Si se quiere cargar un fichero php no funciona con file_get_contents (pues eso te trae todo el código fuente y no lo ejecuta), la solución es:
function getRemoteFile($url, $timeout = 10) {
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
return ($file_contents) ? $file_contents : FALSE;
}
// hay que poner toda la ruta, no funciona solo poniendo pagina_html3.php
$html = getRemoteFile("http://localhost/pruebas/pdf/htmltopdf/pagina_html3.php");
No hay comentarios:
Publicar un comentario