大家好我目前正在使用蛋糕2.1,我试图在浏览器中将一个页面呈现为pdf,但我相信我没有正确加载引擎。我相信这是因为我没有在routes.php
中加载任何内容。
这是routes.php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
Router::mapResources(array('Invoices'));
/**
* Load the CakePHP default routes. Remove this if you do not want to use
* the built-in default routes.
*/
require CAKE . 'Config' . DS . 'routes.php';
这里是boostrap.php
<?php CakePlugin::load('DebugKit');
CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
CakePlugin::loadAll();
这是控制器中的功能
public function view($id = null) {
$this->set('title_for_layout', 'Invoices');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='adminpdf';
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf',
'download'=>true,
'binary'=>'C:\Program Files (x86)\wkhtmltopdf\wkhtmltopdf.exe'));
$this->pdfConfig = array('engine' => 'CakePdf.WkHtmlToPdf');
$this->Invoice->id = $id;
if (!$this->Invoice->exists()) {
throw new NotFoundException(__('Invalid invoice'));
}
$this->pdfConfig = array(
'orientation' => 'potrait',
'filename' => 'Invoice_' . $id
);
$this->set('invoice', $this->Invoice->read(null, $id));
//Retrieve Account Id of current User
$accountid=$this->Auth->user('account_id');
//Find all Invoices where $conditions are satisfied
$invoicedetails=$this->Invoice->find('first', array(
'conditions' => array('Invoice.id'=>$id)));
//prints fieldsInvoice details, including invoice and field information
$invoices=$this->FieldsInvoice->find('all',array(
'conditions'=>array(
'invoice_id'=>$id)));
$itemInvoice=$this->InvoicesItem->find('all',array('conditions'=>array('invoice_id'=>$id)));
//Set variables
$this->set('invoicedetails', $invoicedetails);
$this->set('invoice', $invoices);
$this->set('accountid', $accountid);
$this->set('itemInvoice', $itemInvoice);
}
我正在使用这种加载pdf的方法 - https://github.com/ceeram/CakePdf/
并且正在使用此引擎wkhtmltopdf
我已经坚持了几天,所以任何帮助都会非常感激。
答案 0 :(得分:0)
你使用WkHtmlToPdf你需要下载该引擎,因为它不是默认的。 点击这里:app / plugin / cakepdf / Vendor。 你看默认:dompdf,mpdf和tcpdf。
我使用DomPdf。 在你的bootstrap.php中加载引擎,如下所示:
Configure::write('CakePdf', array(
'engine' => 'CakePdf.DomPdf',
'pageSize'=>'A4',
'orientation' => 'landscape',
));
并在您的控制器中使用:
$this -> pdfConfig = array (
'orientation' => 'landscape',
'download' => true,
'filename' => 'Invoice_' . $id
);
您还可以查看:http://www.slideshare.net/jellehenkens/building-php-documents-with-cakepdf-cakefest-2012
祝你好运,希望这会有所帮助。