Jezza22 Posted April 28, 2009 Share Posted April 28, 2009 I have found a FPDF Class script that produces a basic invoice pdf document and I am trying to work out how to pass data to the LineItems() section. I believe that you achieve this through the public function __construct, but I am not sure how? The data that I want to pass to LineItems also needs to be an Array, because I need to pass multiple lines to it. So I neet to :- 1. How do I create an Array like the following: - $data[] = array("Product Line 1", 1, 1750, 112.50, 862.50); $data[] = array("Product Line 2", 1, 750, 112.50, 862.50); 2. How can I pass this Array to the following Class: - class InvoicePDF extends FPDF { private $PG_W = 190; public function __construct() { parent::__construct(); $this->LineItems(); } public function Header() { $this->SetFont('Arial', 'B', 16); $this->Cell($this->PG_W, 8, "True Track Software Ltd", 0, 0, 'C'); $this->Ln(); $this->Cell($this->PG_W, 5, "INVOICE", 0, 0, 'L'); $this->Ln(10); $this->SetFont('Arial', 'B', 10); $this->Cell(165, 5, "Account:", 0, 0, 'R'); $this->Cell(0, 5, "SHA001", 0, 0, 'R'); $this->Ln(); $this->Cell(165, 5, "Invoice No.:", 0, 0, 'R'); $this->Cell(0, 5, "1", 0, 0, 'R'); $this->Ln(); $this->Cell(165, 5, "Date:", 0, 0, 'R'); $this->Cell(0, 5, date("d/m/Y", time()), 0, 0, 'R'); $this->Ln(10); } public function LineItems($value) { $header = array("Description", "Qty.", "Unit", "VAT", "Total"); // Data $textWrap = str_repeat("This is a word wrap test ", 1); $textNoWrap = "There will be no wrapping here thank you"; for($i = 0; $i < count($data); $i++) { $data[] = array("Product Line 1", 1, 1750, 112.50, 862.50); $data[] = array("Product Line 2", 1, 750, 112.50, 862.50); } } /* Layout */ $this->SetDataFont('Arial', 'B', 12); $this->AddPage(); // Headers and widths $w = array(85, 25, 20, 20, 20, 20); for($i = 0; $i < count($header); $i++) { $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C'); } $this->Ln(); // Mark start coords $x = $this->GetX(); $y = $this->GetY(); $i = 0; foreach($data as $row) { $y1 = $this->GetY(); $this->MultiCell($w[0], 6, $row[0], 'LRB'); $y2 = $this->GetY(); $yH = $y2 - $y1; $this->SetXY($x + $w[0], $this->GetY() - $yH); $this->Cell($w[1], $yH, $row[1], 'LRB'); $this->Cell($w[2], $yH, number_format($row[2], 2), 'LRB', 0, 'R'); $this->Cell($w[3], $yH, number_format($row[3], 2), 'LRB', 0, 'R'); $this->Cell($w[4], $yH, number_format($row[4], 2), 'LRB', 0, 'R'); $this->Ln(); $i++; } $this->Ln(10); $this->setTextFont('Arial', 'B', 12); $this->Cell($w[0] + $w[1] + $w[3], 6, 'Total', 'TB', 0, 'L'); $this->setDataFont(true); $this->Cell($w[2], 6, number_format(2, 2), 'TB', 0, 'R'); $this->Cell($w[2], 6, number_format(2, 2), 'TB', 0, 'R'); $this->Cell($w[2], 6, number_format(2, 2), 'TB', 0, 'R'); $this->Ln(); $this->setTextFont(); $this->Write(10, "Notes: " . "Thank you for your business."); $this->Ln(10); } public function Footer() { $this->Ln(); $this->Cell($this->PG_W, 5, "Payment Terms: " . "On Receipt", 0, 0, 'L'); $this->Ln(10); $this->Cell($this->PG_W, 5, "Please make cheques payable to Me!!", 0, 0, 'L'); $this->Ln(10); $this->setTextFont(true); $this->Cell($this->PG_W, 5, "Payment Details:", 0, 0, 'L'); $this->setTextFont(false); $this->Ln(); $this->Cell($this->PG_W, 5, "Bank A/C No: 000000000", 0, 0, 'L'); $this->Ln(); // Footer address $address = "True Track Software"; $this->SetY(-(($this->getAddressLength($address) * 5) + 20)); $this->SetFont('Arial', '', 7); $this->Ln(); $this->writeAddress($address); } private function setTextFont($isBold = false) { $this->SetFont('Arial', $isBold ? 'B' : '', 9); } private function setDataFont($isBold = false) { $this->SetFont('Arial', $isBold ? '' : '', ; } private function getAddressLength($address) { return count(explode("\n", $address)); } private function writeAddress($address) { $lines = explode("\n", $address); foreach ($lines as $line) { $this->Cell($this->PG_W, 5, $line, 0, 0, 'C'); $this->Ln(4); } } } Link to comment https://forums.phpfreaks.com/topic/156029-array-and-class-construct-help-please-newbie/ Share on other sites More sharing options...
Mchl Posted April 28, 2009 Share Posted April 28, 2009 FPDF is written in PHP4 so it doesn't have __construct() method. It's constructor is in FPDF() method. Link to comment https://forums.phpfreaks.com/topic/156029-array-and-class-construct-help-please-newbie/#findComment-821413 Share on other sites More sharing options...
Jezza22 Posted April 30, 2009 Author Share Posted April 30, 2009 Ummm... I am sorry but I don't entirely get what you mean? The above Class works and produces a PDF document when I initialise the Object/Class and then use $pdf->output(); 1. I am trying to work out how to assign a value to a property within a method LineItem() 2. Work out how to make the value that I want to assign to the method into an Array The good news is I have worked number 2, I need to create a multi dimensional Array as follows: - $data = array ( array($a1,$a2,$a3,$a4,$a5), array($a6,$a7,$a8,$a9,$a10), ); This works and displays two lines on the PDF document. However I want to assign values to $a1,$a2,$a3 etc outside of the Class???? I have an Index.php as follows that initialises the Class and creates the PDF document... Do I have do something in here... define('FPDF_FONTPATH','font/'); require('fpdf.php'); require ('InvoicePDF.class.php'); $a1="ffefe"; $pdf = new InvoicePDF(); $pdf->Output(); Link to comment https://forums.phpfreaks.com/topic/156029-array-and-class-construct-help-please-newbie/#findComment-823020 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.