문제

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', '');
도움이 되었습니까?

해결책

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}

다른 팁

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

I had same problem. I got solution by experimenting things myself mentioned as follows:

Please use concatenation to break $html string into parts. This will solve the problem surely. e.g. I used like this:

$html = 'HTML CONTENT BREAKS HERE'.$variable_name.'HTML CONTENT CONTINUES HERE';

Normally, most developers will use PHP variable within $html value,

$html = 'HTML CONTENT echo php variable HTML CONTENT';

Instead of EOF tags , make use of single(') or double quote(") that will also help.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top