Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractPdf.php
Go to the documentation of this file.
1 <?php
8 
10 
20 {
26  public $y;
27 
35  protected $_renderers = [];
36 
40  const XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID = 'sales_pdf/invoice/put_order_id';
41 
42  const XML_PATH_SALES_PDF_SHIPMENT_PUT_ORDER_ID = 'sales_pdf/shipment/put_order_id';
43 
44  const XML_PATH_SALES_PDF_CREDITMEMO_PUT_ORDER_ID = 'sales_pdf/creditmemo/put_order_id';
45 
51  protected $_pdf;
52 
58  abstract public function getPdf();
59 
65  protected $_paymentData;
66 
70  protected $string;
71 
75  protected $_localeDate;
76 
82  protected $_scopeConfig;
83 
87  protected $_mediaDirectory;
88 
92  protected $_rootDirectory;
93 
97  protected $_pdfConfig;
98 
102  protected $_pdfTotalFactory;
103 
107  protected $_pdfItemsFactory;
108 
113 
117  protected $addressRenderer;
118 
133  public function __construct(
134  \Magento\Payment\Helper\Data $paymentData,
135  \Magento\Framework\Stdlib\StringUtils $string,
136  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
137  \Magento\Framework\Filesystem $filesystem,
138  Config $pdfConfig,
139  \Magento\Sales\Model\Order\Pdf\Total\Factory $pdfTotalFactory,
140  \Magento\Sales\Model\Order\Pdf\ItemsFactory $pdfItemsFactory,
141  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
142  \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
144  array $data = []
145  ) {
146  $this->addressRenderer = $addressRenderer;
147  $this->_paymentData = $paymentData;
148  $this->_localeDate = $localeDate;
149  $this->string = $string;
150  $this->_scopeConfig = $scopeConfig;
151  $this->_mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
152  $this->_rootDirectory = $filesystem->getDirectoryRead(DirectoryList::ROOT);
153  $this->_pdfConfig = $pdfConfig;
154  $this->_pdfTotalFactory = $pdfTotalFactory;
155  $this->_pdfItemsFactory = $pdfItemsFactory;
156  $this->inlineTranslation = $inlineTranslation;
157  parent::__construct($data);
158  }
159 
174  public function widthForStringUsingFontSize($string, $font, $fontSize)
175  {
176  $drawingString = '"libiconv"' == ICONV_IMPL ? iconv(
177  'UTF-8',
178  'UTF-16BE//IGNORE',
179  $string
180  ) : @iconv(
181  'UTF-8',
182  'UTF-16BE',
183  $string
184  );
185 
186  $characters = [];
187  for ($i = 0; $i < strlen($drawingString); $i++) {
188  $characters[] = ord($drawingString[$i++]) << 8 | ord($drawingString[$i]);
189  }
190  $glyphs = $font->glyphNumbersForCharacters($characters);
191  $widths = $font->widthsForGlyphs($glyphs);
192  $stringWidth = array_sum($widths) / $font->getUnitsPerEm() * $fontSize;
193  return $stringWidth;
194  }
195 
207  public function getAlignRight($string, $x, $columnWidth, \Zend_Pdf_Resource_Font $font, $fontSize, $padding = 5)
208  {
209  $width = $this->widthForStringUsingFontSize($string, $font, $fontSize);
210  return $x + $columnWidth - $width - $padding;
211  }
212 
223  public function getAlignCenter($string, $x, $columnWidth, \Zend_Pdf_Resource_Font $font, $fontSize)
224  {
225  $width = $this->widthForStringUsingFontSize($string, $font, $fontSize);
226  return $x + round(($columnWidth - $width) / 2);
227  }
228 
237  protected function insertLogo(&$page, $store = null)
238  {
239  $this->y = $this->y ? $this->y : 815;
240  $image = $this->_scopeConfig->getValue(
241  'sales/identity/logo',
242  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
243  $store
244  );
245  if ($image) {
246  $imagePath = '/sales/store/logo/' . $image;
247  if ($this->_mediaDirectory->isFile($imagePath)) {
248  $image = \Zend_Pdf_Image::imageWithPath($this->_mediaDirectory->getAbsolutePath($imagePath));
249  $top = 830;
250  //top border of the page
251  $widthLimit = 270;
252  //half of the page width
253  $heightLimit = 270;
254  //assuming the image is not a "skyscraper"
255  $width = $image->getPixelWidth();
256  $height = $image->getPixelHeight();
257 
258  //preserving aspect ratio (proportions)
259  $ratio = $width / $height;
260  if ($ratio > 1 && $width > $widthLimit) {
261  $width = $widthLimit;
262  $height = $width / $ratio;
263  } elseif ($ratio < 1 && $height > $heightLimit) {
264  $height = $heightLimit;
265  $width = $height * $ratio;
266  } elseif ($ratio == 1 && $height > $heightLimit) {
267  $height = $heightLimit;
268  $width = $widthLimit;
269  }
270 
271  $y1 = $top - $height;
272  $y2 = $top;
273  $x1 = 25;
274  $x2 = $x1 + $width;
275 
276  //coordinates after transformation are rounded by Zend
277  $page->drawImage($image, $x1, $y1, $x2, $y2);
278 
279  $this->y = $y1 - 10;
280  }
281  }
282  }
283 
291  protected function insertAddress(&$page, $store = null)
292  {
293  $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
294  $font = $this->_setFontRegular($page, 10);
295  $page->setLineWidth(0);
296  $this->y = $this->y ? $this->y : 815;
297  $top = 815;
298  $values = explode(
299  "\n",
300  $this->_scopeConfig->getValue(
301  'sales/identity/address',
302  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
303  $store
304  )
305  );
306  foreach ($values as $value) {
307  if ($value !== '') {
308  $value = preg_replace('/<br[^>]*>/i', "\n", $value);
309  foreach ($this->string->split($value, 45, true, true) as $_value) {
310  $page->drawText(
311  trim(strip_tags($_value)),
312  $this->getAlignRight($_value, 130, 440, $font, 10),
313  $top,
314  'UTF-8'
315  );
316  $top -= 10;
317  }
318  }
319  }
320  $this->y = $this->y > $top ? $top : $this->y;
321  }
322 
329  protected function _formatAddress($address)
330  {
331  $return = [];
332  foreach (explode('|', $address) as $str) {
333  foreach ($this->string->split($str, 45, true, true) as $part) {
334  if (empty($part)) {
335  continue;
336  }
337  $return[] = $part;
338  }
339  }
340  return $return;
341  }
342 
350  protected function _calcAddressHeight($address)
351  {
352  $y = 0;
353  foreach ($address as $value) {
354  if ($value !== '') {
355  $text = [];
356  foreach ($this->string->split($value, 55, true, true) as $_value) {
357  $text[] = $_value;
358  }
359  foreach ($text as $part) {
360  $y += 15;
361  }
362  }
363  }
364  return $y;
365  }
366 
378  protected function insertOrder(&$page, $obj, $putOrderId = true)
379  {
380  if ($obj instanceof \Magento\Sales\Model\Order) {
381  $shipment = null;
382  $order = $obj;
383  } elseif ($obj instanceof \Magento\Sales\Model\Order\Shipment) {
384  $shipment = $obj;
385  $order = $shipment->getOrder();
386  }
387 
388  $this->y = $this->y ? $this->y : 815;
389  $top = $this->y;
390 
391  $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0.45));
392  $page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.45));
393  $page->drawRectangle(25, $top, 570, $top - 55);
394  $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1));
395  $this->setDocHeaderCoordinates([25, $top, 570, $top - 55]);
396  $this->_setFontRegular($page, 10);
397 
398  if ($putOrderId) {
399  $page->drawText(__('Order # ') . $order->getRealOrderId(), 35, $top -= 30, 'UTF-8');
400  $top +=15;
401  }
402 
403  $top -=30;
404  $page->drawText(
405  __('Order Date: ') .
406  $this->_localeDate->formatDate(
407  $this->_localeDate->scopeDate(
408  $order->getStore(),
409  $order->getCreatedAt(),
410  true
411  ),
412  \IntlDateFormatter::MEDIUM,
413  false
414  ),
415  35,
416  $top,
417  'UTF-8'
418  );
419 
420  $top -= 10;
421  $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
422  $page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.5));
423  $page->setLineWidth(0.5);
424  $page->drawRectangle(25, $top, 275, $top - 25);
425  $page->drawRectangle(275, $top, 570, $top - 25);
426 
427  /* Calculate blocks info */
428 
429  /* Billing Address */
430  $billingAddress = $this->_formatAddress($this->addressRenderer->format($order->getBillingAddress(), 'pdf'));
431 
432  /* Payment */
433  $paymentInfo = $this->_paymentData->getInfoBlock($order->getPayment())->setIsSecureMode(true)->toPdf();
434  $paymentInfo = htmlspecialchars_decode($paymentInfo, ENT_QUOTES);
435  $payment = explode('{{pdf_row_separator}}', $paymentInfo);
436  foreach ($payment as $key => $value) {
437  if (strip_tags(trim($value)) == '') {
438  unset($payment[$key]);
439  }
440  }
441  reset($payment);
442 
443  /* Shipping Address and Method */
444  if (!$order->getIsVirtual()) {
445  /* Shipping Address */
447  $this->addressRenderer->format($order->getShippingAddress(), 'pdf')
448  );
449  $shippingMethod = $order->getShippingDescription();
450  }
451 
452  $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
453  $this->_setFontBold($page, 12);
454  $page->drawText(__('Sold to:'), 35, $top - 15, 'UTF-8');
455 
456  if (!$order->getIsVirtual()) {
457  $page->drawText(__('Ship to:'), 285, $top - 15, 'UTF-8');
458  } else {
459  $page->drawText(__('Payment Method:'), 285, $top - 15, 'UTF-8');
460  }
461 
462  $addressesHeight = $this->_calcAddressHeight($billingAddress);
463  if (isset($shippingAddress)) {
464  $addressesHeight = max($addressesHeight, $this->_calcAddressHeight($shippingAddress));
465  }
466 
467  $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1));
468  $page->drawRectangle(25, $top - 25, 570, $top - 33 - $addressesHeight);
469  $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
470  $this->_setFontRegular($page, 10);
471  $this->y = $top - 40;
472  $addressesStartY = $this->y;
473 
474  foreach ($billingAddress as $value) {
475  if ($value !== '') {
476  $text = [];
477  foreach ($this->string->split($value, 45, true, true) as $_value) {
478  $text[] = $_value;
479  }
480  foreach ($text as $part) {
481  $page->drawText(strip_tags(ltrim($part)), 35, $this->y, 'UTF-8');
482  $this->y -= 15;
483  }
484  }
485  }
486 
487  $addressesEndY = $this->y;
488 
489  if (!$order->getIsVirtual()) {
490  $this->y = $addressesStartY;
491  foreach ($shippingAddress as $value) {
492  if ($value !== '') {
493  $text = [];
494  foreach ($this->string->split($value, 45, true, true) as $_value) {
495  $text[] = $_value;
496  }
497  foreach ($text as $part) {
498  $page->drawText(strip_tags(ltrim($part)), 285, $this->y, 'UTF-8');
499  $this->y -= 15;
500  }
501  }
502  }
503 
504  $addressesEndY = min($addressesEndY, $this->y);
505  $this->y = $addressesEndY;
506 
507  $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
508  $page->setLineWidth(0.5);
509  $page->drawRectangle(25, $this->y, 275, $this->y - 25);
510  $page->drawRectangle(275, $this->y, 570, $this->y - 25);
511 
512  $this->y -= 15;
513  $this->_setFontBold($page, 12);
514  $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
515  $page->drawText(__('Payment Method'), 35, $this->y, 'UTF-8');
516  $page->drawText(__('Shipping Method:'), 285, $this->y, 'UTF-8');
517 
518  $this->y -= 10;
519  $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1));
520 
521  $this->_setFontRegular($page, 10);
522  $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
523 
524  $paymentLeft = 35;
525  $yPayments = $this->y - 15;
526  } else {
527  $yPayments = $addressesStartY;
528  $paymentLeft = 285;
529  }
530 
531  foreach ($payment as $value) {
532  if (trim($value) != '') {
533  //Printing "Payment Method" lines
534  $value = preg_replace('/<br[^>]*>/i', "\n", $value);
535  foreach ($this->string->split($value, 45, true, true) as $_value) {
536  $page->drawText(strip_tags(trim($_value)), $paymentLeft, $yPayments, 'UTF-8');
537  $yPayments -= 15;
538  }
539  }
540  }
541 
542  if ($order->getIsVirtual()) {
543  // replacement of Shipments-Payments rectangle block
544  $yPayments = min($addressesEndY, $yPayments);
545  $page->drawLine(25, $top - 25, 25, $yPayments);
546  $page->drawLine(570, $top - 25, 570, $yPayments);
547  $page->drawLine(25, $yPayments, 570, $yPayments);
548 
549  $this->y = $yPayments - 15;
550  } else {
551  $topMargin = 15;
552  $methodStartY = $this->y;
553  $this->y -= 15;
554 
555  foreach ($this->string->split($shippingMethod, 45, true, true) as $_value) {
556  $page->drawText(strip_tags(trim($_value)), 285, $this->y, 'UTF-8');
557  $this->y -= 15;
558  }
559 
560  $yShipments = $this->y;
561  $totalShippingChargesText = "("
562  . __('Total Shipping Charges')
563  . " "
564  . $order->formatPriceTxt($order->getShippingAmount())
565  . ")";
566 
567  $page->drawText($totalShippingChargesText, 285, $yShipments - $topMargin, 'UTF-8');
568  $yShipments -= $topMargin + 10;
569 
570  $tracks = [];
571  if ($shipment) {
572  $tracks = $shipment->getAllTracks();
573  }
574  if (count($tracks)) {
575  $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
576  $page->setLineWidth(0.5);
577  $page->drawRectangle(285, $yShipments, 510, $yShipments - 10);
578  $page->drawLine(400, $yShipments, 400, $yShipments - 10);
579  //$page->drawLine(510, $yShipments, 510, $yShipments - 10);
580 
581  $this->_setFontRegular($page, 9);
582  $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
583  //$page->drawText(__('Carrier'), 290, $yShipments - 7 , 'UTF-8');
584  $page->drawText(__('Title'), 290, $yShipments - 7, 'UTF-8');
585  $page->drawText(__('Number'), 410, $yShipments - 7, 'UTF-8');
586 
587  $yShipments -= 20;
588  $this->_setFontRegular($page, 8);
589  foreach ($tracks as $track) {
590  $maxTitleLen = 45;
591  $endOfTitle = strlen($track->getTitle()) > $maxTitleLen ? '...' : '';
592  $truncatedTitle = substr($track->getTitle(), 0, $maxTitleLen) . $endOfTitle;
593  $page->drawText($truncatedTitle, 292, $yShipments, 'UTF-8');
594  $page->drawText($track->getNumber(), 410, $yShipments, 'UTF-8');
595  $yShipments -= $topMargin - 5;
596  }
597  } else {
598  $yShipments -= $topMargin - 5;
599  }
600 
601  $currentY = min($yPayments, $yShipments);
602 
603  // replacement of Shipments-Payments rectangle block
604  $page->drawLine(25, $methodStartY, 25, $currentY);
605  //left
606  $page->drawLine(25, $currentY, 570, $currentY);
607  //bottom
608  $page->drawLine(570, $currentY, 570, $methodStartY);
609  //right
610 
611  $this->y = $currentY;
612  $this->y -= 15;
613  }
614  }
615 
624  {
625  $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1));
626  $this->_setFontRegular($page, 10);
627  $docHeader = $this->getDocHeaderCoordinates();
628  $page->drawText($text, 35, $docHeader[1] - 15, 'UTF-8');
629  }
630 
638  protected function _sortTotalsList($a, $b)
639  {
640  if (!isset($a['sort_order']) || !isset($b['sort_order'])) {
641  return 0;
642  }
643 
644  if ($a['sort_order'] == $b['sort_order']) {
645  return 0;
646  }
647 
648  return $a['sort_order'] > $b['sort_order'] ? 1 : -1;
649  }
650 
656  protected function _getTotalsList()
657  {
658  $totals = $this->_pdfConfig->getTotals();
659  usort($totals, [$this, '_sortTotalsList']);
660  $totalModels = [];
661  foreach ($totals as $totalInfo) {
662  $class = empty($totalInfo['model']) ? null : $totalInfo['model'];
663  $totalModel = $this->_pdfTotalFactory->create($class);
664  $totalModel->setData($totalInfo);
665  $totalModels[] = $totalModel;
666  }
667 
668  return $totalModels;
669  }
670 
678  protected function insertTotals($page, $source)
679  {
680  $order = $source->getOrder();
681  $totals = $this->_getTotalsList();
682  $lineBlock = ['lines' => [], 'height' => 15];
683  foreach ($totals as $total) {
684  $total->setOrder($order)->setSource($source);
685 
686  if ($total->canDisplay()) {
687  $total->setFontSize(10);
688  foreach ($total->getTotalsForDisplay() as $totalData) {
689  $lineBlock['lines'][] = [
690  [
691  'text' => $totalData['label'],
692  'feed' => 475,
693  'align' => 'right',
694  'font_size' => $totalData['font_size'],
695  'font' => 'bold',
696  ],
697  [
698  'text' => $totalData['amount'],
699  'feed' => 565,
700  'align' => 'right',
701  'font_size' => $totalData['font_size'],
702  'font' => 'bold'
703  ],
704  ];
705  }
706  }
707  }
708 
709  $this->y -= 20;
710  $page = $this->drawLineBlocks($page, [$lineBlock]);
711  return $page;
712  }
713 
720  protected function _parseItemDescription($item)
721  {
722  $matches = [];
723  $description = $item->getDescription();
724  if (preg_match_all('/<li.*?>(.*?)<\/li>/i', $description, $matches)) {
725  return $matches[1];
726  }
727 
728  return [$description];
729  }
730 
736  protected function _beforeGetPdf()
737  {
738  $this->inlineTranslation->suspend();
739  }
740 
746  protected function _afterGetPdf()
747  {
748  $this->inlineTranslation->resume();
749  }
750 
758  protected function _formatOptionValue($value, $order)
759  {
760  $resultValue = '';
761  if (is_array($value)) {
762  if (isset($value['qty'])) {
763  $resultValue .= sprintf('%d', $value['qty']) . ' x ';
764  }
765 
766  $resultValue .= $value['title'];
767 
768  if (isset($value['price'])) {
769  $resultValue .= " " . $order->formatPrice($value['price']);
770  }
771  return $resultValue;
772  } else {
773  return $value;
774  }
775  }
776 
783  protected function _initRenderer($type)
784  {
785  $rendererData = $this->_pdfConfig->getRenderersPerProduct($type);
786  foreach ($rendererData as $productType => $renderer) {
787  $this->_renderers[$productType] = ['model' => $renderer, 'renderer' => null];
788  }
789  }
790 
798  protected function _getRenderer($type)
799  {
800  if (!isset($this->_renderers[$type])) {
801  $type = 'default';
802  }
803 
804  if (!isset($this->_renderers[$type])) {
805  throw new \Magento\Framework\Exception\LocalizedException(__('We found an invalid renderer model.'));
806  }
807 
808  if ($this->_renderers[$type]['renderer'] === null) {
809  $this->_renderers[$type]['renderer'] = $this->_pdfItemsFactory->get($this->_renderers[$type]['model']);
810  }
811 
812  return $this->_renderers[$type]['renderer'];
813  }
814 
823  public function getRenderer($type)
824  {
825  return $this->_getRenderer($type);
826  }
827 
836  protected function _drawItem(
837  \Magento\Framework\DataObject $item,
839  \Magento\Sales\Model\Order $order
840  ) {
841  $type = $item->getOrderItem()->getProductType();
842  $renderer = $this->_getRenderer($type);
843  $renderer->setOrder($order);
844  $renderer->setItem($item);
845  $renderer->setPdf($this);
846  $renderer->setPage($page);
847  $renderer->setRenderedModel($this);
848 
849  $renderer->draw();
850 
851  return $renderer->getPage();
852  }
853 
861  protected function _setFontRegular($object, $size = 7)
862  {
864  $this->_rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/FreeSerif.ttf')
865  );
866  $object->setFont($font, $size);
867  return $font;
868  }
869 
877  protected function _setFontBold($object, $size = 7)
878  {
880  $this->_rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/FreeSerifBold.ttf')
881  );
882  $object->setFont($font, $size);
883  return $font;
884  }
885 
893  protected function _setFontItalic($object, $size = 7)
894  {
896  $this->_rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/FreeSerifItalic.ttf')
897  );
898  $object->setFont($font, $size);
899  return $font;
900  }
901 
908  protected function _setPdf(\Zend_Pdf $pdf)
909  {
910  $this->_pdf = $pdf;
911  return $this;
912  }
913 
920  protected function _getPdf()
921  {
922  if (!$this->_pdf instanceof \Zend_Pdf) {
923  throw new \Magento\Framework\Exception\LocalizedException(__('Please define the PDF object before using.'));
924  }
925 
926  return $this->_pdf;
927  }
928 
935  public function newPage(array $settings = [])
936  {
937  $pageSize = !empty($settings['page_size']) ? $settings['page_size'] : \Zend_Pdf_Page::SIZE_A4;
938  $page = $this->_getPdf()->newPage($pageSize);
939  $this->_getPdf()->pages[] = $page;
940  $this->y = 800;
941 
942  return $page;
943  }
944 
973  public function drawLineBlocks(\Zend_Pdf_Page $page, array $draw, array $pageSettings = [])
974  {
975  foreach ($draw as $itemsProp) {
976  if (!isset($itemsProp['lines']) || !is_array($itemsProp['lines'])) {
977  throw new \Magento\Framework\Exception\LocalizedException(
978  __('We don\'t recognize the draw line data. Please define the "lines" array.')
979  );
980  }
981  $lines = $itemsProp['lines'];
982  $height = isset($itemsProp['height']) ? $itemsProp['height'] : 10;
983 
984  if (empty($itemsProp['shift'])) {
985  $shift = 0;
986  foreach ($lines as $line) {
987  $maxHeight = 0;
988  foreach ($line as $column) {
989  $lineSpacing = !empty($column['height']) ? $column['height'] : $height;
990  if (!is_array($column['text'])) {
991  $column['text'] = [$column['text']];
992  }
993  $top = 0;
994  foreach ($column['text'] as $part) {
995  $top += $lineSpacing;
996  }
997 
998  $maxHeight = $top > $maxHeight ? $top : $maxHeight;
999  }
1000  $shift += $maxHeight;
1001  }
1002  $itemsProp['shift'] = $shift;
1003  }
1004 
1005  if ($this->y - $itemsProp['shift'] < 15) {
1006  $page = $this->newPage($pageSettings);
1007  }
1008 
1009  foreach ($lines as $line) {
1010  $maxHeight = 0;
1011  foreach ($line as $column) {
1012  $fontSize = empty($column['font_size']) ? 10 : $column['font_size'];
1013  if (!empty($column['font_file'])) {
1014  $font = \Zend_Pdf_Font::fontWithPath($column['font_file']);
1015  $page->setFont($font, $fontSize);
1016  } else {
1017  $fontStyle = empty($column['font']) ? 'regular' : $column['font'];
1018  switch ($fontStyle) {
1019  case 'bold':
1020  $font = $this->_setFontBold($page, $fontSize);
1021  break;
1022  case 'italic':
1023  $font = $this->_setFontItalic($page, $fontSize);
1024  break;
1025  default:
1026  $font = $this->_setFontRegular($page, $fontSize);
1027  break;
1028  }
1029  }
1030 
1031  if (!is_array($column['text'])) {
1032  $column['text'] = [$column['text']];
1033  }
1034 
1035  $lineSpacing = !empty($column['height']) ? $column['height'] : $height;
1036  $top = 0;
1037  foreach ($column['text'] as $part) {
1038  if ($this->y - $lineSpacing < 15) {
1039  $page = $this->newPage($pageSettings);
1040  }
1041 
1042  $feed = $column['feed'];
1043  $textAlign = empty($column['align']) ? 'left' : $column['align'];
1044  $width = empty($column['width']) ? 0 : $column['width'];
1045  switch ($textAlign) {
1046  case 'right':
1047  if ($width) {
1048  $feed = $this->getAlignRight($part, $feed, $width, $font, $fontSize);
1049  } else {
1050  $feed = $feed - $this->widthForStringUsingFontSize($part, $font, $fontSize);
1051  }
1052  break;
1053  case 'center':
1054  if ($width) {
1055  $feed = $this->getAlignCenter($part, $feed, $width, $font, $fontSize);
1056  }
1057  break;
1058  default:
1059  break;
1060  }
1061  $page->drawText($part, $feed, $this->y - $top, 'UTF-8');
1062  $top += $lineSpacing;
1063  }
1064 
1065  $maxHeight = $top > $maxHeight ? $top : $maxHeight;
1066  }
1067  $this->y -= $maxHeight;
1068  }
1069  }
1070 
1071  return $page;
1072  }
1073 }
getAlignCenter($string, $x, $columnWidth, \Zend_Pdf_Resource_Font $font, $fontSize)
_drawItem(\Magento\Framework\DataObject $item, \Zend_Pdf_Page $page, \Magento\Sales\Model\Order $order)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$track
Definition: details.phtml:12
$billingAddress
Definition: order.php:25
$source
Definition: source.php:23
$paymentInfo
$shippingAddress
Definition: order.php:40
$order
Definition: order.php:55
$values
Definition: options.phtml:88
__()
Definition: __.php:13
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$address
Definition: customer.php:38
$payment
Definition: order.php:17
$type
Definition: item.phtml:13
static fontWithPath($filePath, $embeddingOptions=0)
Definition: Font.php:590
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
$totals
Definition: totalbar.phtml:10
insertDocumentNumber(\Zend_Pdf_Page $page, $text)
$page
Definition: pages.php:8
drawLineBlocks(\Zend_Pdf_Page $page, array $draw, array $pageSettings=[])
static imageWithPath($filePath)
Definition: Image.php:117
__construct(\Magento\Payment\Helper\Data $paymentData, \Magento\Framework\Stdlib\StringUtils $string, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Filesystem $filesystem, Config $pdfConfig, \Magento\Sales\Model\Order\Pdf\Total\Factory $pdfTotalFactory, \Magento\Sales\Model\Order\Pdf\ItemsFactory $pdfItemsFactory, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Sales\Model\Order\Address\Renderer $addressRenderer, array $data=[])
widthForStringUsingFontSize($string, $font, $fontSize)
$settings
Definition: bootstrap.php:29
$shippingMethod
Definition: popup.phtml:12
$_value
Definition: tax.phtml:15
$filesystem
$i
Definition: gallery.phtml:31
foreach($order->getItems() as $orderItem) $shipment
getAlignRight($string, $x, $columnWidth, \Zend_Pdf_Resource_Font $font, $fontSize, $padding=5)
insertOrder(&$page, $obj, $putOrderId=true)
const SIZE_A4
Definition: Page.php:54