PHP is not displayed when generating PDF

3

I am trying to generate a pdf with data collected in a form with PHP. The library that I am using is mPDF

<?php 

	$cliente = $_POST['cliente'];
	$direccion = $_POST['direccion'];
	$telefono = $_POST['telefono'];

	require_once('pdf/mpdf.php');

	$html = '
    <div class="datos">
      <p> <?php echo $cliente; ?> </p>
      <p> <?php echo $direccion; ?> </p>
      <p> <?php echo $telefono; ?> </p>
    </div>';

	$mpdf = new mPDF('c', 'A4');
	$css = file_get_contents('css/pdf.css');
	$mpdf->writeHTML($css, 1);
	$mpdf->debug = true;
	$mpdf->writeHTML($html);
	$mpdf->Output('reporte.pdf', 'I');

 ?>

At the time of generating the pdf, it shows me the PHP code as plain text. Is there any way to show the data inside the variables?

    
asked by Tefef 26.09.2018 в 12:36
source

1 answer

2

What happens to you is that you are sending text to the document whose content is PHP code, so that will be what you see inside it.

To solve it you must add the values of the variables directly to the text string in the following way:

<?php 

    $cliente = $_POST['cliente'];
    $direccion = $_POST['direccion'];
    $telefono = $_POST['telefono'];

    require_once('pdf/mpdf.php');

    $html = '
    <div class="datos">
      <p>' . htmlspecialchars($cliente) . '</p>
      <p>' . htmlspecialchars($direccion) . '</p>
      <p>' . htmlspecialchars($telefono) . '</p>
    </div>';

    $mpdf = new mPDF('c', 'A4');
    $css = file_get_contents('css/pdf.css');
    $mpdf->writeHTML($css, 1);
    $mpdf->debug = true;
    $mpdf->writeHTML($html);
    $mpdf->Output('reporte.pdf', 'I');

I've used htmlspecialchars() to prevent names, addresses or phones with characters that can be confused with HTML from breaking the design.

    
answered by 26.09.2018 / 12:52
source