Php 使用fpdf使文本适合单元格

Php 使用fpdf使文本适合单元格,php,mysql,fpdf,Php,Mysql,Fpdf,我正在使用fpdf制作pdf文件。。生成的fdf将以表格形式显示数据库中的数据。我的问题是,当数据字符串太长时,它不适合表格单元格。请看下面第3栏: 以下是我的完整代码: <?php require('fpdf17/fpdf.php'); require('db.php'); //create a FPDF object $pdf=new FPDF(); $pdf->SetFont('Times','B',20); //set font for the whole page (

我正在使用fpdf制作pdf文件。。生成的fdf将以表格形式显示数据库中的数据。我的问题是,当数据字符串太长时,它不适合表格单元格。请看下面第3栏:

以下是我的完整代码:

<?php
require('fpdf17/fpdf.php');
require('db.php');

//create a FPDF object
$pdf=new FPDF();

$pdf->SetFont('Times','B',20); //set font for the whole page (font family, style, size)
$pdf->SetTextColor(0,0,0); //using RGB value

//set up a page
$pdf->AddPage('P');  //potrait orientation
$pdf->SetDisplayMode('default'); //using 100 percent zoom and the viewer's default layout

$icon = "files/icon.png";
$pdf->Cell (10);
$pdf->Cell(60, 60, $pdf->Image($icon, $pdf->GetX(), $pdf->GetY(), 33.78), 0, 0, 'L', false);

$pdf->SetFillColor(0,0,0);
$pdf->SetFont('Times', '', 12);
$pdf->SetXY(10, 30);
$pdf->Cell(10);
$pdf->Cell(30, 6, 'Retrieval Date' , 0, 0, '', 0);
date_default_timezone_set("Asia/Kuala_Lumpur"); //set default time zone
$pdf->Cell(30, 6, ': '.date("d/m/Y"), 0, 2, 'B', 0);

//Set x and y position for the main text, reduce font size and write content
$pdf->SetXY(20,40); //setting the position
$pdf->SetFont('Times', 'BU', 14);
$pdf->Write(12,'Absenteeism record for:');

$course_course_code = addslashes( filter_input( INPUT_GET,'course',FILTER_SANITIZE_STRING ) );
$data = "SELECT course_code, course_name FROM studentattendance 
                INNER JOIN course ON studentattendance.course_course_code=course.course_code WHERE course_course_code LIKE '$_GET[course]' GROUP BY course_code";

                $result = $conn->query($data)  or die("Error: ".mysqli_error($conn));
                while($ser=mysqli_fetch_array($result)){

                    $course_code = $ser['course_code'];
                    $course_name = $ser['course_name'];

$pdf->SetFillColor(0,0,0);
$pdf->SetFont('Times', '', 12);
$pdf->SetY(50);
$pdf->Cell(10);
$pdf->SetX(20);
$pdf->Cell(30, 6, 'COURSE' , 0, 0, '', 0);
$pdf->Cell(30, 6, ': '.$course_code. ' - '.$course_name, 0, 2, 'B', 0);
}

//start first table
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Times', 'B', 12);
$pdf->SetXY(21,58);
$pdf->Cell(10, 6, 'No.', 1, 0, 'L', 1);
$pdf->Cell(25, 6, 'Matric no', 1, 0, 'L', 1);
$pdf->Cell(75, 6, 'Name', 1, 0, 'L', 1);
$pdf->Cell(25, 6, 'GROUP', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Absenteeism %', 1, 0, 'L', 1);

$row_height = 6;
$y1_axis = 58;
$y1_axis = $y1_axis + $row_height;
$counter = 1;
$course_course_code = addslashes( filter_input( INPUT_GET,'course',FILTER_SANITIZE_STRING ) );
$data = "SELECT stud_matric, stud_name, group_group_code, getid, 
                SUM(studAtt_endTime - studAtt_startTime)/(course_contacthour_perWeek * 14) AS 'sum' FROM studentattendance 
                INNER JOIN course ON studentattendance.course_course_code=course.course_code 
                INNER JOIN student ON studentattendance.student_stud_matric=student.stud_matric
                WHERE studAtt_status='0' AND course_course_code LIKE '$_GET[course]' GROUP BY getid ORDER BY sum DESC";

$result = $conn->query($data)  or die("Error: ".mysqli_error($conn));
while($ser=mysqli_fetch_array($result)){

    $stud_matric = $ser['stud_matric'];
    $stud_name = $ser['stud_name'];
    $group_group_code = $ser['group_group_code'];
    $per=number_format($ser['sum'] * 100, 2)." %";

$pdf->SetFont('Times', '', 12);
    $pdf->SetXY(21, $y1_axis);
    $pdf->Cell(10, 6, $counter, 1, 0, 'L', 1);
    $pdf->Cell(25, 6, $stud_matric, 1, 0, 'L', 1);
    $pdf->Cell(75, 6, $stud_name, 1, 0, 'L', 1);
    $pdf->Cell(25, 6, $group_group_code, 1, 0, 'L', 1);
    $pdf->Cell(30, 6, $per, 1, 0, 'L', 1);
    $pdf->Ln();


$y1_axis = $y1_axis + $row_height;
$counter++;
if ($counter % 34 === 0) {
    $pdf->AddPage();
    $y1_axis = 20;
}

}
//end first table

//Output the document
$pdf->Output("$course_code.pdf",'I'); 
?>

更改单元格的大小

$pdf->Cell(5, 6, 'No.', 1, 0, 'L', 1);
$pdf->Cell(25, 6, 'Matric no', 1, 0, 'L', 1);
$pdf->Cell(100, 6, 'Name', 1, 0, 'L', 1);
$pdf->Cell(25, 6, 'GROUP', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Absenteeism %', 1, 0, 'L', 1);

更改单元格的大小

$pdf->Cell(5, 6, 'No.', 1, 0, 'L', 1);
$pdf->Cell(25, 6, 'Matric no', 1, 0, 'L', 1);
$pdf->Cell(100, 6, 'Name', 1, 0, 'L', 1);
$pdf->Cell(25, 6, 'GROUP', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Absenteeism %', 1, 0, 'L', 1);

对动态大小字符串使用MultiCell()。它将根据字符串的长度自动调整。动态大小字符串使用MultiCell()。它将根据字符串的长度自动调整。可能值得解释此操作的作用。更改单元格的大小请将其添加到您的答案中以防止其被删除。可能值得解释此操作的作用。更改单元格的大小请将其添加到您的答案中以防止其被删除。