Welcome Guest, Not a member yet? Register   Sign In
[RESOLVED] Mulitiple PDF's with fpdf lib and CI in one controller.
#1

[eluser]Sheldon[/eluser]
Hi,

I am using CI and FPDF with FPDFI, http://www.fpdfi.org

I have it set up and working pretty well, and have been using theses to generate a lot of PDF's in the last couple of years, but now I have a different issue,

I am trying to generate a bunch of separate PDF's inside a for loop.

The Basic's of my controller are;
Code:
<?php

    // inside my CI controller
        
        
        function monthly() {
            
//            Load PDF Library - now loading in loop to try reset buffer
//            $this->load->library('pdf');
//            Load and initialise CI Email library
            $this->load->library('email');
            $this->email->initialize(array('mailtype' => 'html'));

//            start loop - approx 15 records            
            $monthlies = $this->invoice->getMonthlies();
            if($monthlies) {
                foreach($monthlies as $monthly) {
                    
//                    Load CI/FPDF library
                    $this->load->library('pdf');
                    
//                    Create PDF ( I have remove unnesscary code that pulls data for PDF. )
//                    Make a random filename
                    $filename   = md5(time() * rand(100, 1000)) .'.pdf';
                    $this->load->view('pdf/invoice', array('filename' => $filename));
                    
//                    validate customer email and sent PDF.
                    if(valid_email($email)) {

                        $this->email->from($this->config->item('admin_mail'));
                        $this->email->to($email);
                        $this->email->reply_to($this->config->item('admin_mail'));

                        $this->email->subject('Invoice #'. $invoice_id);
                        $this->email->message($this->load->view('admin/email', array('customer' => $customer, 'invoice' => $invoice), TRUE));
                    
//                        Attach my PDF, send and clear email + all attachemnts
                        $this->email->attach($filename);
                        $this->email->send();
                        $this->email->clear(TRUE);
                    
                    }
//                end loop
                }
                
            }
            
//            finished
            redirect();
        }
    
    }

This script run's and emails each record as expected, but it is concatinating my PDF's in to one PDF, EG; 1st loop, correct single page PDF, 2nd loop, 1st page is the previous record, second is a blank PDF page ?
15th record, as the first PDF correct, and a random amount of blank pages.

My PDF View generates a single PDF fine, I have tried to reset thte PDF buffer after my output;
Code:
$this->pdf->Output($filename, 'F');
    $this->pdf->closeParsers();
    $this->pdf->Close();
    unset($this->pdf->buffer);
    $this->pdf->buffer = '';
    $this->pdf->buffer = NULL;

My CI PDF library includes the fpdfi library like so;
Code:
<?php

    if(!defined('BASEPATH')) exit('No direct script access allowed');

    require_once('fpdi.php');

    class Pdf extends fpdi {
    
        var $fontsize = 8;
    
        function Pdf() {
            parent::fpdi('P','mm','A4');
            $this->AddFont('HelveticaNeue-Light','','HelveticaNeue-Light.php');
            $this->AddFont('HelveticaNeue-Light','B','HelveticaNeue-Bold.php');
        }
    
        function Header() {
        
            $ci =& get_instance();
            $this->SetY(40);
        
        }

    
        function Footer() {
        }
    
    }
#2

[eluser]Aken[/eluser]
I haven't played around with FPDF in a while, but I'm pretty certain you'll either need to delete the PDF file that's generated before creating a new one with the same name, or you'll need to use a different name for each PDF file created.
#3

[eluser]Sheldon[/eluser]
[quote author="Aken" date="1261386346"]I haven't played around with FPDF in a while, but I'm pretty certain you'll either need to delete the PDF file that's generated before creating a new one with the same name, or you'll need to use a different name for each PDF file created.[/quote]

Thanks Aken,

Sorry that was an error in my post, In this instance, the filename is generated by the mySQL auto incrementing ID, and a rand md5() has in the file name, is is never the same.
#4

[eluser]Sheldon[/eluser]
Here is the working code,

CI is great for including a sinlge library instance, but there is no way ( That I know of ) to unset a library.


Code:
<?php

    // inside my CI controller
        
        
        function monthly() {
            
//            Load PDF Library - the original PHP way
             require_once('libraries/pdf.php');
//            Load and initialise CI Email library
            $this->load->library('email');
            $this->email->initialize(array('mailtype' => 'html'));

//            start loop - approx 15 records            
            $monthlies = $this->invoice->getMonthlies();
            if($monthlies) {
                foreach($monthlies as $monthly) {
                    
//                    Load CI/FPDF library
                    $this->load->library('pdf');
                    
//                    Create PDF ( I have remove unnesscary code that pulls data for PDF. )
//                    Make a random filename
                    $filename   = md5(time() * rand(100, 1000)) .'.pdf';
//                    generate a random varible name
                    $pdf  = time();
//                    assign my PDF Class to my random name.
                    $$pdf = new Pdf();
                    
//                    Pass in my filename for output AND my randomly named PDF class
                    $this->load->view('pdf/invoice', array('pdf' => $$pdf, 'filename' => $filename));

//                    unset my random PDF name - Do I need to, unsure, but heck, why not !
                    $pdf = md5(time());
                    
//                    validate customer email and sent PDF.
                    if(valid_email($email)) {

                        $this->email->from($this->config->item('admin_mail'));
                        $this->email->to($email);
                        $this->email->reply_to($this->config->item('admin_mail'));

                        $this->email->subject('Invoice #'. $invoice_id);
                        $this->email->message($this->load->view('admin/email', array('customer' => $customer, 'invoice' => $invoice), TRUE));
                    
//                        Attach my PDF, send and clear email + all attachemnts
                        $this->email->attach($filename);
                        $this->email->send();
                        $this->email->clear(TRUE);
                    
                    }
//                end loop
                }
                
            }
            
//            finished
            redirect();
        }
    
    }
#5

[eluser]noviceCODER[/eluser]
Quote:Hi,

I am using CI and FPDF with FPDFI, http://www.fpdfi.org

I have it set up and working pretty well, and have been using theses to generate a lot of PDF’s in the last couple of years,


hi i have problem setting up fpdf, can u please help me??

thanks in advance




Theme © iAndrew 2016 - Forum software by © MyBB