how to auto adjust cell width in fpdf using php and mysql

♀尐吖头ヾ 提交于 2019-12-24 05:12:35

问题


I have a problem in creating table structure in pdf using fpdf library. when any data of perticular cell have a long string then the cell data will overlap with other cell data.. auto adjust cell width in fpdf... plz help me...

My code:

                <?php
    require("fpdf/fpdf.php");

    //Connect to your database
    $mysqli = new mysqli('localhost', 'root', '', 'mus');

            /*
            * This is the "official" OO way to do it,
            */
            if (mysqli_connect_error()) {
                die('Connect Error (' . mysqli_connect_errno() . ') '
                . mysqli_connect_error());
                }

    //Create new pdf file
    $pdf=new FPDF();

    //Open file
    $pdf->Open();

    //Disable automatic page break
    $pdf->SetAutoPageBreak(false);

    //Add first page
    $pdf->AddPage();

    //set initial y axis position per page
    $y_axis_initial = 25;

    //print column titles for the actual page
    $pdf->SetFillColor(232, 232, 232);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetY($y_axis_initial);
    $pdf->SetX(5);
    $pdf->Cell(10, 6, 'CODE', 1, 0, 'L', 1);
    $pdf->Cell(40, 6, 'NAME', 1, 0, 'L', 1);
    $pdf->Cell(40, 6, 'PRICE', 1, 0, 'L', 1);
    $row_height = 6;
    $y_axis = $y_axis_initial + $row_height;

    //Select the Products you want to show in your PDF file
    $sql = 'SELECT WaiterPckey, WaiterName, WaiterShortName FROM `waitermaster`';
    $result= $mysqli->query($sql); 

    //initialize counter
    $i = 0;

    //Set maximum rows per page
    $max = 25;

    //Set Row Height
    ;

    while($row = $result->fetch_row())
    {
        //If the current row is the last one, create new page and print column title
        if ($i == $max)
        {
            $pdf->AddPage();

            //print column titles for the current page
            $pdf->SetY($y_axis_initial);
            $pdf->SetX(4);
            $pdf->Cell(10, 6, 'CODE', 1, 0, 'L', 1);
            $pdf->Cell(40, 6, 'NAME', 1, 0, 'L', 1);
            $pdf->Cell(40, 6, 'PRICE', 1, 0, 'L', 1);

            //Go to next row
            $y_axis = $y_axis + $row_height;

            //Set $i variable to 0 (first row)
            $i = 0;
        }

        $code = $row[0];
        $price = $row[1];
        $name = $row[2];

        $pdf->SetY($y_axis);
        $pdf->SetX(5);
        $pdf->Cell(10, 6, $code, 1, 0, 'L', 1);
        $pdf->Cell(40, 6, $name, 1, 0, 'L', 1);
        $pdf->Cell(40, 6, $price, 1, 0, 'L', 1);

        //Go to next row
        $y_axis = $y_axis + $row_height;
        $i = $i + 1;
    }


    //Create file
    $pdf->Output();
    ?>
    ?>

回答1:


Use string width for cell with

$string = fullname($USER);
$cellWidth = $pdf->GetStringWidth($string);
$pdf->Cell($cellWidth + 5, 0, $string, 0, 0, 'C', true);

String width doc http://www.fpdf.org/en/doc/getstringwidth.htm

Cell docs http://www.fpdf.org/en/doc/cell.htm




回答2:


Use table with multicell to solve the issue of dynamic width and height.And click this link to see the example of plugin here.This will be very much useful for dynamic width and height.




回答3:


For example, you can use this code. You must require mysql_table.php tho.

Define the column first, the size, the name and the align
AddCol (<the name of column in mysql table>, <column size>, <the name you preferred>, <align>)
then you can print the table.
If you think that the first column is smaller, set it to 10 etc.

$pdf->AddCol('code',20,'CODE','C');
$pdf->AddCol('name',40,'NAME');
$pdf->AddCol('price',40,'PRICE','R');
$pdf->Table('-----PUT YOUR SQL QUERY----');
$pdf->Output();

Check it here!



来源:https://stackoverflow.com/questions/25603371/how-to-auto-adjust-cell-width-in-fpdf-using-php-and-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!