【问题标题】:how to auto adjust cell width in fpdf using php and mysql如何使用php和mysql自动调整fpdf中的单元格宽度
【发布时间】:2014-10-25 12:53:20
【问题描述】:

我在使用 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();
    ?>
    ?>

【问题讨论】:

标签: php mysql fpdf


【解决方案1】:

使用 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

【讨论】:

    【解决方案2】:

    使用带有多单元格的表格来解决动态宽度和高度的问题。点击此链接查看插件here的示例。这将非常有用 动态宽度和高度。

    【讨论】:

      【解决方案3】:

      例如,您可以使用此代码。您必须需要 mysql_table.php。

      首先定义列、大小、名称和对齐方式
      AddCol(,,,)
      然后您可以打印表格。
      如果您认为第一列较小,请将其设置为 10 等。

      $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!

      【讨论】:

        【解决方案4】:

        您可以使用此代码来解决此问题... 这里的概念是只有当字符串大小大于单元格宽度时,字体大小才会减小。

        请注意,您不能将此代码用于多行字符串。为此,您必须使用 MultiCell()

            $pdf = new FPDFCellFit();
            $pdf->FPDF('P','mm','A4');
            $pdf->SetMargins(15,15,15);
            $pdf->SetAutoPageBreak(true,15);
            $pdf->AliasNbPages('{nb}');
        
            $pdf->isFooter = true ;
            $pdf->footerSchool=$college->COLLEGE_NAME;
        
            $BodyX=15;$BodyY=25;
        
            $table_head1=array(25,46,40,20,60);
            $table_head2=array(25,20,26,20,20,16,24,40);
            // some code remove from here for show actual code.
            $pdf->CellFit($table_head2[7],$table_height,$item['remark'],1,0,'C',0,'',1,0);
        

        请查看 2020 年 1 月 20 日行以获取此解决方案。

        【讨论】:

          猜你喜欢
          • 2017-07-04
          • 1970-01-01
          • 1970-01-01
          • 2014-08-28
          • 1970-01-01
          • 2015-05-04
          • 1970-01-01
          • 1970-01-01
          • 2014-12-26
          相关资源
          最近更新 更多