Как обернуть текст в несколько столбцов, используя FPDF в php? - PullRequest
0 голосов
/ 18 сентября 2018

У меня есть вопрос в FPDF с использованием Php.

В моем коде сейчас есть столбец 1 всегда будет следовать за высотой столбец 2 , как показано ниже, в зависимости от длины символа.

Проблема только в колонке 2 работает.

Когда я пытаюсь увеличить длину символа в столбце 1, это будет выглядеть так.enter image description here

Когда я пытаюсь увеличить длину символа в столбце 2, это будет выглядеть следующим образом.enter image description here

Когда я пытаюсь увеличить длину символа в столбце 3, это будет выглядеть следующим образом.enter image description here

Как можно сделать / разрешить любую минимальную высоту столбца всегда следовать максимальной высоте ячейки, когда один из столбцов увеличивает длину символа.

Мой текущий код, как показано ниже.

index.php

        class myPDF extends FPDF 
        {
            function HeaderTable(){
                $this->SetFont('Arial','B',10);
                $this->Cell(275, 5, 'TABLE DATA',0,0);
                $this->Ln();

                $this->Cell(275, 1, '',0,0);
                $this->Ln();
                $this->SetFont('Arial','B',6);

                $this->Cell(35,5,'COLUMN 1',1,0);
                $this->Cell(120,5,'COLUMN 2',1,0);
                $this->Cell(120,5,'COLUMN 3',1,0);
                $this->Ln();
            }
            function ViewTable(){
                $datatable = array(
                    array(
                        "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum ",
                        "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum ",
                        "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum "
                    ),
                    array(
                        "18 Sept 2018",
                        "This is second row of data",
                        "Data here for second row",
                    ),
                    array(
                        "18 Sept 2018",
                        "This is third row of data",
                        "Data here for third row",
                    ),
                );

                foreach($datatable as $item)
                {   
                    $cellWidth=120;  
                    $cellHeight=5;  

                        if( ($this->GetStringWidth($item[1]) && $this->GetStringWidth($item[2]))  < $cellWidth)
                        {   
                            $line   = 1;
                            $line2  = 1;
                        } 
                        else
                        {
                            $textLength=strlen($item[1]);
                            $errMargin=10;
                            $startChar=0;
                            $maxChar=0;
                            $textArray=array();
                            $tmpString="";

                            $textLength2=strlen($item[2]);
                            $errMargin2=10;
                            $startChar2=0;
                            $maxChar2=0;
                            $textArray2=array();
                            $tmpString2="";

                            // 1
                                while($startChar < $textLength)
                                {   
                                    while($this->GetStringWidth($tmpString) < ($cellWidth-$errMargin) && 
                                    ($startChar+$maxChar) < $textLength)
                                    {
                                        $maxChar++;
                                        $tmpString=substr($item[1], $startChar, $maxChar);
                                    }
                                    $startChar=$startChar+$maxChar;
                                    array_push($textArray, $tmpString);
                                    $maxChar=0;
                                    $tmpString='';
                                }
                            // 1

                            // 2
                                while($startChar2 < $textLength2)
                                {   
                                    while($this->GetStringWidth($tmpString2) < ($cellWidth-$errMargin2) && 
                                    ($startChar2+$maxChar2) < $textLength2)
                                    {
                                        $maxChar2++;
                                        $tmpString2=substr($item[2], $startChar2, $maxChar2);
                                    }
                                    $startChar2=$startChar2+$maxChar2;
                                    array_push($textArray2, $tmpString2);
                                    $maxChar2=0;
                                    $tmpString2='';
                                }
                            // 2

                            $line=count($textArray);
                            $line2=count($textArray2);
                        }
                    $this->Cell(35,($line * $cellHeight),$item[0],1,0);
                    $xPos=$this->GetX();
                    $yPos=$this->GetY();
                    $this->MultiCell($cellWidth, $cellHeight, $item[1],1);
                    $this->SetXY($xPos + $cellWidth, $yPos);
                    $this->MultiCell($cellWidth, $cellHeight, $item[2],1);
                    $this->SetXY($xPos + $cellWidth, $yPos);
                }
            }
        }

        $pdf = new myPDF('L','mm','A4');
        $pdf->AddPage();

        $pdf->HeaderTable();
        $pdf->ViewTable();

        $pdf->Output();

Благодарим вас за помощь в решении этой проблемы.

Спасибо.

1 Ответ

0 голосов
/ 22 сентября 2018

Для работы с таблицами и многоэлементами вы должны расширить класс fpdf классом PDF_MC_Table. На этой странице вы найдете исходный код и примеры, которые могут вам помочь.Этот класс был создан для работы с таблицами, надеюсь, это поможет вам.

...