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);
Related
I'm new in newer FPDF version to make change from old version to new version for repairing the PDF system in the newer server. When I've tried to make the text rotated and transparent, It errors that shows "Fatal error: Uncaught Exception: FPDF error: No page has been added yet in /var/www/html/pdf/fpdf/fpdf.php:271".
I tried to use a reference from http://www.fpdf.org/en/script/script2.php and http://www.fpdf.org/en/script/script74.php to make this.
So, i put this 3 classes in 1 file:
use setasign\FpdiProtection\FpdiProtection;
require_once('../../pdf/fpdf/fpdf.php');
require_once('../../pdf/fpdi/src/autoload.php');
require_once('../../pdf/fpdi-protection/src/autoload.php');
//require('../../pdf/fpdf/rotation.php');
require('../../pdf/fpdf/fpdf_merge.php');
//Transparent
class AlphaPDF extends FPDF
{
protected $extgstates = array();
function SetAlpha($alpha, $bm='Normal')
{
$gs = $this->AddExtGState(array('ca'=>$alpha, 'CA'=>$alpha, 'BM'=>'/'.$bm));
$this->SetExtGState($gs);
}
function AddExtGState($parms)
{
$n = count($this->extgstates)+1;
$this->extgstates[$n]['parms'] = $parms;
return $n;
}
function SetExtGState($gs)
{
$this->_out(sprintf('/GS%d gs', $gs));
}
function _enddoc()
{
if(!empty($this->extgstates) && $this->PDFVersion<'1.4')
$this->PDFVersion='1.4';
parent::_enddoc();
}
function _putextgstates()
{
for ($i = 1; $i <= count($this->extgstates); $i++)
{
$this->_newobj();
$this->extgstates[$i]['n'] = $this->n;
$this->_put('<</Type /ExtGState');
$parms = $this->extgstates[$i]['parms'];
$this->_put(sprintf('/ca %.3F', $parms['ca']));
$this->_put(sprintf('/CA %.3F', $parms['CA']));
$this->_put('/BM '.$parms['BM']);
$this->_put('>>');
$this->_put('endobj');
}
}
function _putresourcedict()
{
parent::_putresourcedict();
$this->_put('/ExtGState <<');
foreach($this->extgstates as $k=>$extgstate)
$this->_put('/GS'.$k.' '.$extgstate['n'].' 0 R');
$this->_put('>>');
}
function _putresources()
{
$this->_putextgstates();
parent::_putresources();
}
}
//Rotated
class PDF_Rotate extends FPDF
{
var $angle=0;
function Rotate($angle,$x=-1,$y=-1)
{
if($x==-1)
$x=$this->x;
if($y==-1)
$y=$this->y;
if($this->angle!=0)
$this->_out('Q');
$this->angle=$angle;
if($angle!=0)
{
$angle*=M_PI/180;
$c=cos($angle);
$s=sin($angle);
$cx=$x*$this->k;
$cy=($this->h-$y)*$this->k;
$this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
}
}
function _endpage()
{
if($this->angle!=0)
{
$this->angle=0;
$this->_out('Q');
}
parent::_endpage();
}
}
//Rotating Object
class PDF extends PDF_Rotate
{
function RotatedText($x,$y,$txt,$angle)
{
$this->Rotate($angle,$x,$y);
$this->Text($x,$y,$txt);
$this->Rotate(0);
}
}
$pdf = new PDF();
and it begins in $pdf = new PDF();
And this is the error part (in the same file which I put 3 classes) :
if ($height>=$width)
{$pdf->AddPage('P','A4');
$pdf->Image($imagefilePath,2,4,208,293);
$pdf = new AlphaPDF();
$pdf->SetAlpha(0.5);
$pdf = new PDF();
$pdf->SetFont('Arial', '',15);
$pdf->SetTextColor(255,0,0);
$pdf->RotatedText(105-90, 148.5-120,'CONTROLLED COPY',45);
$pdf->RotatedText(105-90, 148.5+7-120,$nmrdvl,45);
$pdf->RotatedText(105-25, 148.5,'CONTROLLED COPY',45);
$pdf->RotatedText(105-25, 148.5+7,$nmrdvl,45);
$pdf->RotatedText(105+35, 148.5+120,'CONTROLLED COPY',45);
$pdf->RotatedText(105+35, 148.5+7+120,$nmrdvl,45);
//$pdf->MultiCell(105,148.5,'CONTROLLED COPY'."\n ".$nmrdvl,0,'C','');
$pdf = new AlphaPDF();
$pdf->SetAlpha(1);
$pdf->SetTextColor(0,0,0);
$pdf = new PDF();
} else
{$pdf->AddPage('L','A4');
$pdf->Image($imagefilePath,2,4,293,208);
$pdf = new AlphaPDF();
$pdf->SetAlpha(0.5);
$pdf = new PDF();
$pdf->SetFont('Arial', '',15);
$pdf->SetTextColor(255,0,0);
$pdf->RotatedText(148.5-120, 105-85, 'CONTROLLED COPY',45);
$pdf->RotatedText(148.5-120, 105+7-85, $nmrdvl,45);
$pdf->RotatedText(148.5-25, 105, 'CONTROLLED COPY',45);
$pdf->RotatedText(148.5-25, 105+7, $nmrdvl,45);
$pdf->RotatedText(148.5+70, 105+85, 'CONTROLLED COPY',45);
$pdf->RotatedText(148.5+70, 105+7+85, $nmrdvl,45);
//$pdf->MultiCell(148.5,105,'CONTROLLED COPY'."\n ".$nmrdvl,0,'C','');
$pdf = new AlphaPDF();
$pdf->SetAlpha(1);
$pdf->SetTextColor(0,0,0);
$pdf = new PDF();
}
So, how should I do to solve this error and how do to make the text rotated and transparent at once using FPDF to make PDF file from PHP?
P.S : PHP version is 8.1.2 and FPDF version is 1.84
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);
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 );
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();
}
I would like to call a function when someone clicks on the pie-chart which I built using Highchart.
My pie chart code is:
function open_risk_level_pie()
{
$chart = new Highchart();
$chart->chart->renderTo = "open_risk_level_pie";
$chart->chart->plotBackgroundColor = lightblue;
$chart->chart->plotBorderWidth = null;
$chart->chart->plotShadow = false;
$chart->title->text = "Risk Level";
$chart->tooltip->formatter = new HighchartJsExpr("function() {
return '<b>'+ this.point.name +'</b>: '+ this.point.y; }");
$chart->plotOptions->pie->allowPointSelect = 1;
$chart->plotOptions->pie->cursor = "pointer";
$chart->plotOptions->pie->dataLabels->enabled = false;
$chart->plotOptions->pie->showInLegend = 1;
$chart->plotOptions->pie->colors = array('red', 'orange', 'yellow', 'black');
$chart->credits->enabled = false;
$array = //some db access code
$high = $array[0][0];
$medium = $array[1][0];
$low = $array[2][0];
// If the array is empty
if (empty($array))
{
$data[] = array("No Data Available", 0);
}
// Otherwise
else
{
// Create the data array
foreach ($array as $row)
{
$data[] = array($row['level'], (int)$row['num']);
}
$chart->series[] = array('type' => "pie",
'name' => "Level",
'data' => $data);
}
echo "<div id=\"open_risk_level_pie\"></div>\n";
echo "<script type=\"text/javascript\">";
echo $chart->render("open_risk_level_pie");
echo "</script>\n";
}
Now I want to call a function whenever someone clicks on the pie chart. I have searched a lot, but unable to find. Some sites mentioned to use "formatter", but I failed to use it. If formatter is the solution for my question please give me steps for the above code.
Use an on click event for a slice.
Docs.