Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save codeigniter view file as a pdf file using mpdf

i want to save my view file as a pdf using mpdf library. my controller function looks like as below. this function will get the invoice_no . it will check if the pdf file already exists on the server or not. if the pdf file already exists, the function will download that file automatically. if the file does not exist, it will generate new pdf file and display that file on browser and it will show an option to save that file as show below on the screenshot

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

      }
}

my view file looks like this

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.

enter image description here I would appreciate for any help. thank you

like image 343
Sujan Shrestha Avatar asked May 23 '26 14:05

Sujan Shrestha


1 Answers

Try this code...

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

like image 122
Pawan Dongol Avatar answered May 26 '26 16:05

Pawan Dongol