Welcome Guest, Not a member yet? Register   Sign In
domPDF and Image issues
#1

[eluser]KaBaDaBrA[/eluser]
Hi there,

I've been using domPDF with the Code Igniter plugin provided by Derek Allard...really works great! What I have done is created a model that has the plugin and pdf creation.

The application that I am developing sends mutiple invoices with one click. When it loops through the clients it sends an invoiceID to the model and creates the necessary PDF documents. After the creation of the PDF documents it sends all the emails with the associated PDF attached. The emailing part works great!

The problem is that the header image only displays on the first invoice selected and the rest of the invoices header images are blank?! Is there any way to unset a variable or clear memory or something when a PDF document is created that will perhaps solve this problem?

Here's my code...

MODEL:

Code:
class Invoices_model extends Model {

    function Invoices_model()
    {
        // Call the Model constructor
        parent::Model();
        
        $this->load->plugin('to_pdf');
    }
    
    function create_invoice($invoiceID, $invoiceQuery, $file_name)
    {    
        
        //GET INVOICE SETTINGS AND LAYOUT
        $sql = $this->db->query("SELECT * FROM settings");
        foreach($sql->result() as $row):
            $data['invoice_header'] = $row->invoice_header;
            $data['invoice_terms'] = $row->invoice_terms;
            $data['invoice_picture'] = $row->invoice_picture;
        endforeach;
        $sql->free_result();
        
        //GET QUERY
        $data['query'] = $invoiceQuery;
        
        //DO PDF CREATION
        $html = $this->load->view('invoices/generate_pdf', $data, true);
        pdf_create($html, $file_name, false);
    }
}

INVOICE LOOPING

Code:
//EMAIL MULTIPLE INVOICES WITH PDF ATTACHED
if($handler_options == '4') {    
        
    $file_name = "";
    $emailTo = "";
    $emailAddress = "";
    $invoiceData = "";
    $checkPayment = "";
    
    //LOAD EMAIL LIBRARY
    $this->load->library('email');
    //LOAD INVOICES MODEL
    $this->load->model('Invoices_model');
    
    //LOOP THROUGH EACH PASSED CHECKBOX VALUE AND CREATE THE PDF
    foreach($list as $invoiceID) :
        
        $startDate = $this->uri->segment(4);
        $endDate = $this->uri->segment(5);
        $section = $this->uri->segment(6);
        
        $this->email->clear(TRUE);
                
        $query = $this->db->query('SELECT * FROM invoices, clients WHERE clients.id = invoices.clientID AND invoices.id = '.$invoiceID);
        
        //GET CLIENT INFORMATION
        foreach($query->result() as $row):
            $showPersonal = "";
            if($row->company == "") {
                $showPersonal = $row->firstname.' '.$row->lastname;
            } else {
                $showPersonal = $row->company;
            }
            $file_name = 'Invoice '.$this->session->userdata('logged_invoice_prefix').$row->invoiceID.' - '.$showPersonal;
            $emailAddress = $row->email;
            $emailTo = $row->firstname.' '.$row->lastname;
            $invoiceData = $this->session->userdata('logged_invoice_prefix').$row->invoiceID;
            $checkPayment = $row->payment_option;
        endforeach;

        //USE INVOICES MODEL AND CREATE INVOICE
    $this->Invoices_model->create_invoice($invoiceID, $query, $file_name);
                    
    $query->free_result();
                
        $config['protocol'] = 'mail';
        $config['charset'] = 'iso-8859-1';
        $config['mailtype'] = 'html';
        $config['wordwrap'] = TRUE;
        
        $this->email->initialize($config);
    
        $this->email->from($this->session->userdata('logged_invoice_email'), $this->session->userdata('logged_businessname').' -  Accounts Department');
        $this->email->to($emailAddress);
        
        $this->email->subject($file_name);
        $style = "<style>";
        $style .= "body {";
        $style .= "font-family: Calibri, Arial";
        $style .= "}";
        $style .= "</style>";
        
        if ($checkPayment == "debit") {
            $this->email->message("test debit");
        } else {
            $this->email->message("test cash / eft");
        }
    
        $this->email->attach('./tmp/'.$file_name.'.pdf');
        
        if ($this->email->send())
        {
            unlink("./tmp/".$file_name.".pdf");
            
            $sql = array('emailed' => '1');
            $this->db->where('id', $invoiceID);
            $this->db->update('invoices', $sql);
            
            $this->session->set_flashdata('success','<p align="center" class="error"><strong>Multiple invoices have been <span class="red_header">successfully</span> emailed! Memory Used: '.$this->benchmark->memory_usage().'</strong></p><p>&nbsp;</p>');

        } else {
            unlink("./tmp/".$file_name.".pdf");
            
            $sql = array('emailed' => '2');
            $this->db->where('id', $invoiceID);
            $this->db->update('invoices', $sql);
            
            $this->session->set_flashdata('success','<p align="center" class="error"><strong>Multiple invoices have <span class="red_header">failed</span> to send!</strong></p><p>&nbsp;</p>');

        }
        $query->free_result();
    endforeach;
                    
    redirect('invoices/index/'.$startDate.'/'.$endDate.'/payInvoiceMethod/'.$section);
}
#2

[eluser]Rubain[/eluser]
Hello,

I'm having the same problem (also with invoices sent from within a loop). Did you find a workaround?

EDIT: I did found a workaround: I only need 2 two images for every pdf I create, each time the same. So I just commented out the lines within the function clear() from the file /dompdf/include/image_cache.cls.php . This does the trick for me.

Files are stored in /tmp/, do I actually need to delete them manually after the loop, or does a server clean his temp-files regularly?

EDIT: files seem to be gone, so the server cleans out /tmp/ automatically.

Ruben Verhack
#3

[eluser]emorling[/eluser]
The SOLUTION for including images in domPDF.

It doesn't matter that the images display in your browser.

You must include the server path to the image.

In my case it was this (img resides in the same folder as my system folder):

<img src="./img/pdf/logo.png">




Theme © iAndrew 2016 - Forum software by © MyBB