cakephp和渲染pdf

时间:2012-10-19 03:22:11

标签: cakephp pdf plugins

大家好我试图为我的网站安装此插件。我目前正在运行蛋糕2.1,并且想要点击一个链接,它以PDF格式呈现发票,这样用户就可以随心所欲地使用它。目前,如果我转到http://localhost/pra/invoices/view/1.pdf它会加载一个html页面,如果我转到http://localhost/pra/invoices/view/pdf/1

这是我的发票控制器中的查看操作的代码

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';
    $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);

    }

这是该操作的视图文件

<div id = "content">
<h2>View Invoice</h2>
                <table id="data">


    <?php

        if($invoicedetails['Invoice']['scheduled']==1)
        {
            $status = 'Scheduled';  
            $fcol = 'Black';
            $bgcol = '#EBD8E8';
            $pay = NULL;
            $dispute = NULL;
        }
        else if($invoicedetails['Invoice']['paid']==1)
        {
            $status = 'Paid';
            $fcol = 'Black';
            $bgcol = '#B9FAEA';
            $pay = NULL;
            $dispute = NULL;
        }
        else if($invoicedetails['Invoice']['sender_id']==$accountid)
        {
            $status = 'Sent';
            $fcol = 'Black';
            $bgcol = '#F8FAC0';
            $pay = NULL;
            $dispute = NULL;
        }
        else if($invoicedetails['Invoice']['receiver_id']==$accountid)
        {
            $status = 'Received';
            $fcol = 'Black';
            $bgcol = '#FAB9B9';
            $pay = $this->Html->link('Pay', array('controller' => 'Invoices','action'=>'pay_admin',$invoicedetails['Invoice']['id'] )) ;
            $dispute = $this->Html->link('Dispute', array('controller' => 'Disputes','action'=>'add_admin',$invoicedetails['Invoice']['id'] ));
        }



    ?>                      
                <tr>
                <th>Sender: </th>
                <td><?php echo $invoicedetails['SenderAccount']['account_name'];?> </td>
                </tr>

                <tr>
                <th>Receiver: </th>
                <td><?php echo $invoicedetails['ReceiverAccount']['account_name'];?> </td>
                </tr>

                <tr>
                <th>Invoice ID: </th>
                <td><?php echo $invoicedetails['Invoice']['id'];?> </td>
                </tr>

                <tr>
                <th>Invoice Date: </th>
                <td><?php echo date('d.m.Y', strtotime($invoicedetails['Invoice']['created'])); ?></td>
                </tr>

                <tr>
                <th>Due Date: </th>
                <td><?php echo date('d.m.Y', strtotime($invoicedetails['Invoice']['expiry_date'])); ?></td>
                </tr>

                <tr>
                <th>Status: </th>
                <td bgcolor='<?php echo $bgcol ?>'><?php echo $status ;?> </td>
                </tr>

                <tr>
                <th>Actions: </th>
                <td><?php echo $pay ?> <?php echo  $dispute ?></td>
                </tr>               


                </table>
                <br>
                <table id="data">
                <tr>
                    <th>Item Name</th>
                    <th>Description</th>
                    <th>Price Per Unit</th>
                    <th>Quantity</th>
                </tr>
                <?php foreach($itemInvoice as $itemInvoices):?>
                <tr>
                <td><?php echo $itemInvoices['Item']['name']; ?></td>
                <td><?php echo $itemInvoices['Item']['description']; ?></td>
                <td>$<?php echo number_format($itemInvoices['Item']['price'], 2, '.', ','); ?></td>
                <td><?php echo $itemInvoices['InvoicesItem']['quantity']; ?></td>
                </tr>
    <?php endforeach; ?>
                </table>

                <br>

                <table id="data">
                <tr>
                    <th>Field Name</th>
                    <th>Entered Value</th>
                </tr>


        <?php foreach($invoice as $invoices):?>
<tr>
<td><?php echo $invoices['Field']['name']; ?> :</td>
<td><?php echo $invoices['FieldsInvoice']['entered_value']; ?></td>
</tr>
    <?php endforeach; ?>



            </table>
            <br><br>

这是我的app / config / bootstrap.php

中的代码
<?php CakePlugin::load('DebugKit');
    CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
     CakePlugin::loadAll();

这是layouts / pdf / adminpdf.ctp

中的布局文件
<?php echo $this->Html->docType('xhtml-trans'); ?>
<html>
<div id = "header" style="background-image:url(<?php echo $this->webroot; ?>img/BannerGradient.jpg);">
<head>

    <title> <?php echo $title_for_layout; ?></title>
    <?php echo $this->Html->css($stylesheet_used); ?>
    <?php echo $this->Html->script(array('jquery','modal.popup')); ?>


    <div id='logo'> <center>
    <?php echo $this->Html->image($image_used, array(
    "alt" => "eBox",
    'url' => array('controller' => 'eboxs', 'action' => 'home_admin'))) ?>
    </center></div> 
    <div id="welcome">

    <?php if($logged_in): ?>
         Logged in as <text6><?php echo $current_user['username']; ?></text6> <?php echo $this->Html->link('My Profile', array('controller'=>'Accounts', 'action'=>'index')); ?> | <?php echo $this->Html->link('Logout', array('controller'=>'users', 'action'=>'logout')); ?> 
    <?php else: ?>
    <?php echo $this->Html->link('Login', array('controller'=>'users', 'action'=>'login')); ?>  

    <?php endif; ?>

    </div>


</head>
</div>
<body>
        <?php echo $this->Session->flash(); ?>
        <?php echo $this->fetch('content'); ?>

</body>
</html>

我在此目录C:\Program Files (x86)\wkhtmltopdf

中也有WkHtmlToPdf引擎

我有两个问题

1)我在views / invoices / views.ctp和views / invoices / pdf / views.ctp中有两个视图,但它似乎从发票/ views.ctp中读取了一个是正确的吗?我的布局文件也发生了同样的事情。

2)我是否需要将蛋糕指向我的WkHtmlToPdf引擎,还是应该找到它?

1 个答案:

答案 0 :(得分:1)

回答Q1

检查插件所需的路由配置。我之前没有使用CakePdf插件,但是听到它的声音,它需要在&#34; a&#34;路由文件:

Router::parseExtensions('pdf');

可以找到更多信息here

回答Q2

您是否必须配置CakePdf插件才能知道本地安装引擎的位置?