我试图使用cakepdf从视图生成PDF,但它没有按预期工作。这是我第一次使用这个插件。
这是我的设置
控制器动作
$this->pdfConfig = array(
'orientation' => 'portrait',
'filename' => $report['reg_no'],
'permissions' => array(
'print','screen_readers','copy_contents'
)
);
$cakePdf = new CakePdf(); //create an instance of the cakePdf class
$cakePdf->template('pdf_view','default'); //define view to use and optional layout
$pdf = $cakePdf->output();
$pdf = $cakePdf->write(APP . 'webroot' .DS .'files'. DS . 'report.pdf');
//$this->set('tcpdf',$tcpdf);
$this->set('report',$report);
$this->render('/Pdf/pdf_view');
我pdf_view 。ctp的代码如下:
相当长<style>
tr .title {
font-weight: bold;
padding: 5px;
width:100px;
}
tr .text {
padding-left: 10px;
}
<div>
<?php
if($report['school']['logo'] == NULL){echo $this->Html->image('/logo_small.png',array('width'=>'100px','height'=>'100px'));
}else {
echo $this->Html->image($report['school']['logo'],array('width'=>'100px','height'=>'100px'));
}
?>
<p>
<font size="5"><b><?php echo $report['school']['name']; ?></b></font><br />
<b><?php echo $report['school']['motto']; ?></b><br />
<b><?php echo $report['school']['address']; ?></b>
</p>
<p align="center"><b><font size="4">Report Card</font></b></p>
<div>
<table align="left">
<tr>
<td class="title">Admission_no</td>
<td class="text"><?php echo $report['reg_no']; ?></td>
</tr>
<tr>
<td class="title">Form</td>
<td class="text"><?php echo $report['form']; ?></td>
</tr>
<tr>
<td class="title">Position</td>
<td class="text"><?php echo $report['rank']; ?></td>
</tr>
</table>
</div>
<div>
<table align="left">
<tr>
<td class="title">Name</td>
<td class="text"><?php echo $report['name']; ?></td>
</tr>
<tr>
<td class="title">Stream</td>
<td class="text"><?php if($report['stream'] == NULL){
echo "<N/A>";
}else {
echo $report['stream'];
} ?></td>
</tr>
<tr>
<td class="title">Out Of</td>
<td class="text"><?php echo $report['outof']; ?></td>
</tr>
</table>
</div>
<br /><br />
<div style="clear:left"><hr />
<!--<table>
<thead>
<tr>
<th>Subject</th>
<th>Score</th>
<th>Grade</th>
<th>Points</th>
<th>Remarks</th>
</tr>
</thead>
<tbody>
<?php /*foreach ($report['subjects'] as $key => $value): ?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $value['score']; ?></td>
<td><?php echo $value['grade']; ?></td>
<td><?php echo $value['points']; ?></td>
</tr>
<?php
$totPoints += $value['points'];
endforeach; ?>
<tr>
<td>Total</td>
<td><?php echo $report['total']; ?></td>
<td><?php echo $totPoints;*/ ?></td>
</tr>
</tbody>
</table>-->
</div>
<br /><hr /><br />
<p>
<font size="4"><b>Class Teacher Remarks</b></font><br /><br />
<font size="4"><b>Principal Remarks</b></font><br /><br />
<font size="4"><b>Fees Balance</b></font><br />
<font size="4"><b>Closing Date</b></font>
<font size="4"><b>Opening Date</b></font>
</p>
问题:
1。表的内联样式没有应用。
2。生成的PDF中充满了关于pdf_view中未定义变量:report的通知。Ctp(但我用了
)$this->set('report',$report)
in my controller action)
我的图像不显示在pdf中,即使在dompdf_config.inc.php中设置了这个
定义("DOMPDF_ENABLE_REMOTE",真的);
4。关于注释掉的标签如果我取消注释,会得到error
Error: Call to undefined method DOMText::getAttribute()
File: /home/r2d2/web-php/sms/app/Plugin/CakePdf/Vendor/dompdf/include /cellmap.cls.php
Line: 400
问题:为什么我得到未定义的变量报告,图像未加载,样式未应用和#4上面..
解决了我所有的问题。可能对其他第一次使用插件的用户有所帮助:
1。意识到不能像对视图那样为PDF呈现传递变量。而不是在stackoverflow上的其他答案:this worked
$this->viewVars(array("report",$report));
2。通过添加fullbase数组选项解决:
echo $this->Html->image($report['school']['logo'],array('width'=>'100px','height'=>'100px','fullBase'=>true));
3。移除tbody标签。从文档中发现,我的大多数CSS样式都不支持