Jump to content

FPDF class change


ninedoors

Recommended Posts

I have searched the site and google but can't find the answer to my question.  So here goes:

 

I am using the fpdf class to generate a report that comes directly from a mysql database.  One of the tutorials there explains very wel how to do it.  The problem I am having is that when I pass the data to the pdf there is too much data in some cells and it continues to write over the neighbouring cells. 

 

I know I need to use the MultiCell function but it isn't very clear on how to do so.  Here is the code for the Row function that I am using:

 

<?php
function Row($data)
{
    $this->SetX($this->TableX);
    $ci=$this->ColorIndex;
    $fill=!empty($this->RowColors[$ci]);
    if($fill)
        $this->SetFillColor($this->RowColors[$ci][0],$this->RowColors[$ci][1],$this->RowColors[$ci][2]);
    foreach($this->aCols as $col)
        $this->Cell($col['w'],5,$data[$col['f']],1,0,$col['a'],$fill);
    $this->Ln();
    $this->ColorIndex=1-$ci;
}

?>

 

From the examples I have seen it looks like I need to change this row:

 

<?php $this->Cell($col['w'],5,$data[$col['f']],1,0,$col['a'],$fill);?>

 

and use the MultiCell function.  But I am not sure how make the next cell go to the right rather than below the current cell.  I am new to using the fpdf class so any help would be greatly appreciated.  Thanks

Link to comment
https://forums.phpfreaks.com/topic/105284-fpdf-class-change/
Share on other sites

I was under the impression that you can somehow use GetX and GetY functions to find the position before you add the current cell and then after you added the current cell use SetXY function to put it to the right of the added cell.  Does this sound right or no?  I uncerstand this logical and have try to do so but haven't got it to work.

 

Any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/105284-fpdf-class-change/#findComment-539120
Share on other sites

with cell(), you can tell it to go to the next cell location to the right. for multicell, you could get x/y before multicell(). you set the width of the cells when you call multicell(), so you'll know the next y from that. so,yes, you could setxy() after multicell. it just doesn't do it automatically like cell() can.

 

maybe something like:

$currentx = $fpdf->getx();
$currenty = $fpdf->gety();

multicell(20, 3, "hello world"); // the new x will be the previous x plus the width of multicell:
$newx = $currentx + 20;
$fpdf->Setxy($newx, $currenty);// next cell should appear to the right and at the top of multicell

very not tested.  :)

Link to comment
https://forums.phpfreaks.com/topic/105284-fpdf-class-change/#findComment-539125
Share on other sites

I'm still having problems with it.  The pdf now looks fine on the first page but the pages beyond that are messed up.  I am attaching the pdf if you would like to look at it.  Also, the script now only print till the end of the cells and doesn't create a new line within the cell.  So it is still only one line the text just gets cut off.  Here is the script I am using as well the the Row function.

 

<?php
define('FPDF_FONTPATH','pdf/font/');
require('pdf/mysql_table.php');

class PDF extends PDF_MySQL_Table
{
function Header()
{
    //Title
    $this->SetFont('Arial','B',18);
    $this->Cell(0,6,'PTL Line Balancing',0,1,'C');
    $this->Ln(10);
    //Ensure table header is output
    parent::Header();
}
}

//Connect to database
include 'config/PMAconfig.php';

$pdf=new PDF();
$pdf->Open();
$pdf->AddPage();
//First table:
$pdf->SetFont('Times','',6);
$pdf->AddCol('id',10,'#','C');
$pdf->AddCol('sku1',25,'Moving Sku','C');
$pdf->AddCol('startloc1',40,'Start','C');
$pdf->AddCol('finloc1',-1,'Finish','C');
$pdf->AddCol('sku2',25,'Sku(SS)','C');
$pdf->AddCol('finloc2',40,'Warehouse','C');
$pdf->AddCol('movepercent',13,'%','C');
$prop=array('HeaderColor'=>array(112,128,144),
            'color1'=>array(255,255,255),
            'color2'=>array(198,226,255),
            'padding'=>2);
$pdf->Table('SELECT id, sku1, startloc1, finloc1, sku2, finloc2, movepercent FROM alltempfcstlocs WHERE (movepercent != 0 AND movepercent != 0.1) ORDER BY id', $prop);

$pdf->Output('C:/xampp/htdocs/PTL/testpdf.pdf');
?>

 

And here is the Row function

 

function Row($data)
{
    $this->SetX($this->TableX);
    $ci=$this->ColorIndex;
    $fill=!empty($this->RowColors[$ci]);
    if($fill)
        {$this->SetFillColor($this->RowColors[$ci][0],$this->RowColors[$ci][1],$this->RowColors[$ci][2]);}
    foreach($this->aCols as $col)
        {
	$x = $this->GetX();
	$y = $this->GetY();
	$this->MultiCell($col['w'],5,$data[$col['f']],1,$col['a'],$fill);
	$newx = $x + $col['w'];
	$this->SetXY($newx, $y);
    	}
$this->Ln();
    $this->ColorIndex=1-$ci;
}

 

If you would like to see the entire class extension I can post it too, it's not that long.

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/105284-fpdf-class-change/#findComment-539192
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.