0

I am using magento 1.9.3.3 and have a weird issue, whereby when I go to print order PDF's for two orders I get the error:

Please define PDF object before using.

Strange, as it works splendidly for other orders, just two orders it is not and I am perplexed as to why this is occurring for these two only.

Here is the getpdf function:

public function getPdf($orders = array())
{
    $this->_beforeGetPdf();
    $this->_initRenderer('order');

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

    foreach ($orders as $order) {
        $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
        $pdf->pages[] = $page;

        //$order = $invoice->getOrder();

        /* Add image */
        $this->insertLogo($page, $order->getStore());

        /* Add address */
        $this->insertAddress($page, $order->getStore());

        /* Add head */
        $this->insertOrder($page, $order, Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId()));

        $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
        $this->_setFontRegular($page);

        /* Add table */
        $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
        $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
        $page->setLineWidth(0.5);

        $page->drawRectangle(25, $this->y, 570, $this->y -15);
        $this->y -=10;

        /* Add table head */
        $page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4));
        $page->drawText(Mage::helper('sales')->__('Product'), 35, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('SKU'), 240, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('Price'), 380, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('QTY'), 430, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('Tax'), 480, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('Subtotal'), 535, $this->y, 'UTF-8');

        $this->y -=15;

        $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));

        /* Add body */
        foreach ($order->getAllItems() as $item){
            if ($item->getParentItem()) {
                continue;
            }

            $shift = array();
            if ($this->y<15) {
                /* Add new table head */
                $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
                $pdf->pages[] = $page;
                $this->y = 800;

                $this->_setFontRegular($page);
                $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
                $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
                $page->setLineWidth(0.5);
                $page->drawRectangle(25, $this->y, 570, $this->y-15);
                $this->y -=10;

                $page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4));
                $page->drawText(Mage::helper('sales')->__('Product'), 35, $this->y, 'UTF-8');
                $page->drawText(Mage::helper('sales')->__('SKU'), 240, $this->y, 'UTF-8');
                $page->drawText(Mage::helper('sales')->__('Price'), 380, $this->y, 'UTF-8');
                $page->drawText(Mage::helper('sales')->__('QTY'), 430, $this->y, 'UTF-8');
                $page->drawText(Mage::helper('sales')->__('Tax'), 480, $this->y, 'UTF-8');
                $page->drawText(Mage::helper('sales')->__('Subtotal'), 535, $this->y, 'UTF-8');

                $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
                $this->y -=20;
            }

            /* Draw item */
            $this->_drawItem($item, $page, $order);
        }

        /* Add totals */
        $this->insertTotals($page, $order);
    }

    $this->_afterGetPdf();

    return $pdf;
}

This is the controller:

public function printAction(){
    $order = $this->_initOrder();
    if (!empty($order)) {
        $order->setOrder($order);
        $pdf = Mage::getModel('Nastnet_OrderPrint/order_pdf_order')->getPdf(array($order));
        return $this->_prepareDownloadResponse('order'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(), 'application/pdf');
    }
    $this->_redirect('*/*/');
}

Update:

I have found that removing the following line from the getpdf function made it work, strange as no special characters are in there.

 $this->insertAddress($page, $order->getStore());

I also moved it below the following line and this made it work:

$this->insertOrder($page, $order, Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId()));

Similarly, if I comment out the line $this->insertTotals($page, $order); it works.

What I am thinking is that the pdf can fit all of the items onto one page and then there ain't enough space for the totals

Update:

The looks to be thrown in line 847 of abstract.php (app/code/core/Mage/Sales/Model/Order/Pdf)

Code:

protected function _getPdf()
{
    if (!$this->_pdf instanceof Zend_Pdf) {
        Mage::throwException(Mage::helper('sales')->__('Please define PDF object before using.'));
    }

    return $this->_pdf;
}

Doing a var_dump on the line $this->_pdf, it returns NULL.

Then a var_dump on $this is object(Nastnet_OrderPrint_Model_Order_Pdf_Order)

  • Do the orders have any special characters in the product names, customer names, or customer addresses? – seanbreeden Jul 20 '17 at 12:06
  • @seanbreeden none whatsoever :s – Vaishal Patel Jul 20 '17 at 16:20
  • @seanbreeden I removed the line $this->insertAddress($page, $order->getStore()); which got it to work. You might onto something here. strange as I cannot see no special characters in the address – Vaishal Patel Jul 20 '17 at 16:37
0

Okay, so not a full blown solution or specific reason why this error is happening, but it looks to be a space issue.

Zend pdf can fit everything except the totals block on one page, then when it goes to print the totals block it runs out of room, so removing this block or another is why it worked then. similarly, reordering the lines would have created an overlap somewhere freeing up more space on the page thus leaving enough room for the totals block.

Consequently, I amended the draw function to leave a gap of 5 instead of 10 per item and this has got it to work.

Amended code:

public function draw()
    {
        $order  = $this->getOrder();
        $item   = $this->getItem();
        $pdf    = $this->getPdf();
        $page   = $this->getPage();
        $shift  = array(0, 5, 0);

        $this->_setFontRegular();

        //$page->drawText($item->getQty()*1, 435, $pdf->y, 'UTF-8');
        $page->drawText($item->getQtyOrdered()*1, 435, $pdf->y, 'UTF-8');

        /* in case Product name is longer than 80 chars - it is written in a few lines */
        foreach (Mage::helper('core/string')->str_split($item->getName(), 60, true, true) as $key => $part) {
            $page->drawText($part, 35, $pdf->y-$shift[0], 'UTF-8');
            $shift[0] += 5;
        }

        $options = $this->getItemOptions();
        if (isset($options)) {
            foreach ($options as $option) {
                // draw options label
                $this->_setFontItalic();
                foreach (Mage::helper('core/string')->str_split(strip_tags($option['label']), 60, false, true) as $_option) {
                    $page->drawText($_option, 35, $pdf->y-$shift[0], 'UTF-8');
                    $shift[0] += 5;
                }
                // draw options value
                $this->_setFontRegular();
                if ($option['value']) {
                    $values = explode(', ', strip_tags($option['value']));
                    foreach ($values as $value) {
                        foreach (Mage::helper('core/string')->str_split($value, 60,true,true) as $_value) {
                            $page->drawText($_value, 40, $pdf->y-$shift[0], 'UTF-8');
                            $shift[0] += 5;
                        }
                    }
                }
            }
        }

        foreach ($this->_parseDescription() as $description){
            $page->drawText(strip_tags($description), 65, $pdf->y-$shift[1], 'UTF-8');
            $shift[1] += 5;
        }

        /* in case Product SKU is longer than 36 chars - it is written in a few lines */
        foreach (Mage::helper('core/string')->str_split($this->getSku($item), 25) as $key => $part) {
            if ($key > 0) {
                $shift[2] += 5;
            }
            $page->drawText($part, 240, $pdf->y-$shift[2], 'UTF-8');
        }

        $font = $this->_setFontBold();

        $row_total = $order->formatPriceTxt($item->getRowTotal());
        $page->drawText($row_total, 565-$pdf->widthForStringUsingFontSize($row_total, $font, 7), $pdf->y, 'UTF-8');

        $price = $order->formatPriceTxt($item->getPrice());
        $page->drawText($price, 395-$pdf->widthForStringUsingFontSize($price, $font, 7), $pdf->y, 'UTF-8');

        $tax = $order->formatPriceTxt($item->getTaxAmount());
        $page->drawText($tax, 495-$pdf->widthForStringUsingFontSize($tax, $font, 7), $pdf->y, 'UTF-8');

        $pdf->y -= max($shift)+5;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.