Working on a project and need to generate pdf of the product details using FPDF. The product details are passed into an array and I need the following to get each of the variable elements in the array '$prod_details' into the functions within the class 'PDF' as shown below:
Examples of how I tried passing the variable array elements:
$this->Cell(30,8,$prod_data['prod_name'],0,0,'C');
$this->Cell(30,10,$prod_data['company_name']);
$this->Cell(20,8,$prod_data['prod_cost'],0,0,'C');
I have tried running this script but I keep getting an error message 'Cannot access empty property'...
find the codes below
<?php
#include_once("includes/db.php");
require('fpdf/fpdf.php');
#include_once("includes/class_product_info.php");
$obj = new allProducts();
$prod_data = array();
if(isset($_GET['c_id'])){
$prod_data = $obj->getProdDetails($_GET['c_id']);
class PDF extends FPDF
{
public $prod_data;
public function createData($input){
$this->prod_data = $input;
}
function Header()
{
// Logo
$this->Image('big_logo.png',10,6,30);
// Arial bold 15
$this->SetFont('Arial','B',20);
// Move to the right
$this->Cell(40);
// Title
$this->Cell(30,10,$this->prod_data['company_name']);
// Draw an header line
$this->Line(10,26,200,26);
// Line break
$this->Ln(20);
}
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Begin with regular font
$this->SetFont('Arial','',9);
// Then put a blue underlined link
//$this->SetTextColor(0,0,255);
$this->SetFont('','U');
$this->Write(10,$this->prod_data['company_name'],'http://www.skills.com');
// Arial italic 8
$this->SetFont('Arial','I',9);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().' ',0,0,'R');
}
function prodDetailTop()
{
// Course title cell
$this->Cell('',10,'',0,0,'C');
$this->Ln();
/* Build cells for the first row */
$this->SetFont('Arial','',10);
$this->SetY(40);
// First Row
$this->Cell(35,8,'Product Name : ',0,0,'L');
$this->Cell(30,8,$this->prod_data['prod_name'],0,0,'C');
$this->SetX(150);
$this->Cell(25,8,'Product Cost : ',0,0,'L');
$this->Cell(20,8,$this->prod_data['prod_cost'],0,0,'C');
$this->Ln();
// Second Row
$this->Cell(35,8,'Discount : ',0,0,'L');
$this->Cell(30,8,$this->prod_data['disc_amt'],0,0,'L');
$this->SetX(150);
$this->Cell(25,8,'No Purchased : ',0,0,'L');
$this->Cell(20,8,$this->prod_data['items_count'].' product(s)',0,0,'L');
$this->Ln();
}
function prodDetailBtm()
{
$this->SetY(80);
$this->Write(10,$this->prod_data['prod_desc']);
}
function generatePageData()
{
$this->AddPage();
$this->prodDetailTop();
$this->prodDetailBtm();
}
}
$pdf = new PDF();
$pdf->createData($prod_data);
//$pdf->Header();
$pdf->generatePageData();
$pdf->Output();
}
else {
?>
<script language="javascript">
window.location = "prod_invoice_err.php";
</script>
<?php
}
?>
Hope to get some help.
Your question is a little vague. It would be helpful it you asked specifically what you're trying to accomplish.
But the first thing I see is that your subclass of the fpdf class, you don't need to write functions to do each and everything you want to do with the pdf. You only need to extend the parts of the class you are overriding (or extending), like header and footer.
So extend it, manipulate header and footer, then close the class. Create your $pdf instance of your new fpdf class, then manipulate that object with your data. You don't need to 'pass in' that data at all.
for instance:
$pdf->Ln(10);
$pdf->Cell($w,9,$title,1,1,'C',true); //from fpdf tutorial
Or, if that doesn't accomplish what you want (although I can't see why it wouldn't, I've done this lots of times), you can always override the constructor. Pass in an array (or event a custom object that you create), and store that in a private variable.
Related
I have an issue with dompdf where it shows me strange padding all the time that should not be there.
Above is the image where it is happening where my html itself i just a simple 1 tag with some text in it.
I initiate the library as:
class OmnPdf extends \Dompdf\Dompdf
{
public function __construct($options = null)
{
if($options == null){
$options = new \Dompdf\Options();
}
$options->setIsPhpEnabled(true);
$options->setIsHtml5ParserEnabled(true); // For combining multiple pdf outputs
$options->setIsFontSubsettingEnabled(true);
$options->setDefaultPaperSize('A4');
$options->setDebugCss(true);
$options->setDebugLayout(true);
$options->setDpi(72);
parent::__construct($options);
}
}
What i am expecting is that the text will be alligned according the outer page (see red line).
Thanks,
Pim
Are you loading a custom font? Because I'm pretty sure this is causing your issue.
I'm calling these 3 functions one after other in this exact order
public function setPrintFitToWidth()
{
$this->sheet->getPageSetup()->setFitToWidth(1);
}
public function setPrintArea($cell_area)
{
$this->sheet->getPageSetup()->setPrintArea($cell_area);
}
public function setPrintMargins($top, $right, $bottom, $left)
{
$this->sheet->getPageMargins()->setTop($top);
$this->sheet->getPageMargins()->setRight($right);
$this->sheet->getPageMargins()->setLeft($left);
$this->sheet->getPageMargins()->setBottom($bottom);
}
The problem is that, opening resulting Excel file, I've page margin set to 'custom' but, in fact, set to different values instead of margin I passed to my function. In fact I called with argument (1,0.5,0.5,1) but I got, in the same orders, 2, 0.8, 0.8, 2. It's really strange ...
Also: I cannot get working setFittoWidth(1); I expect to see adapted for all column in one page, but Excel tell me It's setup on adapt sheet on a page.
What am I doing wrong?
Resolved:
changed
public function setPrintFitToWidth()
{
$this->sheet->getPageSetup()->setFitToWidth(1);
}
to
public function setPrintFitToWidth()
{
$this->sheet->getPageSetup()->setFitToWidth(1);
$this->sheet->getPageSetup()->setFitToHeight(0);
}
About the margins: I tried with zero and margin are respected, so I concluded than PHPExcel unit are in someway 'scaled down'... So, after some 'try' and 'redo', I found the values that generate the correct magins
I have a list of customers who need to get a dynamically generated pdf.
class PDF extends FPDF
{
// Page header
function Header()
{
global $backToTOC;
// Logo
$this->Image('logo.jpg',70,10);
$this->Write(5,'Back to TOC',$backToTOC);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
// 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(5,'Contact support: 1-800-support');
$this->Cell(5,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
foreach ($customer as $k => $v)
{
$pdf = new PDF();
$pdf->AliasNbPages();
//....pdf stuff.....
$pdf->Output($v.'.pdf','F');
}
the result of this is a divide by zero error.
PHP Warning: Division by zero in /var/www/lib/fpdf/fpdf.php on line 796
and the footer page numbers show 0. any thoughts?
The Cell method expects the second parameter to be the cell height, not the cell content (see Cell method). When calling:
$this->Cell(5,'Page '.$this->PageNo().'/{nb}',0,0,'C');
you are using 'Page '.$this->PageNo().'/{nb}' as the height and 0 as the content. It should be:
$this->Cell(5, $cellHeight, 'Page '.$this->PageNo().'/{nb}',0,0,'C');
Try setting your font right after creating the PDF object, it worked on my machine.
Also if it doesn't work, please paste a stacktrace.
I'm trying to create country based charts. i'm using pchart for this, because it will create charts as image by default.
I've created a function which creates barchart. I'm selecting countries and its stats and passing it to the barchart function which creates a chart as a flat file.
When i pass a single a country stats it is creating chart. the problem is when i try to post more than one country, it is creating only one country image not more than that. i don't know where the issue exists? Help appreciated.
sample code is here:
<?php
$con=mysql_connect("localhost","root","");
$ln=mysql_select_db("conif_new",$con);
$qry="select country from chart group by country limit 2";
$cnlist=mysql_query($qry);
while($row=mysql_fetch_row($cnlist)){
$cn=$row[0];
$nqry="select server,count(*) from chart where country='$cn' group by server";
$dset=mysql_query($nqry);
$x="";
$xt="";
while($crow=mysql_fetch_row($dset)){
$x.=$crow[1].",";
$xt.=$crow[0].",";
}
$x=substr($x,0,strlen($x)-1);
$xt=substr($xt,0,strlen($xt)-1);
//echo "$x**$xt<br>";
if(barChart($x,$xt,$cn)){}
}
function barChart($x,$xt,$name){
$x=explode(",",$x);
$xt=explode(",",$xt);
/* CAT:Bar Chart */
/* pChart library inclusions */
include("class/pData.class.php");
include("class/pDraw.class.php");
include("class/pImage.class.php");
/* Create and populate the pData object */
$MyData = new pData();
$MyData->addPoints($x,"Server A");
$MyData->setAxisName(0,"Hits");
$MyData->addPoints($xt,"Months");
$MyData->setSerieDescription("Months","Month");
$MyData->setAbscissa("Months");
/* Create the pChart object */
$myPicture = new pImage(700,230,$MyData);
$myPicture->drawGradientArea(0,0,700,230,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,
"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>100));
$myPicture->drawGradientArea(0,0,700,230,DIRECTION_HORIZONTAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,
"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>20));
$myPicture->setFontProperties(array("FontName"=>"fonts/verdana.ttf","FontSize"=>9));
/* Draw the scale */
$myPicture->setGraphArea(50,30,680,200);
$myPicture->drawScale(array("CycleBackground"=>TRUE,"DrawSubTicks"=>TRUE,"GridR"=>0,"GridG"=>0,"GridB"=>0,
"GridAlpha"=>10));
/* Turn on shadow computing */
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
/* Draw the chart */
$settings = array("Gradient"=>TRUE,"DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"DisplayR"=>255,
"DisplayG"=>255,"DisplayB"=>255,"DisplayShadow"=>TRUE,"Surrounding"=>10);
$myPicture->drawBarChart($settings);
/* Write the chart legend */
$myPicture->drawLegend(580,12,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));
/* Render the picture (choose the best way) */
//$myPicture->autoOutput("pictures/example.drawBarChart.shaded.png");
if($myPicture->render("C:/wamp/www/chart/".$name.".png"))
echo "true";
else
echo "false";
}
?>
I found an answer!!!, just noticed "include statements" inside barchart function. Removed it, Thats it!
I should've placed "include_once" instead of "include" or should've moved the "include" statements outside the function to make it work.
I'm wondering if there's a keep together function for TCPDF. I have one for FPDF, but I can't get it to work in TCPDF.
Here's how I see it working within the PDF generation code:
// ... PDF code/stuff
// while not kept together
// add PDF stuff that should be kept together
// .. more PDF code/stuff
I'm thinking the function would return false if the a new page was added, roll back and then do the while loop again.
I do have the following working, but I'd rather it was in a function/method of TCPDF so it was more reusable:
$pdf->startTransaction();
$block_page = $pdf->getPage();
$print_block = 2; // max 2 tries
while ($print_block > 0) {
// do PDF stuff
if ($pdf->getPage() == $block_page) {
$print_block = 0;
} else {
// rollback
$pdf = $pdf->rollbackTransaction();
$pdf->AddPage();
$block_page = $pdf->getPage();
-- $print_block;
}
}
It would also be cool if it didn't depend on the built in transaction functionality so transactions can be used within the loop, since things like writeHTML() use transactions.
I wanted similar functionality and settled on using transactions. This on TCPDF version 5.9.125.
I inherited my own PDF class from TCPDF and added my own method:
public function writeHTMLTogether($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='') {
$cp = $this->getPage();
$this->startTransaction();
$this->writeHTML($html, $ln, $fill, $reseth, $cell, $align);
if ($this->getPage() > $cp) {
$this->rollbackTransaction(true);//true is very important
$this->AddPage();
$this->writeHTML($html, $ln, $fill, $reseth, $cell, $align);
} else {
$this->commitTransaction();
}
}
Seemed to work fine. Without the true in the rollback it breaks horribly, as writeHTML seems to store lots of properties somewhere.
May not need to create a local variable for current page ($cp) as I think it's stored. But hey.
If you're inheriting to write your own Header and Footer functions anyway, not much extra work.