Edit an invoice PDF

Last modified by dlachasse on Wed, July 25, 2012 11:44
Source|Old Revisions  

I need to modify an invoice so I thought I would document my progress. This based on the invoice in Magento 1.1.2. If you find errors please feel free to correct them. :)

You should never edit Magento core files. So to edit this functionality the easiest way is to do the following:

Copy the contents of /app/code/core/Mage/Bundle/Model/Sales/Order/Pdf/Items/ into /app/code/local/Mage/Sales/Model/Order/Pdf/ - this file will override the original, and will not be overwritten during upgrade. The drawback is that you’ll have to make sure to apply upgrade changes of the original file to this one, to avoid incompatibility with upgraded code.

The PDF is generated using the Zend_Pdf component of the Zend Framework. Here is a little tutorial

Page format: The default invoice PDF is set up in the A4 format. To set it for letter

in /app/code/local/Mage/Sales/Model/Order/Pdf/Invoice.php

change

$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);

to

$page = $pdf->newPage(Zend_Pdf_Page::SIZE_LETTER);

Move Logo: When I changed the page format to Letter it moved the logo off the top. In ZEND_PDF the measurements are in Points(1/72 of an inch) and the origin is is lower left. I moved it down 50pt by subtracting 50 from the Y coordinates.

in /app/code/local/Mage/Sales/Model/Order/Pdf/Abstract.php find the function insertLogo and change

$page->drawImage($image, 25, 800, 125, 825);

to

$page->drawImage($image, 25, 750, 125, 775);

At this point the logo under a grey box. :)

You now need to take 50px away from the rest of the elements Y coords to move everything else down. It get’s a little messy, just keep generating PDFs to watch your progress.

most of the coords are in /app/code/local/Mage/Sales/Model/Order/Pdf/Abstract.php and in /app/code/local/Mage/Sales/Model/Order/Pdf/Invoice.php

Increase the lower Margin: We need some room at the bottom each page for some preprinted material that will be on each invoice.

in /app/code/local/Mage/Sales/Model/Order/Pdf/Invoice.php

change

if ($this->y<15) {

to

if ($this->y<215) {

This adds 200 points (about 2.75”) to the margin

Fonts: The the invoice uses Libertine. It’s a great font but with it the file size was 1.4MB without 8KB.

in /app/code/local/Mage/Sales/Model/Order/Pdf/Invoice.php

I changed all the references to Libertine to Helvetica with cut and paste.

In /app/code/local/Mage/Sales/Model/Order/Pdf/Items/Abstract.php I changed

    protected function _setFontRegular($size = 7)
    {
        $font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir() . '/lib/LinLibertineFont/LinLibertineC_Re-2.8.0.ttf');
        $this->getPage()->setFont($font, $size);
        return $font;
    }

    protected function _setFontBold($size = 7)
    {
        $font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir() . '/lib/LinLibertineFont/LinLibertine_Bd-2.8.1.ttf');
        $this->getPage()->setFont($font, $size);
        return $font;
    }

    protected function _setFontItalic($size = 7)
    {
        $font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir() . '/lib/LinLibertineFont/LinLibertine_It-2.8.2.ttf');
        $this->getPage()->setFont($font, $size);
        return $font;
    }

to

   protected function _setFontRegular($size = 7)
    {
        $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
        $this->getPage()->setFont($font, $size);
        return $font;
    }

    protected function _setFontBold($size = 7)
    {
        $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);
        $this->getPage()->setFont($font, $size);
        return $font;
    }

    protected function _setFontItalic($size = 7)
    {
        $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD_ITALIC);
        $this->getPage()->setFont($font, $size);
        return $font;
    }

Also in /app/code/local/Mage/Sales/Model/Order/Pdf/Abstract.php change

$font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir()."/lib/LinLibertineFont/LinLibertineC_Re-2.8.0.ttf");

to

$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);

Change the font size for the billing and shipping addresses in Magento 1.4.1.1

In /app/code/local/Mage/Sales/Model/Order/Pdf/Abstract.php

Edit Line 249:

Use Default Font Size

$this_setFontRegular($page);

Use Custom Font Size

// The second param sets the font size
$this_setFontRegular($page, 12);

Avaible modules




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs