I am trying to generate a pdf with data collected in a form with PHP. The library 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');
?>
When generating the pdf, it shows me the PHP code as plain text. Is there any way to display the data inside the variables?
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.
To solve it you must add the values of the variables directly to the text string as follows:
I have used
htmlspecialchars()
to prevent names, addresses or phone numbers with characters that could be confused with HTML from breaking the layout.