2

大家好,我目前正在使用 cake 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

我已经坚持了几天,所以任何帮助将不胜感激。

4

1 回答 1

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

祝你好运,我希望这会有所帮助。

于 2013-04-11T09:58:10.533 回答