Question

I'm editing a code that creates a PDF from a table filled with data called from a DB.

The code works perfectly to print in the columns the data of the parameters named in the first row as you can see in the picture.

Table showed OK

But when the DB asks for several parameters, the columns exceed the right margin limit. As you can see in this image.

Table exceeded

I've seen every post in this web from people having problems with tables and FPDF but I can't find a wayout.

This is the code I have to print the data in the PDF:

        $type = 'application/pdf';
        $pdf=new \FPDF('L');//Querformat
        $name=$arbeitsplatz->getName();
        $pdf->AliasNbPages();
        $pdf->SetFont('Arial','',10);
        $pdf->SetTitle('Arbeitsplatzliste');
        $pdf->SetAuthor($user_info->getKuerzel());
        $pdf->SetCreator('PMA Manager');
        $pdf->SetSubject("Arbeitsplatzliste drucken");
        $pdf->SetAutoPageBreak(true, 10);
        $pdf->SetMargins(10, 10, 10);
        //$pdf->SetTopMargin(20);//in mm
        $pdf->AddPage();

        $cellLength_array=array();

        //set initial Y axis position 
        $y_axis = 20;
        $pdf->SetY($y_axis);

        if ($pdf->GetY() == $y_axis)
        {
            //Kopfzeile(Tabelle header)
            $pdf->Cell(30,7,"Probe",1,0,'L');
            $pdf->Cell(20,7,"Frist",1,0,'L');

            //Start to write table
            foreach($apParamArray as $parameter)
            {
                if($bereich==='W' || $bereich==='I'){
                    $arbeit_para_name=utf8_decode($parameter->getName()).' ['.utf8_decode($parameter->getEinheit()).']';
                }
                else 
                {
                    $arbeit_para_name=utf8_decode($parameter->getNameD()).' ['.utf8_decode($parameter->getEinheit()).']';
                }
                $length=strlen($arbeit_para_name)*2;
                array_push($cellLength_array,$length);
                $pdf->Cell($length,7,$arbeit_para_name,1,0,'C');

            }   
                $pdf->Ln(7);//mm
        }

        //Start to add data
        foreach($apProbenArray as $probe)
        {
            $frist=$probe->getFrist();
            $keys=array();
            $keys=array_keys($probe->getParameterliste());

            if($bereich==='W'  || $bereich==='I'){$pma_nr=$probe->getPmaNr();} else{$pma_nr= $probe->getLaborNr();}
            $pdf->Cell(30,7,$pma_nr,'LRB',0,'L');
            $pdf->Cell(20,7,$frist,'LRB',0,'L');

            for($j=0;$j<(sizeof($arbeit_para_r_ID_array));$j++)
            {
                $breite=$cellLength_array[$j];
                $arbeit_parameterID=$arbeit_para_r_ID_array[$j];
                if(in_array($arbeit_parameterID , $keys))
                {
                    $parameterliste=$probe->getParameterliste();
                    //print_r($parameterliste);
                    $ergebnis=$parameterliste[$arbeit_parameterID]->getErgebnis();
                    if($ergebnis!==null)
                        $pdf->Cell($breite,7,utf8_decode($ergebnis),'LRB',0,'C');
                    else
                        $pdf->Cell($breite,7,'...','LRB',0,'C');
                }
                else
                    $pdf->Cell($breite,7,'','LRB',0,'C');
            }

            $pdf->Ln(7);//mm
        }   
        //end of writting data

        //Print and get file
        $pdf->Output('prints/Arbeitsplatzliste.pdf','F');
        $fichier='../web/prints/Arbeitsplatzliste.pdf';
        $response = new Response();
        $filename='Arbeitsplatzliste.pdf';
        $response->setContent(file_get_contents($fichier));
        $response->headers->set('Content-Type', $type);
        $response->headers->set('Content-disposition', 'filename='.$filename);
        return $response;

`

I tryed adding a condition

if ($pdf->GetY() == $y_axis) && ($pdf->GetX() == -10)
$pdf->AddPage();

But didn't work.

My intention was also to show the header line in all pages, but It doesn't work either.

I tryed with this -> How to display table header in every page using FPDF library?

And this -> Add sub-headers in all pages with FPDF

Was it helpful?

Solution

I've made it finally with this:

if($pdf->GetX() < 230)
{
$pdf->Cell($length,7,$parameter,1,0,'C',1);
}
else
$pdf->ln();

So when the array of cells reaches the X axis limit (I've selected 230mm because I have the page configurated in Landscape mode), it will break line.

And for showing the header in every page, I tried several times with the Header() function but didn't work. So I made a condition with GetY() function, then when the GetY() value is equal to the top margin of the new page, it will print the header; if not, it will continue writting the table data.

if($pdf->GetY() == 5)
{
$pdf->ln();
//Header...
}
else
{
//Table data...
}

I hope this could help someone else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top