Magento – Fatal Error: Call to getPdf() on Non-Object in OrderController.php

invoicemassactionmoduleorder-gridpdf

I am getting this following error in a module am developing to print custom invoice/pdf. The code is working upto the controller part, but when it tries to call the model an error is occurring there.

I am not looking to overwrite the core model, as it is intended that old invoice should work as it is, and the new mass action warehouse order print should generate new type of PDF.
Please help with following code, am stuck with no clues!

Fatal error: Call to a member function getPdf() on a non-object in C:\wamp\www\Goku\app\code\local\Goku\Ordersexporter\controllers\Mage\Adminhtml\Sales\OrderController.php on line 332

Structure of module

app-
  |-code
    |-local
        |-Goku
            |-Ordersexporter
                |-Block
                |   |-Mage
                |       |-Adminhtml
                |           |-Sales
                |               |-Order
                |                   |-Grid.php
                |-controllers
                |   |-Mage
                |       |-Adminhtml
                |           |-Sales
                |               |-OrderController.php
                |-etc
                |   |-config.xml
                |-Model
                    |-Order
                        |-Pdf
                            |-Invoice.php

1) grid.php

class Goku_Ordersexporter_Block_Mage_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
    public function __construct()
    {
        parent::__construct();
    }

    protected function _prepareMassaction()
    {

        $this->getMassactionBlock()->addItem('Goku_warehouseOrderPrint', array(
            'label' => Mage::helper('sales')->__('Warehouse Order Sheet'),
            'url' => $this->getUrl('*/sales_order/Goku_warehouseOrderPrint'),
        ));

        parent::_prepareMassaction();
        return $this;
    }
}

2) OrderController.php

$defController = Mage::getBaseDir()
    . DS . 'app' . DS . 'code' . DS . 'core'
    . DS . 'Mage' . DS . 'Adminhtml' . DS . 'controllers'
    . DS . 'Sales' . DS . 'OrderController.php';

require_once $defController;

class Goku_Ordersexporter_Mage_Adminhtml_Sales_OrderController extends Mage_Adminhtml_Sales_OrderController
{
    public function Goku_warehouseOrderPrintAction()
    {
        $orderIds = $this->getRequest()->getPost('order_ids');
        $flag = false;
        if (!empty($orderIds)) {
            foreach ($orderIds as $orderId) {
                $invoices = Mage::getResourceModel('sales/order_invoice_collection')
                    ->setOrderFilter($orderId)
                    ->load();
                if ($invoices->getSize() > 0) {
                    $flag = true;
                    if (!isset($pdf)) {
                        $pdf = Mage::getModel('Goku_Ordersexporter/order_pdf_invoice')->getPdf($invoices);
                    } else {
                        $pages = Mage::getModel('Goku_Ordersexporter/order_pdf_invoice')->getPdf($invoices);
                        $pdf->pages = array_merge($pdf->pages, $pages->pages);
                    }
                }
            }
            if ($flag) {
                return $this->_prepareDownloadResponse(
                    'invoice' . Mage::getSingleton('core/date')->date('Y-m-d_H-i-s') . '.pdf', $pdf->render(),
                    'application/pdf'
                );
            } else {
                $this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
                $this->_redirect('*/*/');
            }
        }
        $this->_redirect('*/*/');
    }
}

3) config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Goku_Ordersexporter>
            <version>1.0.0.0</version>
        </Goku_Ordersexporter>
    </modules>

    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <sales_order_grid>Goku_Ordersexporter_Block_Mage_Adminhtml_Sales_Order_Grid</sales_order_grid>
                </rewrite>
            </adminhtml>
        </blocks>
        <models>
            <Goku_Ordersexporter>
                <class>Goku_Ordersexporter_Model</class>
            </Goku_Ordersexporter>
        </models>
    </global>

    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Goku_Ordersexporter before="Mage_Adminhtml">Goku_Ordersexporter_Mage_Adminhtml</Goku_Ordersexporter>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>

</config>

4) Invoice.php

class Goku_Ordersexporter_Mage_Sales_Model_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Invoice
{
    public function getPdf($invoices = array())
    {
        $this->_beforeGetPdf();
        $this->_initRenderer('invoice');

        $pdf = new Zend_Pdf();
        $this->_setPdf($pdf);
        $style = new Zend_Pdf_Style();
        $this->_setFontBold($style, 10);

        foreach ($invoices as $invoice) {
            if ($invoice->getStoreId()) {
                Mage::app()->getLocale()->emulate($invoice->getStoreId());
                Mage::app()->setCurrentStore($invoice->getStoreId());
            }
            $page  = $this->newPage();
            $order = $invoice->getOrder();
            /* Add image */
            $this->insertLogo($page, $invoice->getStore());
            /* Add address */
            $this->insertAddress($page, $invoice->getStore());
            /* Add head */
            $this->insertOrder(
                $page,
                $order,
                Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId())
            );
            /* Add document text and number */
            $this->insertDocumentNumber(
                $page,
                Mage::helper('sales')->__('Invoice # ') . $invoice->getIncrementId()
            );
            /* Add table */
            $this->_drawHeader($page);
            /* Add body */
            foreach ($invoice->getAllItems() as $item){
                if ($item->getOrderItem()->getParentItem()) {
                    continue;
                }
                /* Draw item */
                $this->_drawItem($item, $page, $order);
                $page = end($pdf->pages);
            }
            /* Add totals */
            $this->insertTotals($page, $invoice);
            if ($invoice->getStoreId()) {
                Mage::app()->getLocale()->revert();
            }
        }
        $this->_afterGetPdf();
        return $pdf;
    }

    public function newPage(array $settings = array())
    {
        /* Add new table head */
        $page = $this->_getPdf()->newPage(Zend_Pdf_Page::SIZE_A4);
        $this->_getPdf()->pages[] = $page;
        $this->y = 800;
        if (!empty($settings['table_header'])) {
            $this->_drawHeader($page);
        }
        return $page;
    }
}

Best Answer

Your class should be names:

Goku_Ordersexporter_Model_Order_Pdf_Invoice

To find these errors, have a look here: Fundamentals for debugging a Magento store

Related Topic