collection not loading in pdf file in magento - php

I have to export data in pdf of orders in Magento
But i got this error:
Fatal error: Call to undefined method Mage_Reports_Model_Resource_Report_Collection::getSelect() in app/code/core/Mage/Adminhtml/Block/Widget/Grid.php on line 1683
my code for getpdf action
public function getPdfFile(){
$this->_isExport = true;
$this->_prepareGrid();
$this->getCollection()->getSelect()->limit();
$this->getCollection()->setPageSize(0);
$this->getCollection()->load();
$this->_afterLoadCollection();
$pdf = new Zend_Pdf();
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
$page->setFont($font, 12);
$width = $page->getWidth();
$i=0;
foreach ($this->_columns as $column) {
if (!$column->getIsSystem()) {
$i+=10;
$header = $column->getExportHeader();
$page->drawText($header, $i, $page->getHeight()-20);
$width = $font->widthForGlyph($font->glyphNumberForCharacter($header));
$i+=($width/$font->getUnitsPerEm()*12)*strlen($header)+10;
}
}
$pdf->pages[] = $page;
return $pdf->render();
}
and my controller action code is
public function exportPdfAction(){
$fileName = 'daily_orders.pdf';
$content = $this->getLayout()->createBlock('reportneworders/adminhtml_reportneworders_grid')->getPdfFile();
$this->_prepareDownloadResponse($fileName, $content);
}
grid is working proper in admin.
csv and excel data export sucessfully but pdf cannot. any one please help me.

The foreach ($this->_columns as $column) statement which you have only prints the headers of the grid.
You have to iterate through the collection of items as well. Before the $pdf->pages[] = $page; line, add something like this:
$j = 40;
foreach ($collection as $item) {
// add code here, which is printing $item information
// Example:
$y = $page->getHeight()-$j;
$page->drawText($item->getincrement_id(), 20, $y);
$page->drawText($item->getcreated_at(), 50, $y);
// etc.
$j += 20;
}
Also, in my own code, in order the PDF export to respond to the grid filters, I had to change the function exportPdfAction() code to the following (otherwise filters were ignored):
public function exportPdfAction(){
$fileName = 'daily_orders.pdf';
$grid = $this->getLayout()->createBlock('reportneworders/adminhtml_reportneworders_grid');
$this->_initReportAction($grid);
$content = $grid->getPdfFile();
$this->_prepareDownloadResponse($fileName, $content);
}
As you see, I have added the $this->_initReportAction($grid);

Related

Dynamically compose the name of function to call on php

I have csv like this.
id,startScore,endScore,total
1,12,34,46
2,10,20,30
.
.
Now I have Entity with the same column name.
So,it has the functions like these below.
setStartScore()
setEndScore()
setTotal()
For now my php code is like this below
$lines = explode('\n',$csvFile); // get CSV Content
$header = array_shift($lines); // get header
$headers = explode(",",$header)
foreach($lines as $line){ // each csv line
$table = new Table();
foreach(explodes(',',$line) as $l){
$i = 0;
foreach($headers as $h){
$table->set{$headers[$i]}($l[$i]) //how can I make dynamically make set***() function.
$i++;
}
I guess if I get doctrine setter/getter naming regulation, it works well though....
You can use a constructor to set the data.
Class Table
// ..
public function __construct($id, $startScore, $endScore, $total){
$this->id = $id;
$this->startScore = $startScore;
$this->endScore = $endScore;
$this->total = $total;
}
Then you can create the objects like:
foreach($lines as $line){ // each csv line
$data = explodes(',',$line);
$table = new Table($data[0], $data[1], $data[2], $data[3]);
$em->persist($table);
}
// ..flush

PHP Trying to Edit an RSS Feed Title and Description Length, but its not working

Here is my code:
function getThisFeed($feedURL, $source){
$n = array(); //Random Variable to output later
$content = file_get_contents($feedURL); //gets contents of RSS feed
$rss = new SimpleXmlElement($content); //Converts to XML Element
$img = "";
switch ($source){
case "reddit":
$img = "http://dev.dotabattles.com/images/newsThumbs/redditThumb.png";
break;
case "joinDota":
$img = "http://dev.dotabattles.com/images/newsThumbs/joinDotaThumb.png";
break;
case "gosu":
$img = "http://dev.dotabattles.com/images/newsThumbs/gosuThumb.jpg";
break;
default:
$img = "http://dev.dotabattles.com/images/newsThumbs/dotaThumb.png";
}
foreach($rss->channel->item as $entry){
array_push($n, array("title" => substr($entry->title, 0, 50), "fullTitle" => $entry->title, "link" => $entry->link, "description" => $entry->description, "date" => strtotime($entry->pubDate), "img" => $img));
} //Loops through XML element saving the important information
return $n; //returns the full array
}
This is the function that grabs a feed and assigns a thumbnail image to it for display purposes.
function getFullFeed($type = 'all'){
$news = array();
$output = "";
$reddit = getThisFeed("http://reddit.com/r/dota2/.rss", "reddit");
$joinDota = getThisFeed("http://www.joindota.com/feeds/news", "joinDota");
$gosu = getThisFeed("http://www.gosugamers.net/dota2/news/rss", "gosu");
switch ($type) {
case "all":
$news = feedArrayCombine($news, $reddit);
$news = feedArrayCombine($news, $joinDota);
$news = feedArrayCombine($news, $gosu);
usort($news, "cmp");
break;
case "reddit":
$news = $reddit;
break;
case "joinDota":
$news = $joinDota;
break;
case "gosu":
$news = $gosu;
break;
default:
$news = "ERROR";
}
return $news;
}
This function is how I can quickly call different feeds throughout my website.
function editFeed($feed, $titleLimit, $descLimit, $numEntries = null){ //for making small changes in the feed
$news = array();
$newsFeed = $feed;
if ($numEntries) {
for ($i = 0; $i < $numEntries; $i++){
array_push($news, $newsFeed[$i]);
}
$newsFeed = $news;
}
foreach($newsFeed as $f){
$f['title'] = substr($f['title'], 0, $titleLimit);
$f['description'] = substr($f['description'], 0, $descLimit);
}
return $newsFeed;
}
And this final one is how I'm actually trying to edit the feed.
<?php
$news = getFullFeed('gosu');
$news = editFeed($news, 5, 100, 6);
echo printFeed($news);
?>
This is where I've called it on my website. printFeed is just a function that combines bootstrap elements with the feed for display purposes once again.
Everything is showing up, and I'm only getting 6 items in the feed, but the titleLength and descriptionLength are not being changed.

How can i print an array passed from a page to FPDF

My fpdf.php file is
<?php
require('fpdf/fpdf.php');
$prod_data = array();
if($_POST['affiliate_pdf']){
$prod_data = unserialize($_POST['affiliate_pdf']);
}
class PDF extends FPDF
{
public $prod_data;
public function createData($input){
$this->prod_data = $input;
}
function Header()
{
// Page header
global $title;
$this->SetFont('Arial','B',15);
$w = $this->GetStringWidth($title)+6;
$this->SetX((210-$w)/2);
$this->SetDrawColor(0,80,180);
$this->SetFillColor(230,230,0);
$this->SetTextColor(220,50,50);
$this->SetLineWidth(1);
$this->Cell($w,9,$title,1,1,'C',true);
$this->Ln(10);
// Save ordinate
$this->y0 = $this->GetY();
}
// Load data
function LoadData($file)
{
// Read file lines
$lines = file($file);
$data = array();
foreach($lines as $line)
$data[] = explode(';',trim($line));
return $data;
}
}
$pdf = new PDF();
// Column headings
$title = 'Title';
$header = array('AFFILIATE CHANNELS', 'TOTAL CHANNEL REVENUE', 'TOTAL SHARE OF REVENUE', 'TOTAL AFFILIATE SHARE OF REVENUE');
// Data loading
$data = $pdf->LoadData('countries.txt');
$pdf->SetFont('Arial','',14);
$pdf->AddPage();
$pdf->Cell(10,10,'Affiliate Name', 0,0,'L');
$pdf->Output();
?>
affiliate_pdf is my array which i am passing to fpdf.php . Here i want to use its values. So, i just want to ask , how can i print this array in $pdf->Cell or can i write it at any other place.
Mainly i just want to print its value on this pdf. It may be in any array format.
You can use print_r(). By defaut, it print array values but you can set a param to return values.
$pdf->Cell(10,10,print_r($your_array, true), 0,0,'L');

How to call an array outside a class using PHP and Codeigniter?

hello guys i just need a little help here about accessing a data array outside a class i'm so confused on how to show the variable outside the class
Here's my code below:
<?php
date_default_timezone_set('Asia/Manila');
require('resources/fpdf/fpdf.php');
class PDF extends FPDF {
function Header(){
//HERE IS THE PLACE WHERE SHOULD I PUT THE ARRAY, BUT I CANT ACCESS IT INSIDE
$this->SetFont('Arial','B',10);
$this->Cell(180,5,'PURCHASE ORDER',0,0,'C');
$this->Ln();
$this->SetFont('Arial','',9);
$this->Cell(40,5,'Suppliers Name:'.$data['spname'].' ');
$this->Ln();
$this->SetFont('Arial','',9);
$this->Ln(20);
}
}
$query = "SELECT * FROM po_order_details WHERE order_code = '".$code."'";
$result = $this->db->query($query);
foreach($result->result_array() as $row){
$data[] = array($row['item_qty'], //THIS IS THE ARRAY THAT I NEED TO GET
$row['spname'],
$row['spaddress'],
);
}
$this->session->set_userdata('session_data',$data);
//Column titles
$pdf = new PDF();
$header = array('QTY','ITEM / DESCRIPTION' , 'UNIT PRICE', 'TOTAL AMOUNT'); // CHANGE THIS ALSO
$pdf->SetFillColor(255,0,0);
$pdf->Ln();
$pdf->AddPage();
$pdf->BuildTable($header,$data);
$pdf->Ln();
$pdf->Output();
?>
that's all i hope you can help me
Why not just change the prototype of your Header method and pass the array as a parameter like this.
<?php
date_default_timezone_set('Asia/Manila');
require('resources/fpdf/fpdf.php');
class PDF extends FPDF {
function Header( &$titles, &$data ){
//HERE IS THE PLACE WHERE SHOULD I PUT THE ARRAY, BUT I CANT ACCESS IT INSIDE
// use $titles and $data as you like here
$this->SetFont('Arial','B',10);
$this->Cell(180,5,'PURCHASE ORDER',0,0,'C');
$this->Ln();
$this->SetFont('Arial','',9);
$this->Cell(40,5,'Suppliers Name:'.$data['spname'].' ');
$this->Ln();
$this->SetFont('Arial','',9);
$this->Ln(20);
}
}
$query = "SELECT * FROM po_order_details WHERE order_code = '".$code."'";
$result = $this->db->query($query);
foreach($result->result_array() as $row){
$data[] = array($row['item_qty'], //THIS IS THE ARRAY THAT I NEED TO GET
$row['spname'],
$row['spaddress'],
);
}
$this->session->set_userdata('session_data',$data);
//Column titles
$pdf = new PDF();
$header = array('QTY','ITEM / DESCRIPTION' , 'UNIT PRICE', 'TOTAL AMOUNT'); // CHANGE THIS ALSO
$pdf->Header( $header, $data );
$pdf->SetFillColor(255,0,0);
$pdf->Ln();
$pdf->AddPage();
$pdf->BuildTable($header,$data);
$pdf->Ln();
$pdf->Output();
?>
Could you just pass it as a parameter and then return the result?
Like:
function Header($param_array = array()) {
// ... modifications, bla bla
return $result_array;
}
And then look up the function which calls that Header() and pass that array parameter all the way through that function.
Of course, the drawback here is that you would modify FPDF class and would not be able to properly update to an newer version, if you need it at some point in the future.

Probleam using classes , array , json and php

Hi, this is my first time trying to create some code in PHP and it took me a long time but I'm able to convert data to xml. Now I need create a JSON object and it's not going well. The biggest problem is trying to create a new class in PHP (I don't know if what I did is ok or not) and if this is the correct way to attach a list. I think some of it's good but to me since I only use java and c# it seems a little crazy. I think I'm doing something wrong. The line that's showing me an error is $array['data']->attach( new Cake($name,$ingredients,$prepare,$image)); but i don't know what I'm doing wrong.
I haven't yet written the line that includes and transforms the array into json
Thanks
//opens the file, if it doesn't exist, it creates
$pointer = fopen($file, "w");
// writes into json
$cake_list['data'] = new SplObjectStorage();
for ($i = 0; $i < $row; $i++) {
// Takes the SQL data
$name = mysql_result($sql, $i, "B.nome");
$ingredients = mysql_result($sql, $i, "B.ingredientes");
$prepare = mysql_result($sql, $i, "B.preparo");
$image = mysql_result($sql, $i, "B.imagem");
// assembles the xml tags
// $content = "{";
// $content .= "}";
$array['data']->attach( new Cake($name,$ingredients,$prepare,$image));
// $content .= ",";
// Writes in file
// echo $content;
$content = json_encode($content);
fwrite($pointer, $content);
// echo $content;
} // close FOR
echo cake_list;
// close the file
fclose($pointer);
// message
// echo "The file <b> ".$file."</b> was created successfully !";
// closes IF($row)
class Cake {
var $name;
var $ingredients;
var $prepare;
var $image;
public function __construct($name, $ingredients, $prepare, $image)
{
$this->name = $name;
$this->ingredients = $ingredients;
$this->prepare = $prepare;
$this->image = $image;
}
}
function create_instance($class, $arg1, $arg2, $arg3, $arg4)
{
$reflection_class = new ReflectionClass($class);
return $reflection_class->newInstanceArgs($arg1, $arg2,$arg3, $arg4);
}
The error you are experiencing is because you are doing $array['data'] when you mean $cake_list['data'] so change the error line to:
$cake_list['data']->attach(new Cake($name, $ingredients, $prepare, $image));
Also a simple way to simple way to create a JSON object (or more accurately a string representation of a JSON object) is to do this:
$array = array(
'name' => $name,
'ingredients' => $ingredients,
'prepare' => $prepare,
'image' => $image
);
$json = json_encode($array);
You can also create simple easy to use objects like this:
$myObject = new stdClass(); // stdClass() is generic class in PHP
$myObject->name = $name;
$myObject->ingredients = $ingredients;
$myObject->prepare = $prepare;
$myObject->image = $image;

Categories