2014-09-01 58 views
2

我在使用fpdf庫創建PDF表格結構時遇到了問題。當perticular細胞中的任何數據有很長的字符串,則該單元格數據將與其他單元格數據重疊.. 自動調整單元格的寬度在FPDF ... plz幫助我...如何使用php和mysql自動調整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(); 
    ?> 
    ?> 
+0

看這裏:http://stackoverflow.com/questions/3477372/make-text-wrap-在-A-細胞與 - FPDF – 2014-09-01 10:19:13

回答

0

表使用多管來解決動態的寬度和height.And的問題,請點擊此鏈接看到。這將是 動態的寬度和高度非常有用的插件here的例子。

0

例如,您可以使用此代碼。你必須要求mysql_table.php tho。

第一定義列,大小,名稱和對準
ADDCOL(<列的MySQL表名稱>,<柱尺寸>,<名稱你優選>,<對齊>)
然後你可以打印表格。
如果你認爲第一列是較小的,將其設置爲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!

相關問題