Magento – Add text to invoice

invoicemagento-1.9pdf

I want to add text to the pdf invoice in the marked arena (see photo).

I'm using Magento version 1.9.2.1

How can I do this?

pdf invoice

Best Answer

You can add specific text after total price follow the code

First override that block add this code in your config file between <global/>:

<models>
        <sales>
            <rewrite>
                <order_pdf_invoice>Namespace_Module_Model_Order_Pdf_Invoice</order_pdf_invoice>
            </rewrite>
        </sales>
    </models>

Create file in app/code/local/Namespace/Module/Model/Order/Pdf/Invoice.php and add the below function at the end of the file:

<?php 
class Mage_Sales_Model_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Abstract 
{
   public function insertText($page)
   {
     $page->drawLine(25, $this->y, 570, $this->y); 
     $this->y -= 25;
     $page->drawText(Mage::helper('sales')->__('Add here Your custom text'), 35, $this->y, 'UTF-8');
 }

Now, you need to call this insertText() function in the getPdf() function and add this getPdf() function after insertText() function like here:

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->insertText($page);  // your custom text is added here
    $this->_afterGetPdf();
    return $pdf;
}

$this->insertText($page); that call your custom text function so you need to add before $this->_afterGetPdf(); this function.

Related Topic