简体   繁体   中英

cakephp and rendering a pdf

hi all I am trying to install this plug-in for my site. I am currently running cake 2.1 and want to click a link and it render the invoice in a pdf so the user can do whatever they want with it. Currently if I goto http://localhost/pra/invoices/view/1.pdf it loads out a html page same with if i goto http://localhost/pra/invoices/view/pdf/1

here is the code for my view action in my invoices controller

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

    }

here is the view file for that action

<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>

here is the code in my app/config/bootstrap.php

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

here is the layout file in 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>

i also have the WkHtmlToPdf engine in this directory C:\\Program Files (x86)\\wkhtmltopdf

i have 2 questions

1) I have two views one in views/invoices/views.ctp and views/invoices/pdf/views.ctp but it seems to read the one from invoices/views.ctp is that correct? the same thing is happening with my layout files.

2) do I need to direct cake to my WkHtmlToPdf engine or should it find it?

Answer for Q1

Check what the routing configuration you need for your plugin. I haven't used the CakePdf plugin before but by the sounds of it, it needs to have the following in "a" routing file:

Router::parseExtensions('pdf');

More information can be found here .

Answer for Q2

Do you have to configure the CakePdf plugin to know where your local install of the engine is?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM