0

I have followed the instruction in the link -> TCPDF With Magento, now I want to call the TCPDF library from Invoice.php file at app/code/core/Mage/Sales/Model/Order/Pdf/Invoice.php because I want to print the invoice data to a TCPDF invoice.

How to use the functions to get the invoice data and print it to a TCPDF file?

Here are the functions which prints invoice data to a PDF file generated by Zend framework.

<?php

class Mage_Sales_Model_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Abstract
{
    /**
     * Draw header for item table
     *
     * @param Zend_Pdf_Page $page
     * @return void
     */
    protected function _drawHeader(Zend_Pdf_Page $page)
    {
        /* Add table head */
        $this->_setFontRegular($page, 10);
        $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, 0, 0));

        //columns headers
        $lines[0][] = array(
            'text' => Mage::helper('sales')->__('Products'),
            'feed' => 35
        );

        $lines[0][] = array(
            'text'  => Mage::helper('sales')->__('SKU'),
            'feed'  => 290,
            'align' => 'right'
        );

        $lines[0][] = array(
            'text'  => Mage::helper('sales')->__('Qty'),
            'feed'  => 435,
            'align' => 'right'
        );

        $lines[0][] = array(
            'text'  => Mage::helper('sales')->__('Price'),
            'feed'  => 360,
            'align' => 'right'
        );

        $lines[0][] = array(
            'text'  => Mage::helper('sales')->__('Tax'),
            'feed'  => 495,
            'align' => 'right'
        );

        $lines[0][] = array(
            'text'  => Mage::helper('sales')->__('Subtotal'),
            'feed'  => 565,
            'align' => 'right'
        );

        $lineBlock = array(
            'lines'  => $lines,
            'height' => 5
        );

        $this->drawLineBlocks($page, array($lineBlock), array('table_header' => true));
        $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
        $this->y -= 20;
    }

    /**
     * Return PDF document
     *
     * @param  array $invoices
     * @return Zend_Pdf
     */
    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;
    }

    /**
     * Create new page and assign to PDF object
     *
     * @param  array $settings
     * @return Zend_Pdf_Page
     */
    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;
    }
}
0

In order to completely avoid Zend_Pdf from the playground, you need to rewrite Mage_Sales_Model_Order_Pdf_Invoice::getPdf() method. Your custom getPdf() should somewhat look like this:

<?php
class [Namespace]_[Module]_Model_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Invoice
{
    public function getPdf($invoices = array())
    {
        foreach ($invoices as $invoice) {

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

            //set registry current order as invoice order.
            $currentOrder = Mage::registry('current_order');
            if (!$currentOrder) {
                Mage::register('current_order', $order);
            }
            if ($currentOrder->getId() != $order->getId()) {
                Mage::unregister('current_order');
                Mage::register('current_order', $order);
            }

            //your tcpdf stuffs comes here
            $tcpdf = new TCPDF_TCPDF();

            //using magneto's default invoice print html file for preparing invoice content
            $request = Mage::app()->getRequest();
            $html = $request->getLayout()
                ->createBlock('sales/order_print_invoice', 'sales.order.pdf.invoice')
                ->setTemplate('sales/order/print/invoice.phtml')
                ->toHtml();

            $tcpdf->AddPage();

            $tcpdf->writeHTML($html, true, false, true, false, '');

            $tcpdf->lastPage();

            return $tcpdf->Output('sales_order_invoice_'.time().'.pdf', 'I');
        }


    }
}

I don't have previous experience with tcpdf. But from the above code, you will get the idea. getPdf() is the function which is responsible for using zend_pdf library to generate invoice pdf. By rewriting this method, you can use tcpdf in order to generate invoice pdf.

Here what we are doing is, using Magento's default invoice print phtml file to generate invoice pdf content. This invoice phtml file uses the class Mage_Sales_Block_Order_Print_Invoice class instance and it requires Mage::registry('current_order') to be set in order to render it.

Hope you can you use this as a starting point.

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.