我刚刚下载了dompdf-0.6.1
,想在我的CakePHP 2.0.5应用程序中使用它。
我在routes.php中添加了Router::parseExtensions('json', 'pdf');
并在应用中创建了default.ctp - >查看 - >布局。
<?php
require_once(APP . 'Vendor' . DS . 'dompdf' . DS . 'dompdf_config.inc.php');
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->set_paper = 'A4';
$dompdf->load_html(utf8_decode($content_for_layout), Configure::read('App.encoding'));
$dompdf->render();
echo $dompdf->output();
我想允许用户将损益表视为.pdf文件 - 可以打印。损益表采用startDate和endDate的参数进行生成。
以下文件是income_statement.ctp。我需要做什么才能让用户通过按浏览器页面上的链接以.pdf的形式查看语句?
.......some code......
<div style="text-align: center">
<h1><?php echo $this->Session->read("Business.name"); ?><br>Income Statement</h1>
<h3>
For period starting <?php echo $startDate; ?> to <?php echo $endDate; ?>
</h3>
<table align="center" width="400px;">
<tr>
<th style="text-align: left;" colspan="2">Revenue</td>
</tr>
........code.........
<td style="text-align: left;">
<?php
$space = " ";
for ($i = 0; $i < 12; $i++)
echo $space;
?>
<b>Total Operating Expenses</b>
</td>
<td style="text-align: right;"><b><?php echo number_format($totalExpenses + $saleDiscount, 2); ?></b></td>
</tr>
<tr>
<th style="text-align: right"><?php echo $totalDescription; ?></th>
<th style="text-align: right"><?php echo number_format($net, 2); ?></th>
</tr>
</table>
....Link for view as pdf.....
</div>
答案 0 :(得分:1)
您可以将pdf保存为实际文件,然后链接到它们(链接到site.com/pdf/filename.pdf):
public function action() {
$view = new View(null, false);
$view->set(compact('variable1', 'variable2'));
$view->viewPath = 'Folder';
$output = $view->render('income_statement', 'layout');
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->set_paper = 'A4';
$dompdf->load_html(utf8_decode($output), Configure::read('App.encoding'));
$dompdf->render();
file_put_contents(APP . 'webroot' . DS . 'pdf' . DS .'filename.pdf');
}
或者,您可以回显$ dompdf从渲染中返回的内容,只需更改页面的标题(链接到site.com/controller/action.pdf):
public function action() {
$this->layout = $this->autoRender = false;
$this->response->header(array('Content-type' => 'application/pdf'));
//Code for generating pdf data similar to above
echo $dompdf->render();
}