I'm generating a report with fpdf using php. I read a csv file and print 3 columns. For each column, I've created a loop that prints the data. That way I have a loop for each column. When I use $pdf->SetAutoPageBreak
my code prints one column on each page. This occurs when the column exceeds the page limit. I've already tried to disable $pdf->SetAutoPageBreak
using false, but it prints out beyond the margin. Can someone give me some help?
<?php
session_start();
require_once('tcpdf/tcpdf.php');
require_once ('fpdf/fpdf.php');
require('Header.php');
require('PageBreak.php');
//print($csvFile);
$fileName= '/Applications/XAMPP/xamppfiles/htdocs/CreateReport/CLINICAL PLACEMENT REQUEST SURVEY - Individual Attempts (2).csv';
ob_start();
function readCsv($fileName){
$handle = fopen($fileName, "r");
$data=array();
while ($col = fgetcsv($handle, 10000, ",")) {
$data[] = [
'Section' => $col[0],
'Q #' => $col [1],
'Q Type' => $col[2],
'Q Title' => $col[3],
'Q Text' => $col[4],
'Bonus' => $col [5],
'Difficulty' => $col[6],
'Answer' => $col[7],
'Answer Match' => $col[8],
'Responses'=> $col[9],
];
}
fclose($handle);
return $data;
}
ob_end_flush();
function countLines($arr, $startLine){ //count blank lines btw sections
$count=0;
$length= count($arr);
for($r=$startLine; $r<=$length; $r++){
if((empty($arr[$r]['Section']))){
$count+=1;
}else{
break;
}
}
return $count;
}
function cmp($a, $b) {
// return $a['Answer Match'] > $b['Answer Match'] ;
return strcmp($a['Answer Match'], $b['Answer Match']) ;
}
function countNumberOfElements( $column){
$arrData= Array();
$arrData= readCsv('/Applications/XAMPP/xamppfiles/htdocs/CreateReport/CLINICAL PLACEMENT REQUEST SURVEY - Individual Attempts (2).csv');
$count=0;
$length=count($arrData);
for ($k=1; $k < $length; $k++) {
if(!empty($arrData[$k][$column])){
$count++;
}
}
return $count;
}
$page=
"<html>
<body>
<h1> hello </h1>
<br>
</body>
</html>
";
// Converts file to pdf
function printPDF($page, $arrCVS)
{
$marginLeft=25;
$marginRight=25;
$marginTop=30;
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage('L');
$pdf->SetMargins($marginLeft, $marginTop, $marginRight, true);
$fileOutput= "Report.pdf";
$pdf->SetAutoPageBreak(true,$marginTop );
$pdf->SetFont('helvetica','', 10);
$lineX=12; //space btw answers
$lineY=5;
$lenght=count($arrCVS); //array lenght
for ($k=2; $k < $lenght; $k++) {
if ($arrCVS[$k]['Answer']=="Health Science 5 (HSC)"){
$arrCVS[$k]['Answer']="HSC";
}else if ($arrCVS[$k]['Answer']=="St. Boniface (STB)"){
$arrCVS[$k]['Answer']="STB";
} else
if ($arrCVS[$k]['Answer']=="Grays Hospital (GRH)"){
$arrCVS[$k]['Answer']="GRH";
} else
if ($arrCVS[$k]['Answer']=="Concordia (CON)"){
$arrCVS[$k]['Answer']="CON";
}
else
if ($arrCVS[$k]['Answer']=="Seven Oaks (SEO)"){
$arrCVS[$k]['Answer']="SEO";
}
else
if ($arrCVS[$k]['Answer']=="Health Science 3 (HSA)"){
$arrCVS[$k]['Answer']="HSA";
}
else
if ($arrCVS[$k]['Answer']=="Health Science 4 (HSB)"){
$arrCVS[$k]['Answer']="HSB";
}
}
for ($x=2; $x < $lenght; $x++) {
switch ($arrCVS[$x]['Answer']) {
case "Personal (PE)":
$arrCVS[$x]['Answer']=" PE ";
break;
case "Bison athlete (BA)":
$arrCVS[$x]['Answer']=" BA ";
break;
case "Work in Facility (WF)":
$arrCVS[$x]['Answer']=" WF ";
break;
case "Live out of town (LT)":
$arrCVS[$x]['Answer']=" LT ";
break;
}
}
//Display student name
$positionXname=$marginLeft;
$positionYname=$marginTop + ($marginTop*0.5);
$pdf->SetXY($positionXname, $positionYname);
$countStudents=0;
$countLines=0;
unset($arrCVS[0]); //skip line 1, header
for ($s=0; $s < $lenght; $s++) {
if(!empty($arrCVS[$s]['Section']) OR $arrCVS[$s]['Section'] =! "Section #" ){
$pdf->SetX($positionXname);
$countStudents++;
$countLines++;
$pdf->Cell($lineX-5,$lineY,$countStudents,0,0,'L');
$pdf->Cell($lineX,$lineY,$arrCVS[$s]['Section'],0,1,'L');
}
if($countLines>24){
// $pdf->AddPage('L');
$positionXname=25;
$positionYname=30;
$countLines=0;
}
}
$lineXSites=12; //space btw answers
$lineYSites=5;
$marginLeftSites=25;
$marginRight=25;
$marginTopSites=30;
//Sites ranking
$positionXsites=$marginLeftSites + ($marginLeftSites*2);
$positionYsites=$marginTopSites + ($marginTopSites*0.5);
$positionXname=25;
$positionYname=30;
$pdf->SetXY($positionXsites, $positionYsites);
$lenght=count($arrCVS);
$numberOfStudents= countNumberOfElements('Section'); //ok
$startPoint=2;//2;
$perRowSites = 1;
for($student=0; $student< $numberOfStudents; $student++){
$arr_Rank=(array_slice($arrCVS, $startPoint,47)); //new array
usort($arr_Rank, 'cmp');
//Print 1 student
for ($j=2; $j <47; $j++) {
if(($arr_Rank[$j]['Q Type'])=="MAT" AND ($arr_Rank[$j]['Responses'])==1){
$pdf->Cell($lineXSites,$lineYSites,$arr_Rank[$j]['Answer'],0,0,'L');
$perRowSites++;
}
if(($perRowSites>6) OR ($arr_Rank[$j]['Answer Match'])=="UnChecked"){ //print 6 sites per row
$pdf->Ln();
$perRowSites=1;
$pdf->SetX($positionXsites);
break;
}
}
$startPoint=$startPoint+47;
}//end $students
$lineXCon=12; //space btw answers
$lineYCon=5;
$marginLeft=25;
$marginRight=25;
$marginTop=30;
//Special considerations
$positionXcon=$marginLeft + ($marginLeft*6);
$positionYcon=$marginTop + ($marginTop*0.5);
$pdf->SetXY($positionXcon, $positionYcon);
unset($arrCVS[0]); //skip line 1, header
$perRow = 1;
$countLines=0;
for ($l=0; $l < $lenght; $l++) {
if (($arrCVS[$l]['Answer Match'])=="UnChecked") {
$perRow++;
if(($arrCVS[$l]['Responses'])==1){
$pdf->Cell($lineXCon,$lineYCon,$arrCVS[$l]['Answer'],0,0,'L');
}
if($perRow>4 ){
$pdf->Ln();
$perRow=1;
$pdf->SetX($positionXcon);
}
}
}
$pdf->Output('reports/'.$fileOutput, 'F');
}
printPDF($page,readCsv($fileName) );
session_destroy();
?>
My header.php class
<?php
require_once ('fpdf/fpdf.php');
class PDF extends FPDF
{
// Page header
function Header()
{
if ($this->page == 1)
{
$marginLeft=25;
$marginRight=25;
$marginTop=30;
$this->SetFont('helvetica','B',12);
$this->SetY(15);
$this->Cell(0,25,'SITES RANKING REPORT',0,0,'C');
$this->Ln(10);
$this->SetXY(25, 30);
$this->Cell(50,15,'STUDENT',0,0,'L',0);
$this->Cell(65,15,'SITES',0,0,'C',0);
$this->Cell(130,15,'CONSIDERATIONS',0,0,'C',0);
$this->line($marginLeft, 45, 300- $marginRight, 45); // 25mm from each edge
$this->Ln(20);
$this->SetFont('helvetica','', 9);
$this->SetXY($marginLeft+($marginLeft*0.25), $marginTop + ($marginTop*0.25));
$this->Cell(150,10,' 1 2 3 4 5 6',0,0,'C',0);
$this->SetXY($marginLeft+($marginLeft*5), $marginTop + ($marginTop*0.25));
$this->Cell(120,10,'LT=Live out of town WF=Work in facility BA=Bison athlete PE=Personal',0,0,'C',0);
// Line break
$this->Ln(20);
}
}
// Page footer
function Footer()
{
$marginLeft=25;
$marginRight=25;
$marginTop=30;
$this->SetY(-15);
$this->line($marginLeft, 215-$marginTop, 300- $marginRight, 215-$marginTop); // 30mm from each top bottom
$this->SetFont('helvetica','', 9);
$this->SetXY($marginLeft*3, 215-$marginTop);
$this->Cell(150,10,'HSC=Health Science 5 STB=St. Boniface CON=Concordia GRH=Grays Hospital SEO=Seven Oaks HSA=Health Science 3 HSB=Health Science 4',0,0,'C',0);
$this->SetFont('helvetica','I',8);
// Page number
$this->Cell(0,25,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
var $col = 0;
function SetCol($col)
{
// Mudar de coluna
$this->col = $col;
$x = 10+$col*65;
$this->SetLeftMargin($x);
$this->SetX($x);
}
function AcceptPageBreak()
{
//esquema de 3 colunas
if($this->col<2)
{
// Avança para a próxima coluna
$this->SetCol($this->col+1);
$this->SetY(10);
return false; //Não quebra a página
}
else
{
// Volta para a primeira coluna e quebra a página
$this->SetCol(0);
return true;
}
}
}
$pdf = new PDF(); //usa a sua implementação específica
$pdf->AddPage();
$pdf->SetFont('Arial','',12);
for($i=1;$i<=300;$i++)
$pdf->Cell(0,5,"Line $i",0,1);
$pdf->Output();
$pdf->AliasNbPages();