I am generating a pdf with the html2pdf library in php, however, I am experiencing two problems.
1.- The file can not be downloaded because it indicates that it has a virus. I do not have the slightest idea why this happens. I'm working on a totally new instance in a development environment I do not think the server really has any viruses. 2.- When starting the process, it consumes many resources. The system has queries to stored procedures and views and in use 10 users do not consume more than 4% of the processing. However, when generating a pdf it goes up to 40% or 50% or for example in my capture it went up to 70% of the processing CPU.
Can you give me some clues of what I'm doing wrong please? : (
First, screenshots of the problems.
My code
This file (My controller), is receiving a GET parameter, this parameter is the encrypted ID of the file that it has to build. So what I do is,
Driver
<?php
define('__h__', dirname(dirname(__FILE__)));
require_once(__h__.'../../../libs/html2pdf/vendor/autoload.php');
use Spipu\Html2Pdf\Html2Pdf;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Exception\ExceptionFormatter;
$path=__h__.'../../../host/';
if(isset($_GET['id'])){
$candidato = new candidatoController($_GET['id']);
$archivo = 'CV_'.$candidato->getNombreCompleto().'.pdf';
$validar = $candidato->getId();
if($validar!=NULL){
ob_start();
require_once 'views/pdftemplates/cv.php';
$html=ob_get_clean();
$html2pdf = new HTML2PDF('P','A4','es','true','UTF-8', array(15, 10, 15, 10));
$html2pdf->setDefaultFont('Arial');
$html2pdf->writeHTML($html);
ob_end_clean();
//$html2pdf->Output($archivo);
$html2pdf->Output($path.$archivo, 'F');
$salida =$path.$archivo;
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="'.$archivo.'"');
readfile($salida);
}else{
tools::alerta(2, "Ocurrió un problema al descargar el archivo.");
}
}else{
header('Location: '.__PATH__.'panel');
}
?>
Vista cv.php
<?php
$candidato = new candidatoController($_GET['id']);
$validar = $candidato->getId();
$imagen = $candidato->getImagen();
?>
<style type="text/css">
<!--
body {
font-family: Arial, Helvetica, sans-serif;
}
table.page_header img{max-width: 30%; }
table.page_footer {width: 100%; border: none; background-color: #00a651; border-top: solid 1mm #00a651; padding: 2mm}
h1 {color: #2a2a2a}
h2 {color: #2a2a2a}
h3 {color: ##2a2a2a}
div.standard
{
padding-left: 5mm;
}
#imgperfil{
width: 100%;
}
hr{
margin-top: 15px;
border: 0;
height: 1px;
background: #333;
background-image: linear-gradient(to right, #ccc, #333, #ccc);
}
p{
margin-top:2px; margin-bottom:2px;
}
-->
</style>
<page backtop="14mm" backbottom="14mm" style="font-size: 12pt">
<page_header>
<table class="page_header">
<tr>
<td style="width: 100%; text-align: left">
<a href="https://misitio.com.mx"><img src="https://misitio.com.mx/images/milogo.png"></a> <br>
Conectando excelentes profesionales con las mejores empresas.
</td>
</tr>
</table>
<hr>
</page_header>
<h4>Datos del candidato:</h4>
<table style="width: 100%;" align="left">
<tr>
<td style="width: 25%; text-align: left; ">
<img id="imgperfil" src="https://misitoi.com.mx/images/usuarios/<?php echo $imagen; ?>">
</td>
<td style="width: 75%; text-align: left; padding-left: 20px">
<h3 style="margin-top:0px; margin-bottom:0px;">
<?php echo $candidato->getNombreCompleto() ?>
</h3>
<h5 style="margin-top:0px; margin-bottom:0px;">
<?php echo $candidato->getProfesion() ?>
</h5>
<p style="margin-top:0px; margin-bottom:0px;"><b>Nacimiento</b>:
<?php echo $candidato->getFnacimiento() ?> <b>Sexo</b>: <?php echo $candidato->getSexo2() ?>
</p>
<p style="margin-top:0px; margin-bottom:0px;"><b>Dirección</b>:
<?php echo $candidato->getDireccion().', loc.'.$candidato->getNLocalidad().', Mpio. '.$candidato->getNMunicipio().', '.$candidato->getEstadoAbrev().' C.P. '. $candidato->getCP(); ?>
</p>
<p style="margin-top:0px; margin-bottom:0px;"><b>Teléfono</b>:
<?php echo $candidato->getTelefono() ?>
</p>
<p style="margin-top:0px; margin-bottom:0px;"><b>Email</b>:
<?php echo $candidato->getEmail() ?>
</p>
<p style="margin-top:0px; padding-bottom:40px;"><b>Clave única</b>:
<?php echo $candidato->getCurp() ?>
</p>
<br>
</td>
</tr>
</table>
<h3>Sobre mi:</h3>
<p style="text-align:justify">
<?php echo $candidato->getSobremi() ?>
</p>
<h3>Historia laboral:</h3>
<?php
$historia = $candidato->getHistoriaLaboral();
foreach ($historia as $key => $value) {
echo '<p><b>'.$value['titulo'].'</b> en '.$value['empresa'].'</p>';
echo '<p>'.$value['inicio'].' - '. $value['fin'].'</p>';
echo '<p>'.$value['descripcion'].'</p>';
echo '<br>';
}
?>
<page_footer>
<table class="page_footer">
<tr>
<td style="width: 50%; text-align: left">
<?php echo 'Curriculum vitae de '.$candidato->getNombreCompleto() ?>
</td>
<td style="width: 50%; text-align: right">
Página [[page_cu]]/[[page_nb]]
</td>
</tr>
</table>
</page_footer>
</page>
<page pageset="old">
<h3>Historial académico:</h3>
<?php
$historialaca = $candidato->getHistorialAcademico();
foreach ($historialaca as $key => $value) {
echo '<p><b>'.$value['curso'].'</b> en '.$value['institucion'].'</p>';
echo '<p><b>Periodo:</b> Del '.$value['inicio'].' al '.$value['fin'].'</p>';
echo '<p>'.$value['descripcion'].'</p>';
echo '<br>';
}
?>
<h3>Dominio del idioma:</h3>
<table style="width: 100%;" align="left">
<tr>
<?php
$idiomas = $candidato->getIdiomas();
//var_dump($idiomas);
foreach ($idiomas as $key => $value) {
echo '<td style="width: 25%; text-align: left; ">';
echo $value['idioma'];
$candidato->estrellaspdf($value['puntuacion']);
echo '</td>';
}
?>
</tr>
</table>
<h3>Información adicional:</h3>
<p><?php echo $candidato->getInfoAdicional() ?></p>
</page>
Thanks for your feedback.