Welcome Guest, Not a member yet? Register   Sign In
FPDF - Extending the Class
#1

[eluser]richman[/eluser]
Hi Everyone,

I am not sure if I am being a little silly or just not seeing the light but I have just started using FPDF(I need this to work on php4) and have added the class to my libary folder.

I can create basic PDF's but I cannot seem to work out how or were to extend the class as per the examples on the FPDF website.

Code:
<?php
require('fpdf.php');

class PDF extends FPDF
{
//Page header
function Header()
{
    //Logo
    $this->Image('logo_pb.png',10,8,33);
    //Arial bold 15
    $this->SetFont('Arial','B',15);
    //Move to the right
    $this->Cell(80);
    //Title
    $this->Cell(30,10,'Title',1,0,'C');
    //Line break
    $this->Ln(20);
}

//Page footer
function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    //Arial italic 8
    $this->SetFont('Arial','I',8);
    //Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

//Instanciation of inherited class
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for($i=1;$i<=40;$i++)
    $pdf->Cell(0,10,'Printing line number '.$i,0,1);
$pdf->Output();
?&gt;

I guess my question is how do you extend this class as I cannot do this in a controller as it is already a class.

It would be great if somebody had a sinple way to get around this.

Regards

Rich
#2

[eluser]xwero[/eluser]
put the extended class in a separate file in the same directory of the fpdf file. if the directory is application/libraries you can do
Code:
$this->load->library('pdf');
$this->pdf->AliasNbPages();
$this->pdf->AddPage();
$this->pdf->SetFont('Times','',12);
for($i=1;$i<=40;$i++)
    $this->pdf->Cell(0,10,'Printing line number '.$i,0,1);
$this->pdf->Output();
i recently added a modified fpdf file maybe it could be as useful to you as it was for me.
#3

[eluser]Jonathon Hill[/eluser]
I'm using TCPDF (based on FPDF) and I put my code in a view because the PDF is properly a view in my opinion. Here's an example:

pdf_view.php:

Code:
&lt;?php


// Extend the TCPDF class to create custom Header and Footer
class Mypdf extends Tcpdf {

    function __construct()
    {
        parent::__construct();
        $this->init();
    }


    function init()
    {
        $this->Open();
        
        # Margins
        $this->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $this->SetHeaderMargin(PDF_MARGIN_HEADER);
        $this->SetFooterMargin(PDF_MARGIN_FOOTER);
        
        # Auto page breaks
        $this->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
        
        # Image scale factor
        $this->setImageScale(PDF_IMAGE_SCALE_RATIO);
        
        # Initialize document
        $this->AliasNbPages();
        
        $this->setPrintFooter(false);
    }


        //Page header
    public function Header()
    {
            //...
        }

}



$pdf = new Mypdf;


# Document information
$pdf->SetCreator(PDF_CREATOR);

// ...

At the end of the view I have this:

Code:
$pdf->Output('quote.pdf', 'I');


Controller:

Code:
$this->load->library('tcpdf');
$this->load->view('pdf_view', $data);
#4

[eluser]xwero[/eluser]
You are right by thinking the pdf output method should be in a view file if you follow MVC very stricly, but i think extending the class in the view file is a bridge too far. It feels like extending a model in a view file to format the data for the view. And i think creating a view file only to add the output method is pushing the strict MVC pattern.
#5

[eluser]Jonathon Hill[/eluser]
Guess so, but I have three PDF views and they're all hundreds of lines long. I don't want to bloat my controllers and it doesn't feel right to create a library for each PDF. So the view is the only way to go.

I don't think there's any problem with the output command at the end of the view because $this->load->view() is normally the last thing in the controller function and CI's default behaviour is to go ahead and output the view. That's what I'm doing here, except I'm calling with TCPDF's output method instead of CI's.
#6

[eluser]xwero[/eluser]
Why doesn't it feel right to create libraries for extended classes of the TCPDF library?
#7

[eluser]Jonathon Hill[/eluser]
Well, extending the TCPDF class is really only a technique that you don't have to use at all, and if the functions you use are common to your various PDF 'views', that would very much be the ideal situation.
#8

[eluser]richman[/eluser]
Hi xwero and Jonathon,

Thanks for your reply to my questions you have helped me out with this one.
#9

[eluser]ImageSmith[/eluser]
Hey Rich,
You could have just phoned your ol buddy who already has dealt with the pain of implementing this. :cheese:
Don't suppose you have messed with TCPDF?
Any one had a shot at creating multi page doc from html with TCPDF?
#10

[eluser]Jonathon Hill[/eluser]
If you want to create a PDF from HTML, TCPDF is a poor choice. Try dompdf.




Theme © iAndrew 2016 - Forum software by © MyBB