php - 如何使用php和mysql自动调整fpdf中的单元格宽度

标签 php mysql fpdf

我在使用 fpdf 库创建 pdf 表结构时遇到问题。当特定单元格的任何数据具有长字符串时,单元格数据将与其他单元格数据重叠。 自动调整 fpdf 中的单元格宽度...请帮助我...

我的代码:

                <?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();
    ?>
    ?>

最佳答案

使用 string 单元格宽度

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

字符串宽度文档 http://www.fpdf.org/en/doc/getstringwidth.htm

细胞文档 http://www.fpdf.org/en/doc/cell.htm

关于php - 如何使用php和mysql自动调整fpdf中的单元格宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25603371/

相关文章:

javascript - 想要将当前网址更改为上一页网址(第二个域)?

php - 如何在 MySQLi 中编写替代 mysql_field_name 的代码?

mysql 正则表达式查询挂起,但在 mysql 进程重新启动时显示结果

PHP - 创建表失败

php - 是否可以用FPDF创建垂直柱形图

破折号的 PHP preg_replace 正则表达式

php - 如何限制php脚本的传出响应速度?

MYSQL从具有重复值的表中选择

php - PDF X-1a :2001 format in PHP

php - FPDF 中的图像对齐错误