Related
How can I auto adjust the cell height in fpdf?
My pdf looks a little bit bad ;)
Screenshot of PDF
My code:
$pdf->SetFont('Arial', 'B', 13);
$pdf->SetLeftMargin(30);
$pdf->SetFillColor(193,229,252);
$pdf->Cell(15, 20, '', 0, 1, 'C');
$pdf->Cell($width_cell[0], 10, 'Datum', 1, 0, 'C', true);
$pdf->Cell($width_cell[1], 10, 'Von', 1, 0, 'C', true);
$pdf->Cell($width_cell[2], 10, 'Bis', 1, 0, 'C', true);
$pdf->Cell($width_cell[3], 10, 'Grund', 1, 1, 'C', true);
$pdf->SetFont('Arial', '', 13);
$fill = false;
while ($z = $stmt->fetch(PDO::FETCH_ASSOC)) {
$pdf->Cell($width_cell[0],10,date("d.m.Y", strtotime($z['date'])),1,0,'C',$fill);
$pdf->Cell($width_cell[1],10,substr($z['startTime'], 0, 5) . ' Uhr',1,0,'C',$fill);
$pdf->Cell($width_cell[2],10,substr($z['endTime'], 0, 5) . ' Uhr',1,0,'C',$fill);
$pdf->Cell($width_cell[3],10,$z['reason'],1,1,'C',$fill);
}
$pdf->Output();
To output multiple lines into a cell you'll need to use MultiCell instead of Cell.
I am trying to download a pdf file. I can download it but I want the name of the file to be specific to a MySQL data. That is, in the code below. I want the name of the file to be the data inside $id. How to achieve this?
<?php
//include connection file
ob_start();
include_once('fpdf/fpdf.php');
include_once('connection.php');
class PDF extends FPDF
{
// Page header
function Header()
{
$this->SetFont('Arial','B',14);
$this->Cell(276, 5, 'Details', 0, 0, 'C');
$this->Ln();
$this->SetFont('Times', '', 12);
$this->Cell(276, 10, 'Details of students', 0, 0, 'C');
$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
function headerTable()
{
$this->SetFont('Times', 'B', 12);
$this->Cell(40, 10, 'ID', 1, 0, 'C');
$this->Cell(20, 10, 'Name', 1, 0, 'C');
$this->Cell(90, 5, 'Score', 1, 0, 'C');
$this->Cell(30, 10, 'Percentage', 1, 0, 'C');
$this->Cell(20, 10, 'Age', 1, 0, 'C');
$this->Cell(0, 5, '', 0, 1, 'C');
//mini cell open
$this->Cell(60, 5, '', 0, 0, 'C');
$this->Cell(45, 5, 'Wrongs', 1, 0, 'C');
$this->Cell(45, 5, 'Rights', 1, 0, 'C');
//mini cell close
//$this->Cell(20, 10, 'Wrongs', 1, 0, 'C');
//$this->Cell(20, 10, 'Rights', 1, 0, 'C');
$this->Ln();
}
function viewTable($db)
{
$this->SetFont('Times', '', 12);
$id=$_GET['id'];
$sql = "SELECT * FROM Datas WHERE ID = '$id'";
$stmt = $db->query($sql);
//$stmt->bindParam('s',$id);
//$stmt->query();
//$result=$stmt->get_result();
//$stmt->get_result();
while($data = $stmt->fetch(PDO::FETCH_OBJ)){
$this->Cell(40, 10, $data->ID, 1, 0, 'C');
$this->Cell(20, 10, $data->Name, 1, 0, 'L');
$this->Cell(45, 10, $data->Wrongs, 1, 0, 'L');
$this->Cell(45, 10, $data->Rights, 1, 0, 'L');
$this->Cell(30, 10, $data->Percentage, 1, 0, 'L');
$this->Cell(20, 10, $data->Age, 1, 0, 'L');
$this->Ln();
}
}
}
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage('L','A4',0);
$pdf->headerTable();
$pdf->viewTable($db);
$pdf->Output("D", "test.pdf"); //How to change test.pdf to $id.pdf? $id value obtained from viewTable()
ob_end_flush();
?>
Nevermind, got it. It was simple by adding $id to the name. The complete script:
<?php
//include connection file
ob_start();
include_once('fpdf/fpdf.php');
include_once('connection.php');
class PDF extends FPDF
{
// Page header
function Header()
{
$this->SetFont('Arial','B',14);
$this->Cell(276, 5, 'Details', 0, 0, 'C');
$this->Ln();
$this->SetFont('Times', '', 12);
$this->Cell(276, 10, 'Details of students', 0, 0, 'C');
$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
function headerTable()
{
$this->SetFont('Times', 'B', 12);
$this->Cell(40, 10, 'ID', 1, 0, 'C');
$this->Cell(20, 10, 'Name', 1, 0, 'C');
$this->Cell(90, 5, 'Score', 1, 0, 'C');
$this->Cell(30, 10, 'Percentage', 1, 0, 'C');
$this->Cell(20, 10, 'Age', 1, 0, 'C');
$this->Cell(0, 5, '', 0, 1, 'C');
//mini cell open
$this->Cell(60, 5, '', 0, 0, 'C');
$this->Cell(45, 5, 'Wrongs', 1, 0, 'C');
$this->Cell(45, 5, 'Rights', 1, 0, 'C');
//mini cell close
//$this->Cell(20, 10, 'Wrongs', 1, 0, 'C');
//$this->Cell(20, 10, 'Rights', 1, 0, 'C');
$this->Ln();
}
function viewTable($db)
{
$this->SetFont('Times', '', 12);
$id=$_GET['id'];
$sql = "SELECT * FROM Datas WHERE ID = '$id'";
$stmt = $db->query($sql);
//$stmt->bindParam('s',$id);
//$stmt->query();
//$result=$stmt->get_result();
//$stmt->get_result();
while($data = $stmt->fetch(PDO::FETCH_OBJ)){
$this->Cell(40, 10, $data->ID, 1, 0, 'C');
$this->Cell(20, 10, $data->Name, 1, 0, 'L');
$this->Cell(45, 10, $data->Wrongs, 1, 0, 'L');
$this->Cell(45, 10, $data->Rights, 1, 0, 'L');
$this->Cell(30, 10, $data->Percentage, 1, 0, 'L');
$this->Cell(20, 10, $data->Age, 1, 0, 'L');
$this->Ln();
}
}
}
$id=$_GET['id'];
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage('L','A4',0);
$pdf->headerTable();
$pdf->viewTable($db);
$pdf->Output("D", "$id.pdf");
ob_end_flush();
?>
I have an invoice template. i used Cell for showing text into box.
here is my code:
$ntitle = 'No:';
$factornum = '1898';
$startheader = 15;
$pdf->SetXY($startheader, 4);
$pdf->Cell(50, 0, 'Date: '.date("Y-m-d"), 1, 1, 'C', 0, '', 1);
$pdf->SetXY($startheader, 12);
$pdf->SetTextColor(255, 0, 0);
$pdf->Cell(50, 0, $ntitle . $factornum, 1, 1, 'C', 0, '', 1);
$pdf->SetTextColor(0, 0, 0);
I want to use two text color in second cell.
$ntitle should be echo in black and $factornum in red
For example, output must be like:
No: (with black color) 1898 (with red color)
How can I do this in TCPDF?
I checked TCPDF examples. but I did not get any results
$ntitle = 'No:';
$factornum = '1898';
$startheader = 15;
$pdf->AddPage();
$pdf->SetXY($startheader, 4);
$pdf->Cell(50, 0, 'Date: '.date("Y-m-d"), 1, 1, 'C', 0, '', 1);
$pdf->SetXY($startheader, 12);
$pdf->SetTextColor(255, 0, 0);
//$pdf->Cell(50, 0, $ntitle . $factornum, 1, 1, 'C', 0, '', 1);
$pdf->SetFillColor(255, 255, 255);
$pdf->writeHTMLCell(50, '', '', '', '<span style="color:#000">'.$ntitle.'</span>' . $factornum, 1, 1, 'C', 0, '', 1);
$pdf->SetTextColor(0, 0, 0);
im making a pdf file using fpdf.. The fdf generated will show data in table form from the database. My problem is, when the data string is too long, it do not fit well in the table cell. Look below in column no 3:
Below is my full code:
<?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');
?>
Please help me..
Change the size of the cell
$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);
I have a simple script that generates a PDF file. I'm getting the info I need from the database and using a foreach to create the pages of the PDF file, and the last page I have some charts and other info that are not retrived from DB. All the pages, except this last one, must have a subheader.
My current code is:
foreach ($sql as $key => $value)
{
$pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
$pdf->Cell(90, $line_height, $value['product'], '', '', 'L');
$pdf->Cell(90, $line_height, $value['ammount'], '', '', 'L');
$pdf->Cell(90, $line_height, $value['value'], '', '', 'L');
}
Basically, I need to have the subheader describing each row. But If I do this, all the lines will have the subheader:
foreach ($sql as $key => $value)
{
$pdf->SetFont('Arial', 'B', $font_size);
$pdf->Ln(2 * $line_height);
$pdf->Cell(20, $line_height, 'Number', '', '', 'L');
$pdf->Cell(120, $line_height, 'Product', '', '', 'L');
$pdf->Cell(40, $line_height, 'Ammount', '', '', 'L');
$pdf->Cell(40, $line_height, 'Value ($)', '', '', 'L');
//
$pdf->SetFont('Arial', 'B', $font_size);
$pdf->Ln(2 * $line_height);
$pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
$pdf->Cell(120, $line_height, $value['product'], '', '', 'L');
$pdf->Cell(40, $line_height, $value['ammount'], '', '', 'L');
$pdf->Cell(40, $line_height, $value['value'], '', '', 'L');
}
I need this subheader in each page, except the last one. I've tried to do some crazy code with pageNo() function, but it didn't work.
Thank's!
Ok guys, I solved my problem. For anyone who's looking for something similar, here is what I did: The GetY() function return the Y position of that line, so when it's the first line of the page, this value will be 0 (or a constant, depends of your layout). I just did:
foreach ($sql as $key => $value)
{
if ($pdf->GetY() == 0)
{
$pdf->SetFont('Arial', 'B', $font_size);
$pdf->Ln(2 * $line_height);
$pdf->Cell(20, $line_height, 'Number', '', '', 'L');
$pdf->Cell(120, $line_height, 'Product', '', '', 'L');
$pdf->Cell(40, $line_height, 'Ammount', '', '', 'L');
$pdf->Cell(40, $line_height, 'Value ($)', '', '', 'L');
}
$pdf->SetFont('Arial', 'B', $font_size);
$pdf->Ln(2 * $line_height);
$pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
$pdf->Cell(120, $line_height, $value['product'], '', '', 'L');
$pdf->Cell(40, $line_height, $value['ammount'], '', '', 'L');
$pdf->Cell(40, $line_height, $value['value'], '', '', 'L');
}
Thank you all!
override the method header() of FPDF in your class.
class PDF extends FPDF
{
public function Header() {
$this->Cell(0, 8, "HEADER TEXT", 0, 0, 'R');
}
}
This method will be called automatically by the FPDF while adding a new page.
I had this same problem trying to get a header on each page. This also connects to a MySql database. Here is what ended up working for me.
<?php
//PDF USING MULTIPLE PAGES
//FILE Originally CREATED BY: Carlos José Vásquez Sáez
//I fixed this on May 3/2013 as it was not working correctly for me.
define('FPDF_FONTPATH', '/font/');
require('fpdf.php');
//Connect to your database
$link = mysql_connect("localhost","","") or die ('Could not select database.');
$db_select = mysql_select_db("", $link) or die ('Could not select database.');
//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 = 10;
//Set Row Height
$row_height = 8;
//print column titles for the actual page
$pdf->SetFillColor(232, 232, 232);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetY($y_axis_initial);
$pdf->SetX(10);
$pdf->Cell(80, $row_height, 'Products', 1, 0, 'C', 1);
$pdf->Cell(20, $row_height, 'Price', 1, 0, 'C', 1);
$pdf->Cell(30, $row_height, 'Customer', 1, 0, 'C', 1);
//Select the Products you want to show in your PDF file
$result=mysql_query('SELECT Product,Price,Customer FROM your_table ORDER BY Product', $link);
//initialize counter
$i = 0;
//Set maximum rows per page
$max = 30;
//Data Table y axis position starting point
$y_axis = $y_axis_initial + $row_height;
while($row = mysql_fetch_array($result))
{
//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(10);
$pdf->Cell(80, $row_height, 'Product', 1, 0, 'C', 1);
$pdf->Cell(20, $row_height, 'Price', 1, 0, 'C', 1);
$pdf->Cell(30, $row_height, 'Customer', 1, 0, 'C', 1);
//Set $i variable to 0 (first row)
$i = 0;
//Reset the y axis value
$y_axis = $y_axis_initial + $row_height;;
}
$Product = $row['Product'];
$Price = $row['Price'];
$Cust = $row['Customer'];
$pdf->SetY($y_axis);
$pdf->SetX(10);
$pdf->Cell(80, $row_height, $Product, 1, 0, 'L', 1);
$pdf->Cell(20, $row_height, $Price, 1, 0, 'L', 1);
$pdf->Cell(30, $row_height, $Cust, 1, 0, 'L', 1);
//Go to next row
$y_axis = $y_axis + $row_height;
$i = $i + 1;
}
mysql_close($link);
//Create file
$pdf->Output();