Table MultiCell in FPDF - php

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

Related

Database Details in PDF are not getting filled?

I am extracting data from MySQL database using php and exporting it as PDF. The columns are not getting filled due to the PDF page size. How do I push the unfilled columns to next line? So that first line has 4 columns and next line has another 4 columns, so on..
<?php
//include connection file
include_once "connection.php";
include_once 'fpdf/fpdf.php';
class PDF extends FPDF {
// Page header
function Header() {
}
// 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');
}
}
$db = new dbObj();
$connString = $db->getConnstring();
$id = $_GET['id'];
$query = $connString->prepare("SELECT ID, Name, Wrongs, Rights, Percentage, Age FROM Datas WHERE ID=?");
$query->bind_param('s', $id);
$query->execute();
$result = $query->get_result();
$display_heading = array('ID' => 'ID', 'Name' => 'Name', 'Wrongs' => 'Wrongs', 'Rights' => 'Rights', 'Percentage' => 'Percentage', 'Age' => 'Age');
//$result = mysqli_query($connString, "SELECT ID, Name, Wrongs, Rights, Percentage, Age FROM Datas") or die("database error:". mysqli_error($connString));
$header = mysqli_query($connString, "SHOW columns FROM Datas");
$pdf = new PDF();
//header
$pdf->AddPage();
//foter page
$pdf->AliasNbPages();
$pdf->SetFont('Arial', 'B', 12);
foreach ($header as $heading) {
$pdf->Cell(45, 12, $display_heading[$heading['Field']], 1);
}
foreach ($result as $row) {
$pdf->Ln();
foreach ($row as $column) {
$pdf->Cell(45, 12, $column, 1);
}
}
$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);

fpdf display image in table row using table with multilcell

I am having trouble displaying image using fpdf library. I use fpdf Table with MultiCells scripts [http://www.fpdf.org/?go=script&id=3 ].
I can display data by calling this function
$data = [
['A','B','C','image_path'],
['A','B','C','image_path'],
['A','B','C','image_path'],
];
$pdf->Row($header);
foreach($data as $v) $pdf->Row($v);
It will generate data nicely. I want to replace image_path with image . How can I display image using this script?
My pdf will be look like this.
Modify PDF_MC_Table class Row() functions by using Inhertence.
Class Pdf extends PDF_MC_Table{
protected $imageKey = '';
public function setImageKey($key){
$this->imageKey = $key;
}
public function Row($data){
$nb=0;
for($i=0;$i<count($data);$i++)
$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
$h=5*$nb;
$this->CheckPageBreak($h);
for($i=0;$i<count($data);$i++){
$w=$this->widths[$i];
$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
$x=$this->GetX();
$y=$this->GetY();
$this->Rect($x,$y,$w,$h);
//modify functions for image
if(!empty($this->imageKey) && in_array($i,$this->imageKey)){
$ih = $h - 0.5;
$iw = $w - 0.5;
$ix = $x + 0.25;
$iy = $y + 0.25;
$this->MultiCell($w,5,$this->Image ($data[$i],$ix,$iy,$iw,$ih),0,$a);
}
else
$this->MultiCell($w,5,$data[$i],0,$a);
$this->SetXY($x+$w,$y);
}
$this->Ln($h);
}
}
}
Now call this function like this
$data = [
['A','B','C','image_path'],
['A','B','C','image_path'],
['A','B','C','image_path'],
];
$pdf = new Pdf();
$pdf->AddPage();
$pdf->setImageKey = [4];
$pdf->Row($data);

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

FPDF Getting page numbers of a 1 or 2-Sided form letter

I create a form letter from an order table. Each order can have either 1 or 2 pages. The PDF contains all orders. Now I want to put the page numbers for every order on the PDF.
First Order: Pages 1 and 2,
Second Order: Page 3,
Third Order: Page 4.
The number of pages depends on how many articles a customer ordered (max 2 pages).
PageNo() uses the whole document for numbering. Maybe someone had the same problem?
The expected result can be achieved by subclassing FPDF's FPDF class, and adding an $orderPageNo property to keep track of the current order's "sub-page number". You can then use that property in your customized Header or Footer method.
Example:
<?php
require('fpdf/fpdf.php');
class PDF extends FPDF {
protected $orderNumber, $orderPageNo;
function AcceptPageBreak() {
$accept = parent::AcceptPageBreak();
if ($accept) {
$this->orderPageNo++;
}
return $accept;
}
function Header() {
$this->Cell(50, 30, 'Order #'.$this->orderNumber);
$this->Cell(50, 30, 'Page '.$this->orderPageNo, 0, 1);
}
function Order($orderNumber, $items) {
$this->orderNumber = $orderNumber;
$this->orderPageNo = 1;
$this->AddPage();
for ($i = 1; $i <= $items; $i++) {
$this->Cell(30, 12, 'Item #'.$i, 1, 1);
}
}
}
$pdf = new PDF();
$pdf->SetFont('Arial', '', 12);
$orders = array(1 => 15, 2 => 25, 3 => 35);
foreach ($orders as $orderNumber => $items) {
$pdf->Order($orderNumber, $items);
}
$pdf->Output();

Categories