i want to result data to table - php

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();
}

Related

Problem with Columns of SQL table on PHP FPDF

I've been having trouble creating a PDF using FPDF with PHP. I managed to fetch the data from my database and make the headers get right, but when the data gets posted it gets all chaotic, the script first puts all results from one column before starting the rest, making the positioning get really wrong.
I am using php 7
Here is one example of how data is being shown:
Column Error 1
Error 2
I can't understand why the first column is shown with the right Y position but all the other ones get like this
Here is the code as is.
$result = $mysqli->query("SELECT * FROM `tb_login`");
$number_of_rows = mysqli_num_rows($result);
//Inicializar as colunas
$column_idlogin = "";
$column_iduser = "";
$column_nome = "";
$column_snome = "";
$column_cpf = "";
$column_email = "";
$column_datahr = "";
//Adicionar a coluna para cada linha presente
while($row = mysqli_fetch_array($result))
{
$idlogin = $row["idLogin"];
$iduser = $row["idUsuario"];
$nome = $row["nmUsuario"];
$snome = $row["sobrenomeUsuario"];
$cpf = $row["cpfUsuario"];
$email = $row["emailUsuario"];
$datahr = $row["data_hora"];
$column_idlogin = $column_idlogin.$idlogin."\n";
$column_iduser = $column_iduser.$iduser."\n";
$column_nome = $column_nome.$nome."\n";
$column_snome = $column_snome.$snome."\n";
$column_cpf = $column_cpf.$cpf."\n";
$column_email = $column_email.$email."\n";
$column_datahr = $column_datahr.$datahr."\n";
}
$mysqli -> close();
//Create a new PDF file
$pdf=new FPDF();
$pdf->AddPage();
//Fields Name position
$Y_Fields_Name_position = 12;
//Table position, under Fields Name
$Y_Table_Position = 24;
//First create each Field Name
//Gray color filling each Field Name box
$pdf->SetFillColor(232,232,232);
//Bold Font for Field Name
$pdf->SetFont('Arial','B',10);
$pdf->SetY($Y_Fields_Name_position);
$pdf->SetX(20);
$pdf->Cell(18,12,'ID_LOGIN',1,0,'L',1);
$pdf->SetX(38);
$pdf->Cell(18,12,'ID_USER',1,0,'L',1);
$pdf->SetX(56);
$pdf->Cell(20,12,'NOME',1,0,'C',1);
$pdf->SetX(76);
$pdf->Cell(28,12,'SOBRENOME',1,0,'C',1);
$pdf->SetX(104);
$pdf->Cell(30,12,'CPF',1,0,'C',1);
$pdf->SetX(134);
$pdf->Cell(30,12,'EMAIL',1,0,'C',1);
$pdf->SetX(164);
$pdf->Cell(34,12,'DATA_ACESSO',1,0,'C',1);
$pdf->Ln();
//Agora mostre as colunas
$pdf->SetFont('Arial','',10);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(20);
$pdf->MultiCell(18,12,$column_idlogin,1);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(38);
$pdf->MultiCell(18,12,$column_iduser,1);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(56);
$pdf->MultiCell(20,12,$column_nome,1);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(76);
$pdf->MultiCell(28,12,$column_snome,1);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(104);
$pdf->MultiCell(30,12,$column_cpf,1);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(134);
$pdf->MultiCell(30,12,$column_email,1);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(164);
$pdf->MultiCell(34,12,$column_datahr,1);
$pdf->Output()
I tried declaring Sety as $Y_Table_Position for each column, but it doesn't seem to work.
I never used this library and didn't test this code so take it with a grain of salt.
I used your one cell working example and tried to iterate on it.
I cleaned up a bit of your code.
That while loop was strange.
//Create a new PDF file
$pdf=new FPDF();
$pdf->AddPage();
//First create each Field Name
//Gray color filling each Field Name box
$pdf->SetFillColor(232,232,232);
//Bold Font for Field Name
$pdf->SetFont('Arial','B',10);
$pdf->SetY(12);
$pdf->SetX(20);
$pdf->Cell(18,12,'ID_LOGIN',1,0,'L',1);
$pdf->SetX(38);
$pdf->Cell(18,12,'ID_USER',1,0,'L',1);
$pdf->SetX(56);
$pdf->Cell(20,12,'NOME',1,0,'C',1);
$pdf->SetX(76);
$pdf->Cell(28,12,'SOBRENOME',1,0,'C',1);
$pdf->SetX(104);
$pdf->Cell(30,12,'CPF',1,0,'C',1);
$pdf->SetX(134);
$pdf->Cell(30,12,'EMAIL',1,0,'C',1);
$pdf->SetX(164);
$pdf->Cell(34,12,'DATA_ACESSO',1,0,'C',1);
$pdf->Ln();
//Agora mostre as colunas
$pdf->SetFont('Arial','',10);
$result = $mysqli->query("SELECT * FROM `tb_login`");
$number_of_rows = mysqli_num_rows($result);
//Lets just create array of rows first
$rows = [];
while($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
/**
* Custom cell width config
*/
$cells_config_arr = [
'idLogin' => ['x' => 20],
'idUsuario' => ['x' => 20],
'nmUsuario' => ['x' => 20],
'sobrenomeUsuario' => ['x' => 20],
'cpfUsuario' => ['x' => 20],
'emailUsuario' => ['x' => 20],
'data_hora' => ['x' => 20]
];
$cell_y = 24;
foreach($rows as $row){
$cell_x = 0;
foreach($row as $key => $value){
//Lets set coord of by with of last cells sumed
$cell_x = $cell_x + $cells_config_arr[ $key ]['x'];
$pdf->SetY($cell_y);
$pdf->SetX($cell_x);
$pdf->Cell($cells_config_arr[ $key ]['x'], 12, $value, 1, 0, 'L', 1);
}
$pdf->Ln();
$cell_y = $cell_y + $cell_y;
}
$pdf->Output();

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 );

PDF Columns not wrapping width to new line

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

Table MultiCell in FPDF

I am currently using FPDF to output data into a table, however i am running into issues with table cell wrapping.
My current table function is below:
function ImprovedTableCol2($header, $data, $cols ,$colWidth1, $colWidth2, $tableHeader, $closeTable, $fontSize, $icon)
{
// Table Header
if ($tableHeader==true) {
$this->SetFontSize(12);
$this->Cell(90,7,$header[0],1,0,'L');
$this->Cell(90,7,$header[1],1,0,'L');
$this->Ln();
}
// Table body
$this->SetFontSize($fontSize);
$this-> MultiCell(90,6,$data[0],'LRB');
$this->MultiCell(90,6,$data[1],'LRB');
$this->Ln();
}
My table page is as follows:
$pdf->AddPage();
$pdf->SetFont('Arial','B');
$pdf->SetFontSize(18);
$pdf->Cell(0,10,'Method Statement','B',1,'L');
$pdf->SetFont('');
$pdf->SetFontSize(10);
$pdf->Ln();
$header = array('', '');
$intTotalSCRows = "{method_statement:total_rows}";
$arrSafetyCheck = array();
array_push($arrSafetyCheck, array(
"day" => "{method_statement:day}",
"statement" => "{method_statement:statement}",
"engineer" => "{method_statement:engineer}",
"count" => "{method_statement:count}")
);
foreach ($arrSafetyCheck as $key => $list) {
if ($list['count']=="1") {
$data = array(utf8_decode($list['day']), $list['statement']);
$pdf->ImprovedTableCol2($header,$data,2,130,50,true,false,9,false);
} else if ($list['count']==$intTotalSCRows) {
$data = array(utf8_decode($list['day']), $list['statement']);
$pdf->ImprovedTableCol2($header,$data,2,130,50,false,true,9,false);
} else {
$data = array(utf8_decode($list['day']), $list['statement']);
$pdf->ImprovedTableCol2($header,$data,2,130,50,false,false,9,false);
}
}
The MultiCell function does wrap the text, but i cannot get the 2 table columns to sit side by side.
You will need to manually set the position of the cell (see the updated method below)
function ImprovedTableCol2($header, $data, $cols ,$colWidth1, $colWidth2, $tableHeader, $closeTable, $fontSize, $icon)
{
// Table Header
if ($tableHeader==true) {
$this->SetFontSize(12);
$this->Cell(90,7,$header[0],1,0,'L');
$this->Cell(90,7,$header[1],1,0,'L');
$this->Ln();
}
// Table body
$this->SetFontSize($fontSize);
// Get X,Y coordinates
$x = $this->GetX();
$y = $this->GetY();
$this->MultiCell(90,6,$data[0],'LRB');
// update the X coordinate to account for the previous cell width
$x += 90;
// set the XY Coordinates
$this->SetXY($x, $y);
$this->MultiCell(90,6,$data[1],'LRB');
$this->Ln();
}

Categories