require('inc/fpdf.php');
require('inc/mem_image.php');
class PDF extends FPDF
{
// Colored table
function FancyTable()
{
$this->SetFont('Helvetica','B');
$this->Cell(27, 4, 'Other Title', 1, 0);
}
}
$pdf = new PDF_MemImage();
//$pdf = new PDF();
$pdf->SetFont('Helvetica','',7);
$pdf->AddPage();
$img = file_get_contents('logo.jpg');
$pdf->MemImage($img, 50, 30);
$pdf->Cell(20,10,'Title',1,1,'C');
//$pdf->FancyTable();
$pdf->Output();
I am trying to incorporate the mem_image script into my existing FPDF pages. I am using a method called fancyTable to set up my table data/layout. Unfortunately trying to call fancyTable breaks the page (blank). If I comment it out, its fine. If I switch out the $pdf declaration to new PDF() instead of new PDF_memimage() fancyTable works, but obviously my image doesn’t load. I think its probably just a conflict with the class, but I am not versed in OO syntax.
The solution to this is actually quite straight forward: you have extended the wrong class.
All you need to do to fix it is change this:
…to this:
…and then change this:
…to this:
By extending
FPDF, you have created a new branch in the inheritance tree based on the trunk (FPDF), whereas what you want to do is base it on the existing branchPDF_MemImage.For the record, naming your class
PDFis probably not a great idea – firstly it’s not very descriptive (it doesn’t indicate that it inherits from FPDF or MemImage) and secondly it is very short and would run the risk of collision with another class if used as part of a larger application. I personally would call itFPDF_MemImage_FancyTable, YMMV.Also, in this day and age, there really is no need to write classes that are PHP4 compatible. (In saying this, I actually do need to do this occasionally, but I doubt you do)
EDIT
In case it’s not clear from the above description, the final code would look like this: