Search code examples
phpsymfony1tcpdf

Write variable PHP with TCPDF


I have to generate a PDF with TCPDF, my information come from my database, so I have to write PHP variables. I know how to do.

But I want to write CSS + xHTML (as an example I have seen of TCPDF site) using :

$html = <<<EOF

<style>
  <!-- My CSS -->
</style>

  <!-- My xHTML -->

EOF;

$pdf->writeHTML('$html', '');

I have errors when I try to open the PDF. I have read I must escape the '$' with '\'. After this, I have no error, but the content of my variables doesn't appear in my PDF.

EDIT :

@Sarfraz: I don't see how can I do this if I do this, it doesn't work ?

$html = <<<EOF

    <style>
      <!-- My CSS -->
    </style>

      <!-- My xHTML -->
      <span><?php echo $myVar; ?></span>

    EOF;

    $pdf->writeHTML('$html', '');

Solution

  • Make sure that there is no space and indentation before EOF; otherwise you will get error.

    Also if you have any variables in heredoc syntax escape put them in {} like {$somevar}