PDF Columns not wrapping width to new line - php

I have succeeded in outputting a pdf and the width of the email and address column is overlapping into the next column. I need help on how to make it print the results from the database in a new line even though the word length is longer than the column width, it should just break and extend the height of the entire row.
Below is my code;
<?php
require('fpdf.php');
$db = new PDO('mysql:host=localhost;dbname=ddname;charset=utf8', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$colNames = array();
$result = $db->query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='dbname' AND TABLE_NAME='table_name'");
foreach($result as $row) {
$colNames[] = $row["COLUMN_NAME"];
}
$pdf = new FPDF('L','mm','A4');
$pdf->AddPage();
$pdf->SetDisplayMode(real,'default');
$pdf->SetFont('Arial','B',12);
$pdf->SetTextColor(50,60,100);
$pdf->SetDrawColor(50,60,100);
$pdf->Cell(100,10,'Tabledata table_name',1,0,'C',0);
$pdf->Ln();
foreach($colNames as $colName) {
$pdf->SetFont('Arial','B',8);
if($colName=="id")
{
$pdf->Cell(6,10,$colName,1);
} elseif($colName=="email_address"){
$pdf->Cell(22,10,$colName,1);}elseif($colName=="title"){
$pdf->Cell(10,10,$colName,1);}else
{
$pdf->Cell(22,10,$colName,1);
}
}
foreach($db->query("SELECT * FROM table_name") as $row2) {
$pdf->Ln();
foreach($colNames as $colName) {
$pdf->SetFont('Arial','',8);
if($colName=="id")
{
$pdf->Cell(6,10,$row2[$colName],1);
} elseif($colName=="title"){
$pdf->Cell(10,10,$row2[$colName],1);}else
{
$pdf->Cell(22,10,$row2[$colName],1);
}
}
}
$pdf->Output();
?>
This is a sample of the current result

Related

i want to result data to table

why fill the table sideways?
I want to go down, help plis :D
Controller
public function cetak_inbound($id){
$pdf = new FPDF('L','mm','A4');
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->SetX(60);
$Y_Fields_Name_position = 15;
$pdf->SetY($Y_Fields_Name_position);
$pdf->SetX(10);
$pdf->Image('http://localhost/race/assets/frontend/img/logo-race.png',10);
$pdf->ln(30);
$cetakinb = $this->M_inbound->cetak_inbound($id);
foreach ($cetakinb->result() as $row){
$pdf->Cell(40,6,$row->kd_inbound,1,0);
$pdf->Cell(25,6,$row->tgl_inbound,1,0);
$pdf->Cell(67,6,$row->reciver_name,1,0);
}
$pdf->Output();
}
Model
public function cetak_inbound($id){
$hasil=$this->db->query("SELECT nama_cs,kd_inbound,tgl_inbound,reciver_name,inbound.status FROM inbound INNER JOIN inbound_detail ON inbound.id_inbound=inbound_detail.id_inbound INNER JOIN service ON service.id_service=inbound_detail.id_service INNER JOIN customers ON customers.id_cs=service.id_cs WHERE inbound.id_inbound='$id'");
return $hasil;
}
You could simply add $pdf->Ln() on each of the last column like this :
public function cetak_inbound($id){
$pdf = new FPDF('L','mm','A4');
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->SetX(60);
$Y_Fields_Name_position = 15;
$pdf->SetY($Y_Fields_Name_position);
$pdf->SetX(10);
$pdf->Image('http://localhost/race/assets/frontend/img/logo-race.png',10);
$pdf->ln(30);
$cetakinb = $this->M_inbound->cetak_inbound($id);
foreach ($cetakinb->result() as $row){
$pdf->Cell(40,6,$row->kd_inbound,1,0);
$pdf->Cell(25,6,$row->tgl_inbound,1,0);
$pdf->Cell(67,6,$row->reciver_name,1,0);
$pdf->Ln();
}
$pdf->Output();
}

Header columns are breaking in PDF file using PHP and FPDF

I am facing one issue while producing PDF file using PHP and FPDF. My code is below:
require_once('/var/www/hlwcab.com/public_html/cab/service/sms/smsAPIService.php');
class PDF extends FPDF{
// Page header
function Header(){
// Logo
$this->Image('../../../admin/img/HLW-CabManagementLogopdf.png',10,-1,30);
$this->SetFont('Arial','B',13);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(80,10,'Report List',1,0,'C');
// Line break
$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');
}
}
if ($_REQUEST["action"]=="downloadMonthlyReportData") {
$data=array();
$start_time=$_POST['start_time'];
$end_time=$_POST['end_time'];
$sql="select * from cb_wallet_driver_logs order by id desc";
$stmt = $db->prepare($sql);
if ($stmt->execute()) {
$row = $stmt->fetchAll();
$number_of_rows= count($row);
if ($number_of_rows > 0) {
$slno=1;
foreach ($row as $key => $value) {
$last_update=$value['last_update'];
$owner_id=$value['owner_id'];
$sql="select * from cb_owner_info where owner_id=:owner_id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':owner_id', htmlspecialchars(strip_tags($owner_id)));
if ($stmt->execute()) {
$orow = $stmt->fetchAll();
$number_of_orows= count($orow);
if ($number_of_orows > 0) {
foreach ($orow as $key => $val) {
$owner_name=$val['name'];
}
}
}
$dt = new DateTime($last_update);
$odate=$dt->format('d-m-Y');
$ndate=date('d-m-Y',strtotime($odate));
$stdate=date('d-m-Y',strtotime($start_time));
$endate=date('d-m-Y',strtotime($end_time));
if ($ndate >= $stdate && $ndate <= $endate) {
$data[]=array("sl_no"=>$slno,"owner_name"=>$owner_name,"last_update"=>$last_update,"book_id"=>$value['book_id'],"operator_total"=>$value['operator_total'],"ride_total"=>$value['ride_total']);
$slno++;
}
}
}
}
$result1=array();
if (count($data) > 0) {
$pdf = new PDF();
//$pdf = new FPDF();
//header
$pdf->AddPage();
$width_cell=array(10,30,40,40,30,30);
$pdf->SetFont('Arial','B',12);
$pdf->SetFillColor(193,229,252);
$pdf->Cell($width_cell[0],10,'Sl No',1,0,C,true);
$pdf->Cell($width_cell[1],10,'Owner Name',1,0,C,true);
$pdf->Cell($width_cell[2],10,'Date',1,0,C,true);
$pdf->Cell($width_cell[3],10,'Booking ID',1,0,C,true);
$pdf->Cell($width_cell[4],10,'Operator',1,1,C,true);
$pdf->Cell($width_cell[5],10,'Ride',1,1,C,true);
$pdf->SetFont('Arial','',10);
//Background color of header//
$pdf->SetFillColor(235,236,236);
//to give alternate background fill color to rows//
$fill=false;
foreach ($data as $row) {
$pdf->Cell($width_cell[0],10,$row['sl_no'],1,0,C,$fill);
$pdf->Cell($width_cell[1],10,$row['owner_name'],1,0,L,$fill);
$pdf->Cell($width_cell[2],10,$row['last_update'],1,0,C,$fill);
$pdf->Cell($width_cell[3],10,$row['book_id'],1,0,C,$fill);
$pdf->Cell($width_cell[4],10,$row['operator_total'],1,1,C,$fill);
$pdf->Cell($width_cell[5],10,$row['ride_total'],1,1,C,$fill);
$fill = !$fill;
}
$today = date('YmdHi');
$filename='report_'.$today.'.pdf';
$totalpath='/var/www/hlwcab.com/public_html/cab/service/admin/report/download/'.$filename;
$pdf->Output($totalpath, "F");
$result1["msg"] = "Succesfully Downloaded...";
}else{
header("HTTP/1.0 401 Unauthorized");
$result1["msg"] = "No record to download...";
}
//print_r($result1);exit;
echo json_encode($result1);
}
Here I am first fetching the data from MySQL table and creating the pdf file which is given below.
Here the last header column is breaking and coming to down. Here I need to set all header part of column in the same row.
Change the 2nd one (1) in the line below to zero (0).
From:
pdf->Cell($width_cell[4],10,'Operator',1,1,C,true);
To:
pdf->Cell($width_cell[4],10,'Operator',1,0,C,true);
Do the same for the cell in the loop that outputs the actual values. From:
pdf->Cell($width_cell[4],10,$row['operator_total'],1,1,C,$fill);
To:
pdf->Cell($width_cell[4],10,$row['operator_total'],1,0,C,$fill);

How to change font color for data in database to print in pdf like for absent=red color and present=green?

I want to change the font color of the text in PDF to absent is to red and present is to green. How can i add those in these codes? Need help for a beginner. Thanks alot.
Btw, I used fpdf to make the output a downloadable pdf.
<?php
//include connection file
include "db.php";
include_once('fpdf.php');
class PDF extends FPDF
{
// Page header
function Header()
{
// Logo
// $this->Image('https://i2.wp.com/tutorialswebsite.com/wp-content/uploads/2016/01/cropped-LOGO-1.png',10,10,50);
$this->SetFont('Arial','B',13);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(80,10,'ICT-5 Attendance',1,0,'C');
// Line break
$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');
}
}
Should i add that in here?
$display_heading = array('roll_number'=>'Roll Number', 'student_name'=> 'Name of Student', 'attendance_status'=> 'Attendance Status','id'=> 'DATABASE ID Num.','date'=> 'Date');
$result = mysqli_query($conn, "SELECT id, student_name, roll_number, attendance_status, date FROM attendance_records WHERE date ='$_POST[date]'") or die("database error:". mysqli_error($conn));
$header = mysqli_query($conn, "SHOW columns FROM attendance_records WHERE field != 'created_on'");
$pdf = new PDF();
//header
$pdf->AddPage();
//foter page
$pdf->AliasNbPages();
$pdf->SetFont('Arial','B',10);
foreach($header as $heading) {
$pdf->Cell(39,10,$display_heading[$heading['Field']],1);
}
foreach($result as $row) {
$pdf->SetFont('Arial','I',8.5);
$pdf->Ln();
foreach($row as $column)
$pdf->Cell(39,10,$column,1);
}
$pdf->Output();
?>
I'm not sure about this but perhaps it will help. I've never used fPDF but a quick look at the documentation suggests that SetTextColor would be the method you need.
foreach( $result as $row ) {
$pdf->SetFont('Arial','I',8.5);
$pdf->Ln();
foreach($row as $column) {
switch( $row['attendance_status'] ){
case 'Absent': $pdf->SetTextColor(255,0,0); break;
case 'Present':$pdf->SetTextColor(0,255,0);break;
}
$pdf->Cell(39,10,$column,1);
}
}
To change ( hopefully ) just the word Absent/Present try this perhaps:
foreach( $result as $row ) {
$pdf->SetFont('Arial','I',8.5);
$pdf->Ln();
foreach($row as $fieldname => $column) {
if( $fieldname=='attendance_status' ){
switch( $row['attendance_status'] ){
case 'Absent': $pdf->SetTextColor(255,0,0); break;
case 'Present':$pdf->SetTextColor(0,255,0);break;
}
} else {
$pdf->SetTextColor(0,0,0); #default to BLACK
}
$pdf->Cell(39,10,$column,1);
}
}

FPDF: How to limit the fetch data so footer will not add to next page

How can I add a limitation on the output so that the footer will not go to next page?
class myPDF extends FPDF{
function header(){
$dates = new DateTime();
$date2 = $dates->format('Y');
$date=$_POST['month'];
$this->SetFont('Arial','B',14);
$this->Cell(189,5,'ABSTRACT OF COMMUNITY TAX CERTIFICATES',0,0,'C');
$this->Ln();
$this->SetFont('Times','',13);
$this->Cell(189,10,'For the month of '.$date.' '.$date2,0,0,'C');
$this->Ln(20);
}
function headerTable(){
$this->SetTextColor(136,0,0);
$this->SetFont('Times','B',10);
$this->Cell(30,8,'Date',1,0,'C');
$this->Cell(30,8,'CTC No.',1,0,'C');
$this->Cell(60,8,'Name',1,0,'C');
$this->Cell(23,8,'Individual',1,0,'C');
$this->Cell(23,8,'Corporation',1,0,'C');
$this->Cell(24,8,'Total',1,0,'C');
$this->Ln();
}
function viewTable($db){
$this->SetTextColor(0,0,0);
$this->SetFont('Times','',10);
$name = $_SESSION['name'];
$repnumber=$_POST['id'];
$stmt = $db->query("select * from tblctcab where cedrepnum='$repnumber' and username='$name' order by repdate asc");
$count=0;
while($data = $stmt->fetch(PDO::FETCH_OBJ)){
$this->Cell(30,8,$data->repdate,1,0,'C');
$this->Cell(30,8,$data->ctcnumber,1,0,'C');
$this->Cell(60,8,$data->cedlname.', '.$data->cedfname,1,0,'C');
$this->Cell(23,8,$data->totalampa,1,0,'C');
$this->Cell(23,8,'',1,0,'C');
$this->Cell(24,8,$data->totalampa,1,0,'C');
$this->Ln();
}
$stmts = $db->query("select * from tblctcc where cedrepn='$repnumber' and username='$name' order by rpdate asc");
$count=0;
while($dat = $stmts->fetch(PDO::FETCH_OBJ)){
$this->Cell(30,8,$dat->rpdate,1,0,'C');
$this->Cell(30,8,$dat->ctcnum,1,0,'C');
$this->Cell(60,8,$dat->comfname,1,0,'C');
$this->Cell(23,8,'',1,0,'C');
$this->Cell(23,8,$dat->totalampas,1,0,'C');
$this->Cell(24,8,$dat->totalampas,1,0,'C');
$this->Ln();
}
$dbs=new mysqli('localhost' , 'root' , '', 'tres');
$repdate=$_POST['id'];
$ctcab = $dbs->query("select sum(totalampa) as totalampa from tblctcab where cedrepnum='$repnumber' and username='$name'");
$data = $ctcab->fetch_assoc();
$totalab = $data['totalampa'];
$ctcc = $dbs->query("select sum(totalampas) as totalampas from tblctcc where cedrepn='$repnumber' and username='$name'");
$datac = $ctcc->fetch_assoc();
$totalcc = $datac['totalampas'];
$overall = $totalab + $totalcc;
$this->SetTextColor(136,0,0);
$this->SetFont('Times','B',10);
$this->Cell(120,8,$_POST['id'],1,0,'C');
//$this->Cell(55,8,'TOTAL',1,0,'C');
$this->Cell(23,8,number_format($totalab),1,0,'C');
$this->Cell(23,8,number_format($totalcc),1,0,'C');
$this->Cell(24,8,number_format($totalab),1,0,'C');
$this->Ln();
$this->SetTextColor(0,0,0);
$this->SetFont('Times','',10);
$this->Cell(95,8,'',0,0,'C');
$this->Cell(95,8,'',0,0,'C');
$this->Ln();
$this->Cell(95,5,'Prepared By:',0,0,'L');
$this->Cell(95,5,' Certified By:',0,0,'L');
$this->Ln();
$this->SetFont('Times','',10);
$this->Cell(95,5,$_SESSION['name'],0,0,'C');
$this->Cell(95,5,'CONNIE',0,0,'C');
$this->Ln();
$this->Cell(95,5,$_SESSION['position'],0,0,'C');
$this->Cell(95,5,'Treasurer',0,0,'C');
$this->Ln();
}
function footer(){
$this->SetY(-15);
$this->SetFont('Arial','',8);
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'R');
}
}
$pdf = new myPDF();
$pdf->AliasNbPages();
$pdf->AddPage('','A4',0);
$pdf->headerTable();
$pdf->viewTable($db);
$pdf->Output();

How to display images in pdf using fpdf library?

Currently I am working on opencart. And I have to display data in pdf for that I use FPDF.
my function looks like
public function exportPDF(){
require_once(DIR_SYSTEM . 'lib/fpdf.php');
$pdf = new fpdf();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
$category_id = $this->request->get['id'];
$this->load->model('catalog/product');
$this->load->model('tool/image');
$temp_data = $this->model_catalog_product->getProductscsv($category_id);
foreach($temp_data as $data)
{
if ($data['image']) {
$image = $this->model_tool_image->resize($data['image'], 178, 276);
} else {
$image = $this->model_tool_image->resize('placeholder.png', 178, 276);
}
$data2[] = array(
'product_id' =>$data['product_id'],
'model' =>$data['model'],
'name' =>$data['name'],
'price' =>$data['price'],
'image' =>$image,
);
}
$row = array();
$pdf->SetFont('Arial','',12);
$pdf->Ln();
$pdf->Cell(35,10,'id',1);
$pdf->Cell(35,10,'model',1);
$pdf->Cell(35,10,'name',1);
$pdf->Cell(35,10,'price',1);
$pdf->Cell(35,10,'image',1);
foreach($data2 as $row)
{
$pdf->SetFont('Arial','',10);
$pdf->Ln();
foreach($row as $column)
$pdf->Cell(35,50,$column,1);
}
$pdf->Output();
}
And current output pdf looks like:
My need is I need to display the images in image column instead of link. how can it make possible. I am new to this, and trying for long time.
How to use $pdf->Image(); in the $data2 array. How to display images in image column in the pdf.
Try this,
foreach($data2 as $row)
{
$pdf->SetFont('Arial','',10);
$pdf->Ln();
foreach($row as $key=>$column)
{
if($key == "image"){
$pdf->Cell(35,50,$this->Image($column,$this->GetX(),$this->GetY()),1);
}else{
$pdf->Cell(35,50,$column,1);
}
}
}
and also read this : reference
Try this code
$this->Cell( 35,10, $pdf->Image($image, $pdf->GetX(), $pdf->GetY()), 0, 0, 'L', false );

Categories