使用mpdf将codeigniter视图文件保存为pdf文件

时间:2015-03-18 06:07:23

标签: codeigniter mpdf

我想使用mpdf库将我的视图文件保存为pdf。我的控制器功能如下所示。此函数将获取 invoice_no 。它将检查服务器上是否已存在pdf文件。如果pdf文件已存在,该函数将自动下载该文件。如果文件不存在,它将生成新的pdf文件并在浏览器上显示该文件,它将显示保存该文件的选项,如下面的截图所示

function generate_pdf()
{
     $this->load->helper('download');
    if(isset($_GET['action'])&& isset($_GET['id']))
    {
        $invoice_no = $_GET['id'];
        $download = true;
    }
    else
    {
        $download = false;
    }
    $agent_id = $this->session->userdata('agent_id');
    $agent_email = $this->session->userdata('agent_email');

    $file = FCPATH.'invoice pdf files/'.$agent_email.'/Invoice '. $invoice_no.'.pdf';

    if(file_exists($file) && ( $download==true))
    {
        $data = file_get_contents($file);
        $name = 'Invoice '. $invoice_no.'.pdf';
        force_download($name, $data);
    }
    else
    { 

    $agent_id = $this->session->userdata('agent_id');
    $agent_email = $this->session->userdata('agent_email');
    $file = FCPATH.'invoice pdf files/'.$agent_email.'/Invoice '. $invoice_no.'.pdf';
    $invoice_details = $this->agents_model->get_result_array('tbl_portal_payment_history',array('agent_id'=>$agent_id, 'invoice_no'=>$invoice_no));
    $transaction_details = $this->agents_model->get_result_array('tbl_portal_invoice_number',array('agent_id'=>$agent_id, 'invoice_no'=>$invoice_no));
    $invoice_date = $transaction_details[0]['invoice_registration_date'];
    $invoice_date = strtotime($invoice_date);
    $invoice_date = date('D, F j\<\s\u\p\>S\<\/\s\u\p\>, Y',$invoice_date); 
    $data['invoice_date'] = $invoice_date;
    $date = date("h:i:sa");
    $date = strtotime($date);
    $data['today_date'] = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>, Y',$date);
    $agent_details = $this->agents_model->get_row_array('tbl_agents', array('id'=>$agent_id));
    $data['agent_name'] = $agent_details['first_name'].' '.$agent_details['last_name'];

    $data['address2'] = ($agent_details['address2']!='')? $agent_details['address2'].', ':'';
    $data['city'] = ($agent_details['city']!='')? $agent_details['city'].', ':'';
    $data['zip_code']= ($agent_details['zip_code']!='')? $agent_details['zip_code']:'';
    $data['address'] =  $address2.$city.$zip_code;

    $data['unpaid'] = base_url('images/unpaid-1.PNG');
    $data['logo'] = base_url('images/footer-logo.png'); 
    $data['agent_details'] =$agent_details;
    $data['invoice_details'] =$invoice_details;
    $data['transaction_details'] =$transaction_details;
    $data['invoice_no'] =$invoice_no;

    $html = $this->load->view('agents/invoices/invoice_pdf_view',$data);

    $main_folder   =   base_url('invoice pdf files/');
        $sub_folder   =   base_url('invoice pdf files/'.$agent_email.'/');
        $filename = 'Invoice '.$invoice_no;
        if(!is_dir($main_folder)){
            mkdir('invoice pdf files');
            mkdir('invoice pdf files/'.$agent_email.'/');
        }   

        if(!is_dir($sub_folder)){
            mkdir('invoice pdf files/'.$agent_email.'/');
        }

        $pdfFilePath = FCPATH.'invoice pdf files/'.$agent_email.'/'.$filename.'.pdf';
        $this->load->library('pdf');
        $pdf = $this->pdf->load();$pdf->WriteHTML($html);
        $pdf->Output($pdfFilePath, 'F'); 
        $pdf->Output();

      }
}

我的视图文件看起来像这样

invoice_pdf_view.php

<html>
<head>
<title>Pdf file</title>
</head>
<body>

<div class="logo" style="position: absolute;  float: left; right: 0; top: 0; margin: 0;">
<img style="height:150px;" src="<?php echo $unpaid;?>">
</div>

<div class="logo" style="float: left; width: 54%; margin-top:15px; margin-bottom: 0pt; ">
<img src="<?php echo $logo;?>">
</div>


<div class="address" style="text-align: right;">
    111 Great Neck Road<br/>
    Suite 215<br/>
    Great Neck, NY 11021<br/>   
</div>

<div class="" style="margin-top:30 opt; background-color:#EEE;">
    <div class="title" style="font-weight: bold; font-size: 150%;">Invoice #<?php echo $invoice_no; ?></div>
    Invoice Date: <?php echo $invoice_date;?><br/>
    Due Date: <?php echo $invoice_date;?><br/>
</div>

<div class="" style="margin-top:30 opt; ">
    <div class="" style="font-weight: bold; font-size: 150%;">Invoiced To</div>
    VORO NYC<br/>
    ATTN: <?php echo $agent_name; ?><br/>
    <?php echo $agent_details['address1'];?><br/>
    <?php echo $address;?> <br/>
    <?php echo $agent_details['country'];?><br/>
</div>
<br/>
<table border="1" cellPadding="9" cellspacing="0" >
    <tbody>
        <tr>
            <th style="width:150%">Description</th>
            <th>Total</th>
        </tr>
<?php 
$sub_total = 0;
if(isset($invoice_details)){
foreach ($invoice_details as $key => $value) {
    $product_detals = get_row_array('tbl_voro_store_products', array('id'=>$value['prod_id']));
    $total = $value["price"]*$value['prod_qty'];               
    $sub_total = $sub_total + $total;

    $sub_total_sum = number_format((float)$sub_total, 2, '.', '');

   ?><tr>
   <td><?php echo $value['prod_qty'].' x '.$product_detals["prod_name"];?></td>
   <td><?php echo number_format((float)$total, 2, '.', '');?> USD</td>

   </tr>
    <?php 
    }
}

 $credit = '0';
 $credit = number_format((float)$credit, 2, '.', '');           
 $total= $sub_total - $credit;
 $total = number_format((float)$total, 2, '.', '');

 ?>
        <tr>
            <td class="table-description" align="right">Sub Total</td>
            <td class="table-description"><?php echo $sub_total_sum;?>USD</td>
        </tr>

        <tr>
            <td class="table-description" align="right">Credit</td>
            <td class="table-description"><?php echo $credit;?>USD</td>
        </tr>

        <tr>
            <td class="table-description" align="right">Total</td>
            <td class="table-description"><?php echo $total;?>USD</td>
        </tr>
    </tbody>
</table>

<h3>Transactions</h3>


<table border="1" cellPadding="9" cellspacing="0" width="100%">
    <tbody>
        <tr>
            <th>Transaction Date</th>
            <th>Gateway</th>
            <th>Transaction ID</th>
            <th width="20%">Amount</th>
        </tr>
        <?php 
    if(isset($transaction_details)){
    foreach ($transaction_details as $key => $value) {
       ?>
       <tr>
       <td><?php echo $value["invoice_payment_date"];?></td>
       <td>VISA, MASTERCARD, DISCOVER</td>
       <td><?php echo $value["transaction_id"];?></td>
       <td><?php echo number_format($value['cart_total'],2);?></td>
       </tr>

    <?php }
        }
    else{
        ?>
        <tr>
            <td colspan="4" align="center">No Related Transactions Found</td>
        </tr>
   <?php  } ?>

        <tr>
            <td colspan="3" class="table-description" align="right">Total</td>
            <td class="table-description"><?php echo $sub_total_sum;?>USD</td>
        </tr>
    </tbody>
</table>
<br/>

<div class="center">
    PDF Generated on <?php echo $today_date;?>
</div>

</body>

当我调用函数 generate_pdf()时,它会显示如下的空白pdf文件。

enter image description here 我将不胜感激任何帮助。谢谢

2 个答案:

答案 0 :(得分:1)

试试这段代码......

$ html = $ this-&gt; load-&gt; view('agents / invoices / invoice_pdf',$ data,true);

答案 1 :(得分:0)

可能是您的服务器上没有mbstring扩展的问题。 启用该扩展程序并试用。