开发者

TCPDF, php variable values do not appear on the saved pdf file

开发者 https://www.devze.com 2023-03-07 15:03 出处:网络
I created a pdf using TCPDF and I filled it with some php variables and the current date using heredoc syntax

I created a pdf using TCPDF and I filled it with some php variables and the current date using heredoc syntax

Everything is fine when the pdf is generated and viewed in the browser, but on the saved pdf file the php variable values are not shown, only the date...

I declare my variables at the top like so:

$name = $_POST['name'];
$score = $_POST['percentage'];
$ku_number = $_POST['ku-number'];
$date = Date('d - m - Y');

Only $date is shown on the saved pdf.

Any suggestion?

Thank you

Edit: code to pass the variables to TCPDF

// Print a text
$html = <<<EOF
<!-- EXAMPLE OF CSS STYLE -->
<style>
table{
 text-align:center;
 color:#000;
}
</style>
<table id="name" cellpadding="0">
<tr>
<td width="10" height="80">&nbsp;</td>
<td width="620">&nbsp;</td>
<td width="10">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td width="620">$name</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="10">&nbsp;</td>
<td>&nbsp;</td>
<td width="10">&nbsp;</td>
</tr>
</table>

<table id="score" cellpadding="0" >
<tr>
<td width="10" height="180">&nbsp;</td>
<td width="620">&nbsp;</td>
<td width="10">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td width="620">$score</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="10" height="207">&nbsp;</td>
<td>&nbsp;</td>
<td width="10">&nbsp;</td>
</tr>
</table>

<table id="ku-number" cellpadding="0" >
<tr height="2">
<td width="50" height="2"></td>
<td width="620" height="2"></td>
<td width="10" height="2"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td width="620" height="20"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="50">&nbsp;</td>
<td>$ku_number</td>
<td >&nbsp;</td>开发者_如何学C
</tr>
</table>

<table id="date" cellpadding="0" >
<tr height="2">
<td width="50" height="2"></td>
<td width="620" height="2"></td>
<td width="10" height="2"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td width="420" height="20">$date</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="50">&nbsp;</td>
<td></td>
<td >&nbsp;</td>
</tr>
</table>

EOF;
$pdf->writeHTML($html, true, false, true, false, '');


//Close and output PDF document
$pdf->Output('certificate.pdf', 'I');


I had the same problem. I found a solution by experimenting things myself mentioned as follows:

Please use concatenation to break $html string into parts. This will surely solve the problem. e.g. I used something 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' ;

I hope this will work.


Using $_SESSION to store the variables before outputting the PDF solved the problem

Credits go to Yuri Stuken


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:this

Have a bAlaNCeD Life !

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号