сохранить файл просмотра codeigniter в виде файла pdf, используя mpdf

я хочу сохранить файл просмотра в формате pdf, используя библиотеку mpdf. моя функция контроллера выглядит так, как показано ниже. эта функция получит 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>

when i call the function generate_pdf() , it displays blank pdf file like this.

введите здесь описание изображенияБуду признателен за любую помощь. благодарю вас


person Sujan Shrestha    schedule 18.03.2015    source источник
comment
Попробуйте print_r($html);exit; в своей функции PDF и проверьте, что вы получаете внутри.   -  person Narendrasingh Sisodia    schedule 18.03.2015
comment
привет @narendra-sisodia я попробовал print_r($html);exit; и он показывает содержимое страницы просмотра.   -  person Sujan Shrestha    schedule 18.03.2015
comment
Показывает ли он, как вы хотели, вместе с деталями   -  person Narendrasingh Sisodia    schedule 18.03.2015
comment
ya @narendra-sisodia показывает подробности о print_r($html) ;, но не отображает в представлении PDF.   -  person Sujan Shrestha    schedule 18.03.2015


Ответы (2)


Попробуйте этот код...

$html = $this->load->view('агенты/счета/invoice_pdf',$data,true);

person Pawan Dongol    schedule 18.03.2015

Возможно, проблема в том, что расширение mbstring недоступно на вашем сервере. Включите это расширение и попробуйте.

person Venkatesan    schedule 01.04.2015