Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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', '');
like image 971
Elorfin Avatar asked Jun 26 '26 00:06

Elorfin


2 Answers

For more refined results:

Outputting the final PDF:
When you’ve finished creating all required cells, images, links, text etc. you have to call the Output() method to actually get your hands on your dynamically created PDF. This can take no parameters in which case the PDF is sent to the browser, more commonly though, developers specify the filename and the destination of the generated PDF. The destination can be one of four values, these are:

I: send the file inline to the browser.  
D: send to the browser and force a file download with the name given by name.  
F: save to a local file with the name given by name.  
S: return the document as a string.

You can see my code sets the destination value as F:

$pdf->Output(”./pdfs/example.pdf”, “F”);

referenced from: http://www.akamarketing.com/blog/109-php-to-pdf-conversion-with-tcpdf.html

like image 105
Usman A. Chughtai Avatar answered Jun 27 '26 15:06

Usman A. Chughtai


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}

like image 21
Sarfraz Avatar answered Jun 27 '26 14:06

Sarfraz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!