I'm trying to center the logo shown in my invoice PDF but I don't really know how to do this. Is there a .phtml template file where I can apply a css class or how do I center it?
1 Answer
I do not know if you're talking about magento 1 or magento 2. In magento 1 there is not a .phtml file for the invoice.
The invoice is created with Zend functions and if you want to change the position of the logo you have to modify the insertLogo () function inside the Mage_Sales_Model_Order_Pdf_Abstract class.
As you can see from this function, the position of the logo is defined by these values:
$y1 = $top - $height;
$y2 = $top;
$x1 = 25; //left margin
$x2 = $x1 + $width;
But change magento core functions is highly discouraged, so in my opinion the best solution is to rewrite the Mage_Sales_Model_Order_Pdf_Invoice class, changing the getPdf() function and adding a custom insertLogo() function inside the same class.
In my case I changed the getPdf() function from
$this->insertLogo($page, $invoice->getStore());
to
$this->insertLogo($page, $invoice->getStore(),225);//225 is the margin from left
and I created my custom function
public function insertLogo(&$page, $store = null, $x1 = 25){
$this->y = $this->y ? $this->y : 815;
$image = Mage::getStoreConfig('sales/identity/logo', $store);
if ($image) {
$image = Mage::getBaseDir('media') . '/sales/store/logo/' . $image;
if (is_file($image)) {
$image = Zend_Pdf_Image::imageWithPath($image);
$top = 830; //top border of the page
$widthLimit = 270; //half of the page width
$heightLimit = 270; //assuming the image is not a "skyscraper"
$width = $image->getPixelWidth();
$height = $image->getPixelHeight();
//preserving aspect ratio (proportions)
$ratio = $width / $height;
if ($ratio > 1 && $width > $widthLimit) {
$width = $widthLimit;
$height = $width / $ratio;
} elseif ($ratio < 1 && $height > $heightLimit) {
$height = $heightLimit;
$width = $height * $ratio;
} elseif ($ratio == 1 && $height > $heightLimit) {
$height = $heightLimit;
$width = $widthLimit;
}
$y1 = $top - $height;
$y2 = $top;
//$x1 = 25; hide this
$x2 = $x1 + $width;
//coordinates after transformation are rounded by Zend
$page->drawImage($image, $x1, $y1, $x2, $y2);
$this->y = $y1 - 10;
}
}
}
If you do not know how rewrite class Mage_Sales_Model_Order_Pdf_Invoice read this article http://inchoo.net/magento/how-to-add-custom-attribute-to-magentos-pdf-invoice/