Php 使用fpdf的pdf分页符过多
我想以表格的形式显示MySQL数据库中的数据。当数据超过第一页时,下一行将正确写入下一页。但之后的每一行都会写入一个新页面。这意味着第一页看起来不错,但第二页及其后的页面将只包含一行。我怎样才能防止这种情况?下面是我的代码:Php 使用fpdf的pdf分页符过多,php,html,mysql,fpdf,Php,Html,Mysql,Fpdf,我想以表格的形式显示MySQL数据库中的数据。当数据超过第一页时,下一行将正确写入下一页。但之后的每一行都会写入一个新页面。这意味着第一页看起来不错,但第二页及其后的页面将只包含一行。我怎样才能防止这种情况?下面是我的代码: <?php require('fpdf17/fpdf.php'); require('db.php'); //create a FPDF object $pdf=new FPDF(); //set font for the entire document $pdf
<?php
require('fpdf17/fpdf.php');
require('db.php');
//create a FPDF object
$pdf=new FPDF();
//set font for the entire document
$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
//insert an image
$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:');
$fac = addslashes( filter_input( INPUT_GET,'fac',FILTER_SANITIZE_STRING ) );
$data = "SELECT * FROM faculty WHERE fac_code LIKE '$_GET[fac]'";
$result = $conn->query($data) or die("Error: ".mysqli_error($conn));
while($ser=mysqli_fetch_array($result)){
$fac_name = $ser['fac_name'];
$pdf->SetFillColor(0,0,0);
$pdf->SetFont('Times', '', 12);
$pdf->SetY(50);
$pdf->Cell(10);
$pdf->SetX(20);
$pdf->Cell(30, 6, 'FACULTY' , 0, 0, '', 0);
$pdf->Cell(30, 6, ': '.$fac.'-'.' '.$fac_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(45, 6, 'Name', 1, 0, 'L', 1);
$pdf->Cell(31, 6, 'Programme', 1, 0, 'L', 1);
$pdf->Cell(20, 6, 'Course', 1, 0, 'L', 1);
$pdf->Cell(35, 6, 'Absenteeism %', 1, 0, 'L', 1);
$row_height = 6;
$y1_axis = 58;
$y1_axis = $y1_axis + $row_height;
$counter = 1;
$fac = addslashes( filter_input( INPUT_GET,'fac',FILTER_SANITIZE_STRING ) );
$data = "
SELECT
stud_matric, stud_name, fac_code, fac_name, group_group_code, prog_code, course_course_code,
getid, SUM(studAtt_endTime - studAtt_startTime)/(course_contacthour_perWeek * 14) AS 'sum'
FROM studentattendance";
$result = $conn->query($data) or die("Error: ".mysqli_error($conn));
while($ser=mysqli_fetch_array($result)){
$fac_name = $ser['fac_name'];
$stud_matric = $ser['stud_matric'];
$stud_name = $ser['stud_name'];
$prog_code = $ser['prog_code'];
$course_course_code = $ser['course_course_code'];
$per= $ser['sum'];
$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(45, 6, $stud_name, 1, 0, 'L', 1);
$pdf->Cell(31, 6, $prog_code, 1, 0, 'L', 1);
$pdf->Cell(20, 6, $course_course_code, 1, 0, 'L', 1);
$pdf->Cell(35, 6, $per, 1, 0, 'L', 1);
$pdf->Ln();
$y1_axis = $y1_axis + $row_height;
$counter++;
}
//end first table
//Output the document
$pdf->Output("$fac.pdf",'I');
?>
我怀疑原因是您的$y1_轴
太大;最简单的解决方案可能是添加到数据库循环的底部:
$y1_axis = $y1_axis + $row_height;
$counter++;
if ($counter % 35 === 0) {
$pdf->AddPage();
$y1_axis = 20;
}
创建pdf时,请确保使用$pdf->SetAutoPageBreak(false)
禁用自动分页符。我想您需要实现分页脚本。fpdf代码的其余部分在哪里?你如何创建$pdf
?我已经编辑了这篇文章并显示了我的完整代码@米肯:怎么了?顺便说一句,我已经编辑了帖子并显示了我的完整代码@ErenArdahan@ErenArdahan不需要分页,Ln()
命令移到下一行,PDF应该可以自己处理分页符。是的,效果很好。。TQ!顺便说一句,还有一个问题,我能做些什么来适应表格单元格中的长数据。。请看第三列@Miken32我想你可以使用GetStringWidth()
,如果它比你的列宽,则将列高增加一倍并将文本换行。TQ,我将尝试找出你的建议@米肯32