PageRenderTime 108ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Sales/Model/Order/Pdf/Abstract.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 757 lines | 482 code | 96 blank | 179 comment | 55 complexity | dd5baf6c684656aae6023809b3ad4c89 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Sales
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Sales Order PDF abstract model
  28. *
  29. * @category Mage
  30. * @package Mage_Sales
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Sales_Model_Order_Pdf_Abstract extends Varien_Object
  34. {
  35. public $y;
  36. /**
  37. * Item renderers with render type key
  38. *
  39. * model => the model name
  40. * renderer => the renderer model
  41. *
  42. * @var array
  43. */
  44. protected $_renderers = array();
  45. const XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID = 'sales_pdf/invoice/put_order_id';
  46. const XML_PATH_SALES_PDF_SHIPMENT_PUT_ORDER_ID = 'sales_pdf/shipment/put_order_id';
  47. const XML_PATH_SALES_PDF_CREDITMEMO_PUT_ORDER_ID = 'sales_pdf/creditmemo/put_order_id';
  48. /**
  49. * Zend PDF object
  50. *
  51. * @var Zend_Pdf
  52. */
  53. protected $_pdf;
  54. protected $_defaultTotalModel = 'sales/order_pdf_total_default';
  55. /**
  56. * Retrieve PDF
  57. *
  58. * @return Zend_Pdf
  59. */
  60. abstract public function getPdf();
  61. /**
  62. * Returns the total width in points of the string using the specified font and
  63. * size.
  64. *
  65. * This is not the most efficient way to perform this calculation. I'm
  66. * concentrating optimization efforts on the upcoming layout manager class.
  67. * Similar calculations exist inside the layout manager class, but widths are
  68. * generally calculated only after determining line fragments.
  69. *
  70. * @param string $string
  71. * @param Zend_Pdf_Resource_Font $font
  72. * @param float $fontSize Font size in points
  73. * @return float
  74. */
  75. public function widthForStringUsingFontSize($string, $font, $fontSize)
  76. {
  77. $drawingString = '"libiconv"' == ICONV_IMPL ? iconv('UTF-8', 'UTF-16BE//IGNORE', $string) : @iconv('UTF-8', 'UTF-16BE', $string);
  78. $characters = array();
  79. for ($i = 0; $i < strlen($drawingString); $i++) {
  80. $characters[] = (ord($drawingString[$i++]) << 8) | ord($drawingString[$i]);
  81. }
  82. $glyphs = $font->glyphNumbersForCharacters($characters);
  83. $widths = $font->widthsForGlyphs($glyphs);
  84. $stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
  85. return $stringWidth;
  86. }
  87. /**
  88. * Calculate coordinates to draw something in a column aligned to the right
  89. *
  90. * @param string $string
  91. * @param int $x
  92. * @param int $columnWidth
  93. * @param Zend_Pdf_Resource_Font $font
  94. * @param int $fontSize
  95. * @param int $padding
  96. * @return int
  97. */
  98. public function getAlignRight($string, $x, $columnWidth, Zend_Pdf_Resource_Font $font, $fontSize, $padding = 5)
  99. {
  100. $width = $this->widthForStringUsingFontSize($string, $font, $fontSize);
  101. return $x + $columnWidth - $width - $padding;
  102. }
  103. /**
  104. * Calculate coordinates to draw something in a column aligned to the center
  105. *
  106. * @param string $string
  107. * @param int $x
  108. * @param int $columnWidth
  109. * @param Zend_Pdf_Resource_Font $font
  110. * @param int $fontSize
  111. * @return int
  112. */
  113. public function getAlignCenter($string, $x, $columnWidth, Zend_Pdf_Resource_Font $font, $fontSize)
  114. {
  115. $width = $this->widthForStringUsingFontSize($string, $font, $fontSize);
  116. return $x + round(($columnWidth - $width) / 2);
  117. }
  118. protected function insertLogo(&$page, $store = null)
  119. {
  120. $image = Mage::getStoreConfig('sales/identity/logo', $store);
  121. if ($image) {
  122. $image = Mage::getStoreConfig('system/filesystem/media', $store) . '/sales/store/logo/' . $image;
  123. if (is_file($image)) {
  124. $image = Zend_Pdf_Image::imageWithPath($image);
  125. $page->drawImage($image, 25, 800, 125, 825);
  126. }
  127. }
  128. //return $page;
  129. }
  130. protected function insertAddress(&$page, $store = null)
  131. {
  132. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  133. $this->_setFontRegular($page, 5);
  134. $page->setLineWidth(0.5);
  135. $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
  136. $page->drawLine(125, 825, 125, 790);
  137. $page->setLineWidth(0);
  138. $this->y = 820;
  139. foreach (explode("\n", Mage::getStoreConfig('sales/identity/address', $store)) as $value){
  140. if ($value!=='') {
  141. $page->drawText(trim(strip_tags($value)), 130, $this->y, 'UTF-8');
  142. $this->y -=7;
  143. }
  144. }
  145. //return $page;
  146. }
  147. /**
  148. * Format address
  149. *
  150. * @param string $address
  151. * @return array
  152. */
  153. protected function _formatAddress($address)
  154. {
  155. $return = array();
  156. foreach (explode('|', $address) as $str) {
  157. foreach (Mage::helper('core/string')->str_split($str, 65, true, true) as $part) {
  158. if (empty($part)) {
  159. continue;
  160. }
  161. $return[] = $part;
  162. }
  163. }
  164. return $return;
  165. }
  166. protected function insertOrder(&$page, $obj, $putOrderId = true)
  167. {
  168. if ($obj instanceof Mage_Sales_Model_Order) {
  169. $shipment = null;
  170. $order = $obj;
  171. } elseif ($obj instanceof Mage_Sales_Model_Order_Shipment) {
  172. $shipment = $obj;
  173. $order = $shipment->getOrder();
  174. }
  175. /* @var $order Mage_Sales_Model_Order */
  176. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0.5));
  177. $page->drawRectangle(25, 790, 570, 755);
  178. $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
  179. $this->_setFontRegular($page);
  180. if ($putOrderId) {
  181. $page->drawText(Mage::helper('sales')->__('Order # ').$order->getRealOrderId(), 35, 770, 'UTF-8');
  182. }
  183. //$page->drawText(Mage::helper('sales')->__('Order Date: ') . date( 'D M j Y', strtotime( $order->getCreatedAt() ) ), 35, 760, 'UTF-8');
  184. $page->drawText(Mage::helper('sales')->__('Order Date: ') . Mage::helper('core')->formatDate($order->getCreatedAtStoreDate(), 'medium', false), 35, 760, 'UTF-8');
  185. $page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
  186. $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
  187. $page->setLineWidth(0.5);
  188. $page->drawRectangle(25, 755, 275, 730);
  189. $page->drawRectangle(275, 755, 570, 730);
  190. /* Calculate blocks info */
  191. /* Billing Address */
  192. $billingAddress = $this->_formatAddress($order->getBillingAddress()->format('pdf'));
  193. /* Payment */
  194. $paymentInfo = Mage::helper('payment')->getInfoBlock($order->getPayment())
  195. ->setIsSecureMode(true)
  196. ->toPdf();
  197. $payment = explode('{{pdf_row_separator}}', $paymentInfo);
  198. foreach ($payment as $key=>$value){
  199. if (strip_tags(trim($value))==''){
  200. unset($payment[$key]);
  201. }
  202. }
  203. reset($payment);
  204. /* Shipping Address and Method */
  205. if (!$order->getIsVirtual()) {
  206. /* Shipping Address */
  207. $shippingAddress = $this->_formatAddress($order->getShippingAddress()->format('pdf'));
  208. $shippingMethod = $order->getShippingDescription();
  209. }
  210. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  211. $this->_setFontRegular($page);
  212. $page->drawText(Mage::helper('sales')->__('SOLD TO:'), 35, 740 , 'UTF-8');
  213. if (!$order->getIsVirtual()) {
  214. $page->drawText(Mage::helper('sales')->__('SHIP TO:'), 285, 740 , 'UTF-8');
  215. }
  216. else {
  217. $page->drawText(Mage::helper('sales')->__('Payment Method:'), 285, 740 , 'UTF-8');
  218. }
  219. if (!$order->getIsVirtual()) {
  220. $y = 730 - (max(count($billingAddress), count($shippingAddress)) * 10 + 5);
  221. }
  222. else {
  223. $y = 730 - (count($billingAddress) * 10 + 5);
  224. }
  225. $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
  226. $page->drawRectangle(25, 730, 570, $y);
  227. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  228. $this->_setFontRegular($page);
  229. $this->y = 720;
  230. foreach ($billingAddress as $value){
  231. if ($value!=='') {
  232. $page->drawText(strip_tags(ltrim($value)), 35, $this->y, 'UTF-8');
  233. $this->y -=10;
  234. }
  235. }
  236. if (!$order->getIsVirtual()) {
  237. $this->y = 720;
  238. foreach ($shippingAddress as $value){
  239. if ($value!=='') {
  240. $page->drawText(strip_tags(ltrim($value)), 285, $this->y, 'UTF-8');
  241. $this->y -=10;
  242. }
  243. }
  244. $page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
  245. $page->setLineWidth(0.5);
  246. $page->drawRectangle(25, $this->y, 275, $this->y-25);
  247. $page->drawRectangle(275, $this->y, 570, $this->y-25);
  248. $this->y -=15;
  249. $this->_setFontBold($page);
  250. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  251. $page->drawText(Mage::helper('sales')->__('Payment Method'), 35, $this->y, 'UTF-8');
  252. $page->drawText(Mage::helper('sales')->__('Shipping Method:'), 285, $this->y , 'UTF-8');
  253. $this->y -=10;
  254. $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
  255. $this->_setFontRegular($page);
  256. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  257. $paymentLeft = 35;
  258. $yPayments = $this->y - 15;
  259. }
  260. else {
  261. $yPayments = 720;
  262. $paymentLeft = 285;
  263. }
  264. foreach ($payment as $value){
  265. if (trim($value)!=='') {
  266. $page->drawText(strip_tags(trim($value)), $paymentLeft, $yPayments, 'UTF-8');
  267. $yPayments -=10;
  268. }
  269. }
  270. if (!$order->getIsVirtual()) {
  271. $this->y -=15;
  272. $page->drawText($shippingMethod, 285, $this->y, 'UTF-8');
  273. $yShipments = $this->y;
  274. $totalShippingChargesText = "(" . Mage::helper('sales')->__('Total Shipping Charges') . " " . $order->formatPriceTxt($order->getShippingAmount()) . ")";
  275. $page->drawText($totalShippingChargesText, 285, $yShipments-7, 'UTF-8');
  276. $yShipments -=10;
  277. $tracks = array();
  278. if ($shipment) {
  279. $tracks = $shipment->getAllTracks();
  280. }
  281. if (count($tracks)) {
  282. $page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
  283. $page->setLineWidth(0.5);
  284. $page->drawRectangle(285, $yShipments, 510, $yShipments - 10);
  285. $page->drawLine(380, $yShipments, 380, $yShipments - 10);
  286. //$page->drawLine(510, $yShipments, 510, $yShipments - 10);
  287. $this->_setFontRegular($page);
  288. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  289. //$page->drawText(Mage::helper('sales')->__('Carrier'), 290, $yShipments - 7 , 'UTF-8');
  290. $page->drawText(Mage::helper('sales')->__('Title'), 290, $yShipments - 7, 'UTF-8');
  291. $page->drawText(Mage::helper('sales')->__('Number'), 385, $yShipments - 7, 'UTF-8');
  292. $yShipments -=17;
  293. $this->_setFontRegular($page, 6);
  294. foreach ($tracks as $track) {
  295. $CarrierCode = $track->getCarrierCode();
  296. if ($CarrierCode!='custom')
  297. {
  298. $carrier = Mage::getSingleton('shipping/config')->getCarrierInstance($CarrierCode);
  299. $carrierTitle = $carrier->getConfigData('title');
  300. }
  301. else
  302. {
  303. $carrierTitle = Mage::helper('sales')->__('Custom Value');
  304. }
  305. //$truncatedCarrierTitle = substr($carrierTitle, 0, 35) . (strlen($carrierTitle) > 35 ? '...' : '');
  306. $truncatedTitle = substr($track->getTitle(), 0, 45) . (strlen($track->getTitle()) > 45 ? '...' : '');
  307. //$page->drawText($truncatedCarrierTitle, 285, $yShipments , 'UTF-8');
  308. $page->drawText($truncatedTitle, 300, $yShipments , 'UTF-8');
  309. $page->drawText($track->getNumber(), 395, $yShipments , 'UTF-8');
  310. $yShipments -=7;
  311. }
  312. } else {
  313. $yShipments -= 7;
  314. }
  315. $currentY = min($yPayments, $yShipments);
  316. // replacement of Shipments-Payments rectangle block
  317. $page->drawLine(25, $this->y + 15, 25, $currentY);
  318. $page->drawLine(25, $currentY, 570, $currentY);
  319. $page->drawLine(570, $currentY, 570, $this->y + 15);
  320. $this->y = $currentY;
  321. $this->y -= 15;
  322. }
  323. }
  324. protected function _sortTotalsList($a, $b) {
  325. if (!isset($a['sort_order']) || !isset($b['sort_order'])) {
  326. return 0;
  327. }
  328. if ($a['sort_order'] == $b['sort_order']) {
  329. return 0;
  330. }
  331. return ($a['sort_order'] > $b['sort_order']) ? 1 : -1;
  332. }
  333. protected function _getTotalsList($source)
  334. {
  335. $totals = Mage::getConfig()->getNode('global/pdf/totals')->asArray();
  336. usort($totals, array($this, '_sortTotalsList'));
  337. $totalModels = array();
  338. foreach ($totals as $index => $totalInfo) {
  339. if (!empty($totalInfo['model'])) {
  340. $totalModel = Mage::getModel($totalInfo['model']);
  341. if ($totalModel instanceof Mage_Sales_Model_Order_Pdf_Total_Default) {
  342. $totalInfo['model'] = $totalModel;
  343. } else {
  344. Mage::throwException(
  345. Mage::helper('sales')->__('PDF total model should extend Mage_Sales_Model_Order_Pdf_Total_Default')
  346. );
  347. }
  348. } else {
  349. $totalModel = Mage::getModel($this->_defaultTotalModel);
  350. }
  351. $totalModel->setData($totalInfo);
  352. $totalModels[] = $totalModel;
  353. }
  354. return $totalModels;
  355. }
  356. protected function insertTotals($page, $source){
  357. $order = $source->getOrder();
  358. $totals = $this->_getTotalsList($source);
  359. $lineBlock = array(
  360. 'lines' => array(),
  361. 'height' => 15
  362. );
  363. foreach ($totals as $total) {
  364. $total->setOrder($order)
  365. ->setSource($source);
  366. if ($total->canDisplay()) {
  367. foreach ($total->getTotalsForDisplay() as $totalData) {
  368. $lineBlock['lines'][] = array(
  369. array(
  370. 'text' => $totalData['label'],
  371. 'feed' => 475,
  372. 'align' => 'right',
  373. 'font_size' => $totalData['font_size'],
  374. 'font' => 'bold'
  375. ),
  376. array(
  377. 'text' => $totalData['amount'],
  378. 'feed' => 565,
  379. 'align' => 'right',
  380. 'font_size' => $totalData['font_size'],
  381. 'font' => 'bold'
  382. ),
  383. );
  384. }
  385. }
  386. }
  387. $page = $this->drawLineBlocks($page, array($lineBlock));
  388. return $page;
  389. }
  390. protected function _parseItemDescription($item)
  391. {
  392. $matches = array();
  393. $description = $item->getDescription();
  394. if (preg_match_all('/<li.*?>(.*?)<\/li>/i', $description, $matches)) {
  395. return $matches[1];
  396. }
  397. return array($description);
  398. }
  399. /**
  400. * Before getPdf processing
  401. *
  402. */
  403. protected function _beforeGetPdf() {
  404. $translate = Mage::getSingleton('core/translate');
  405. /* @var $translate Mage_Core_Model_Translate */
  406. $translate->setTranslateInline(false);
  407. }
  408. /**
  409. * After getPdf processing
  410. *
  411. */
  412. protected function _afterGetPdf() {
  413. $translate = Mage::getSingleton('core/translate');
  414. /* @var $translate Mage_Core_Model_Translate */
  415. $translate->setTranslateInline(true);
  416. }
  417. protected function _formatOptionValue($value, $order)
  418. {
  419. $resultValue = '';
  420. if (is_array($value)) {
  421. if (isset($value['qty'])) {
  422. $resultValue .= sprintf('%d', $value['qty']) . ' x ';
  423. }
  424. $resultValue .= $value['title'];
  425. if (isset($value['price'])) {
  426. $resultValue .= " " . $order->formatPrice($value['price']);
  427. }
  428. return $resultValue;
  429. } else {
  430. return $value;
  431. }
  432. }
  433. protected function _initRenderer($type)
  434. {
  435. $node = Mage::getConfig()->getNode('global/pdf/'.$type);
  436. foreach ($node->children() as $renderer) {
  437. $this->_renderers[$renderer->getName()] = array(
  438. 'model' => (string)$renderer,
  439. 'renderer' => null
  440. );
  441. }
  442. }
  443. /**
  444. * Retrieve renderer model
  445. *
  446. * @throws Mage_Core_Exception
  447. * @return Mage_Sales_Model_Order_Pdf_Items_Abstract
  448. */
  449. protected function _getRenderer($type)
  450. {
  451. if (!isset($this->_renderers[$type])) {
  452. $type = 'default';
  453. }
  454. if (!isset($this->_renderers[$type])) {
  455. Mage::throwException(Mage::helper('sales')->__('Invalid renderer model'));
  456. }
  457. if (is_null($this->_renderers[$type]['renderer'])) {
  458. $this->_renderers[$type]['renderer'] = Mage::getSingleton($this->_renderers[$type]['model']);
  459. }
  460. return $this->_renderers[$type]['renderer'];
  461. }
  462. /**
  463. * Public method of protected @see _getRenderer()
  464. *
  465. * Retrieve renderer model
  466. *
  467. * @param string $type
  468. * @return Mage_Sales_Model_Order_Pdf_Items_Abstract
  469. */
  470. public function getRenderer($type)
  471. {
  472. return $this->_getRenderer($type);
  473. }
  474. /**
  475. * Draw Item process
  476. *
  477. * @param Varien_Object $item
  478. * @param Zend_Pdf_Page $page
  479. * @param Mage_Sales_Model_Order $order
  480. * @return Zend_Pdf_Page
  481. */
  482. protected function _drawItem(Varien_Object $item, Zend_Pdf_Page $page, Mage_Sales_Model_Order $order)
  483. {
  484. $type = $item->getOrderItem()->getProductType();
  485. $renderer = $this->_getRenderer($type);
  486. $renderer->setOrder($order);
  487. $renderer->setItem($item);
  488. $renderer->setPdf($this);
  489. $renderer->setPage($page);
  490. $renderer->setRenderedModel($this);
  491. $renderer->draw();
  492. return $renderer->getPage();
  493. }
  494. protected function _setFontRegular($object, $size = 7)
  495. {
  496. $font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir() . '/lib/LinLibertineFont/LinLibertineC_Re-2.8.0.ttf');
  497. $object->setFont($font, $size);
  498. return $font;
  499. }
  500. protected function _setFontBold($object, $size = 7)
  501. {
  502. $font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir() . '/lib/LinLibertineFont/LinLibertine_Bd-2.8.1.ttf');
  503. $object->setFont($font, $size);
  504. return $font;
  505. }
  506. protected function _setFontItalic($object, $size = 7)
  507. {
  508. $font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir() . '/lib/LinLibertineFont/LinLibertine_It-2.8.2.ttf');
  509. $object->setFont($font, $size);
  510. return $font;
  511. }
  512. /**
  513. * Set PDF object
  514. *
  515. * @param Zend_Pdf $pdf
  516. * @return Mage_Sales_Model_Order_Pdf_Abstract
  517. */
  518. protected function _setPdf(Zend_Pdf $pdf)
  519. {
  520. $this->_pdf = $pdf;
  521. return $this;
  522. }
  523. /**
  524. * Retrieve PDF object
  525. *
  526. * @throws Mage_Core_Exception
  527. * @return Zend_Pdf
  528. */
  529. protected function _getPdf()
  530. {
  531. if (!$this->_pdf instanceof Zend_Pdf) {
  532. Mage::throwException(Mage::helper('sales')->__('Please define PDF object before using.'));
  533. }
  534. return $this->_pdf;
  535. }
  536. /**
  537. * Create new page and assign to PDF object
  538. *
  539. * @param array $settings
  540. * @return Zend_Pdf_Page
  541. */
  542. public function newPage(array $settings = array())
  543. {
  544. $pageSize = !empty($settings['page_size']) ? $settings['page_size'] : Zend_Pdf_Page::SIZE_A4;
  545. $page = $this->_getPdf()->newPage($pageSize);
  546. $this->_getPdf()->pages[] = $page;
  547. $this->y = 800;
  548. return $page;
  549. }
  550. /**
  551. * Draw lines
  552. *
  553. * draw items array format:
  554. * lines array;array of line blocks (required)
  555. * shift int; full line height (optional)
  556. * height int;line spacing (default 10)
  557. *
  558. * line block has line columns array
  559. *
  560. * column array format
  561. * text string|array; draw text (required)
  562. * feed int; x position (required)
  563. * font string; font style, optional: bold, italic, regular
  564. * font_file string; path to font file (optional for use your custom font)
  565. * font_size int; font size (default 7)
  566. * align string; text align (also see feed parametr), optional left, right
  567. * height int;line spacing (default 10)
  568. *
  569. * @param Zend_Pdf_Page $page
  570. * @param array $draw
  571. * @param array $pageSettings
  572. * @throws Mage_Core_Exception
  573. * @return Zend_Pdf_Page
  574. */
  575. public function drawLineBlocks(Zend_Pdf_Page $page, array $draw, array $pageSettings = array())
  576. {
  577. foreach ($draw as $itemsProp) {
  578. if (!isset($itemsProp['lines']) || !is_array($itemsProp['lines'])) {
  579. Mage::throwException(Mage::helper('sales')->__('Invalid draw line data. Please define "lines" array.'));
  580. }
  581. $lines = $itemsProp['lines'];
  582. $height = isset($itemsProp['height']) ? $itemsProp['height'] : 10;
  583. if (empty($itemsProp['shift'])) {
  584. $shift = 0;
  585. foreach ($lines as $line) {
  586. $maxHeight = 0;
  587. foreach ($line as $column) {
  588. $lineSpacing = !empty($column['height']) ? $column['height'] : $height;
  589. if (!is_array($column['text'])) {
  590. $column['text'] = array($column['text']);
  591. }
  592. $top = 0;
  593. foreach ($column['text'] as $part) {
  594. $top += $lineSpacing;
  595. }
  596. $maxHeight = $top > $maxHeight ? $top : $maxHeight;
  597. }
  598. $shift += $maxHeight;
  599. }
  600. $itemsProp['shift'] = $shift;
  601. }
  602. if ($this->y - $itemsProp['shift'] < 15) {
  603. $page = $this->newPage($pageSettings);
  604. }
  605. foreach ($lines as $line) {
  606. $maxHeight = 0;
  607. foreach ($line as $column) {
  608. $fontSize = empty($column['font_size']) ? 7 : $column['font_size'];
  609. if (!empty($column['font_file'])) {
  610. $font = Zend_Pdf_Font::fontWithPath($column['font_file']);
  611. $page->setFont($font, $fontSize);
  612. }
  613. else {
  614. $fontStyle = empty($column['font']) ? 'regular' : $column['font'];
  615. switch ($fontStyle) {
  616. case 'bold':
  617. $font = $this->_setFontBold($page, $fontSize);
  618. break;
  619. case 'italic':
  620. $font = $this->_setFontItalic($page, $fontSize);
  621. break;
  622. default:
  623. $font = $this->_setFontRegular($page, $fontSize);
  624. break;
  625. }
  626. }
  627. if (!is_array($column['text'])) {
  628. $column['text'] = array($column['text']);
  629. }
  630. $lineSpacing = !empty($column['height']) ? $column['height'] : $height;
  631. $top = 0;
  632. foreach ($column['text'] as $part) {
  633. $feed = $column['feed'];
  634. $textAlign = empty($column['align']) ? 'left' : $column['align'];
  635. $width = empty($column['width']) ? 0 : $column['width'];
  636. switch ($textAlign) {
  637. case 'right':
  638. if ($width) {
  639. $feed = $this->getAlignRight($part, $feed, $width, $font, $fontSize);
  640. }
  641. else {
  642. $feed = $feed - $this->widthForStringUsingFontSize($part, $font, $fontSize);
  643. }
  644. break;
  645. case 'center':
  646. if ($width) {
  647. $feed = $this->getAlignCenter($part, $feed, $width, $font, $fontSize);
  648. }
  649. break;
  650. }
  651. $page->drawText($part, $feed, $this->y-$top, 'UTF-8');
  652. $top += $lineSpacing;
  653. }
  654. $maxHeight = $top > $maxHeight ? $top : $maxHeight;
  655. }
  656. $this->y -= $maxHeight;
  657. }
  658. }
  659. return $page;
  660. }
  661. }