【问题标题】:cakephp and loading enginecakephp 和加载引擎
【发布时间】:2012-10-13 23:28:47
【问题描述】:

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

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

【问题讨论】:

    标签: cakephp pdf routes


    【解决方案1】:

    您使用 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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-12
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      相关资源
      最近更新 更多