Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
AbstractPdf Class Reference
Inheritance diagram for AbstractPdf:
DataObject Creditmemo Invoice Shipment Packaging

Public Member Functions

 getPdf ()
 
 __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)
 
 getAlignRight ($string, $x, $columnWidth, \Zend_Pdf_Resource_Font $font, $fontSize, $padding=5)
 
 getAlignCenter ($string, $x, $columnWidth, \Zend_Pdf_Resource_Font $font, $fontSize)
 
 insertDocumentNumber (\Zend_Pdf_Page $page, $text)
 
 getRenderer ($type)
 
 newPage (array $settings=[])
 
 drawLineBlocks (\Zend_Pdf_Page $page, array $draw, array $pageSettings=[])
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 

Data Fields

 $y
 
const XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID = 'sales_pdf/invoice/put_order_id'
 
const XML_PATH_SALES_PDF_SHIPMENT_PUT_ORDER_ID = 'sales_pdf/shipment/put_order_id'
 
const XML_PATH_SALES_PDF_CREDITMEMO_PUT_ORDER_ID = 'sales_pdf/creditmemo/put_order_id'
 

Protected Member Functions

 insertLogo (&$page, $store=null)
 
 insertAddress (&$page, $store=null)
 
 _formatAddress ($address)
 
 _calcAddressHeight ($address)
 
 insertOrder (&$page, $obj, $putOrderId=true)
 
 _sortTotalsList ($a, $b)
 
 _getTotalsList ()
 
 insertTotals ($page, $source)
 
 _parseItemDescription ($item)
 
 _beforeGetPdf ()
 
 _afterGetPdf ()
 
 _formatOptionValue ($value, $order)
 
 _initRenderer ($type)
 
 _getRenderer ($type)
 
 _drawItem (\Magento\Framework\DataObject $item, \Zend_Pdf_Page $page, \Magento\Sales\Model\Order $order)
 
 _setFontRegular ($object, $size=7)
 
 _setFontBold ($object, $size=7)
 
 _setFontItalic ($object, $size=7)
 
 _setPdf (\Zend_Pdf $pdf)
 
 _getPdf ()
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_renderers = []
 
 $_pdf
 
 $_paymentData
 
 $string
 
 $_localeDate
 
 $_scopeConfig
 
 $_mediaDirectory
 
 $_rootDirectory
 
 $_pdfConfig
 
 $_pdfTotalFactory
 
 $_pdfItemsFactory
 
 $inlineTranslation
 
 $addressRenderer
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Sales Order PDF abstract model

@api @SuppressWarnings(PHPMD.ExcessiveClassComplexity) @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 19 of file AbstractPdf.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
\Magento\Payment\Helper\Data$paymentData
\Magento\Framework\Stdlib\StringUtils$string
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig
\Magento\Framework\Filesystem$filesystem
Config$pdfConfig
Total\Factory$pdfTotalFactory
ItemsFactory$pdfItemsFactory
\Magento\Framework\Stdlib\DateTime\TimezoneInterface$localeDate
\Magento\Framework\Translate\Inline\StateInterface$inlineTranslation
\Magento\Sales\Model\Order\Address\Renderer$addressRenderer
array$data@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 133 of file AbstractPdf.php.

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  }
$filesystem

Member Function Documentation

◆ _afterGetPdf()

_afterGetPdf ( )
protected

After getPdf processing

Returns
void

Definition at line 746 of file AbstractPdf.php.

747  {
748  $this->inlineTranslation->resume();
749  }

◆ _beforeGetPdf()

_beforeGetPdf ( )
protected

Before getPdf processing

Returns
void

Definition at line 736 of file AbstractPdf.php.

737  {
738  $this->inlineTranslation->suspend();
739  }

◆ _calcAddressHeight()

_calcAddressHeight (   $address)
protected

Calculate address height

Parameters
array$address
Returns
int Height @SuppressWarnings(PHPMD.UnusedLocalVariable)

Definition at line 350 of file AbstractPdf.php.

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  }
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$address
Definition: customer.php:38
$value
Definition: gender.phtml:16
$_value
Definition: tax.phtml:15

◆ _drawItem()

_drawItem ( \Magento\Framework\DataObject  $item,
\Zend_Pdf_Page  $page,
\Magento\Sales\Model\Order  $order 
)
protected

Draw Item process

Parameters
\Magento\Framework\DataObject$item
\Zend_Pdf_Page$page
\Magento\Sales\Model\Order$order
Returns
\Zend_Pdf_Page

Definition at line 836 of file AbstractPdf.php.

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  }
$order
Definition: order.php:55
$type
Definition: item.phtml:13
$page
Definition: pages.php:8

◆ _formatAddress()

_formatAddress (   $address)
protected

Format address

Parameters
string$address
Returns
array

Definition at line 329 of file AbstractPdf.php.

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  }
$address
Definition: customer.php:38

◆ _formatOptionValue()

_formatOptionValue (   $value,
  $order 
)
protected

Format option value process

Parameters
array | string$value
\Magento\Sales\Model\Order$order
Returns
string

Definition at line 758 of file AbstractPdf.php.

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  }
$order
Definition: order.php:55
$value
Definition: gender.phtml:16

◆ _getPdf()

_getPdf ( )
protected

Retrieve PDF object

Exceptions

Definition at line 920 of file AbstractPdf.php.

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  }
__()
Definition: __.php:13

◆ _getRenderer()

_getRenderer (   $type)
protected

Retrieve renderer model

Parameters
string$type
Returns
\Magento\Sales\Model\Order\Pdf\Items\AbstractItems
Exceptions

Definition at line 798 of file AbstractPdf.php.

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  }
__()
Definition: __.php:13
$type
Definition: item.phtml:13

◆ _getTotalsList()

_getTotalsList ( )
protected

Return total list

Returns
\Magento\Sales\Model\Order\Pdf\Total\DefaultTotal[] Array of totals

Definition at line 656 of file AbstractPdf.php.

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  }
$_option $_optionId $class
Definition: date.phtml:13
$totals
Definition: totalbar.phtml:10

◆ _initRenderer()

_initRenderer (   $type)
protected

Initialize renderer process

Parameters
string$type
Returns
void

Definition at line 783 of file AbstractPdf.php.

784  {
785  $rendererData = $this->_pdfConfig->getRenderersPerProduct($type);
786  foreach ($rendererData as $productType => $renderer) {
787  $this->_renderers[$productType] = ['model' => $renderer, 'renderer' => null];
788  }
789  }
$type
Definition: item.phtml:13

◆ _parseItemDescription()

_parseItemDescription (   $item)
protected

Parse item description

Parameters
\Magento\Framework\DataObject$item
Returns
array

Definition at line 720 of file AbstractPdf.php.

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  }

◆ _setFontBold()

_setFontBold (   $object,
  $size = 7 
)
protected

Set font as bold

Parameters
\Zend_Pdf_Page$object
int$size
Returns
\Zend_Pdf_Resource_Font

Definition at line 877 of file AbstractPdf.php.

878  {
880  $this->_rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/FreeSerifBold.ttf')
881  );
882  $object->setFont($font, $size);
883  return $font;
884  }
static fontWithPath($filePath, $embeddingOptions=0)
Definition: Font.php:590

◆ _setFontItalic()

_setFontItalic (   $object,
  $size = 7 
)
protected

Set font as italic

Parameters
\Zend_Pdf_Page$object
int$size
Returns
\Zend_Pdf_Resource_Font

Definition at line 893 of file AbstractPdf.php.

894  {
896  $this->_rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/FreeSerifItalic.ttf')
897  );
898  $object->setFont($font, $size);
899  return $font;
900  }
static fontWithPath($filePath, $embeddingOptions=0)
Definition: Font.php:590

◆ _setFontRegular()

_setFontRegular (   $object,
  $size = 7 
)
protected

Set font as regular

Parameters
\Zend_Pdf_Page$object
int$size
Returns
\Zend_Pdf_Resource_Font

Definition at line 861 of file AbstractPdf.php.

862  {
864  $this->_rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/FreeSerif.ttf')
865  );
866  $object->setFont($font, $size);
867  return $font;
868  }
static fontWithPath($filePath, $embeddingOptions=0)
Definition: Font.php:590

◆ _setPdf()

_setPdf ( \Zend_Pdf  $pdf)
protected

Set PDF object

Parameters
\Zend_Pdf$pdf
Returns
$this

Definition at line 908 of file AbstractPdf.php.

909  {
910  $this->_pdf = $pdf;
911  return $this;
912  }

◆ _sortTotalsList()

_sortTotalsList (   $a,
  $b 
)
protected

Sort totals list

Parameters
array$a
array$b
Returns
int

Definition at line 638 of file AbstractPdf.php.

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  }

◆ drawLineBlocks()

drawLineBlocks ( \Zend_Pdf_Page  $page,
array  $draw,
array  $pageSettings = [] 
)

Draw lines

Draw items array format: lines array;array of line blocks (required) shift int; full line height (optional) height int;line spacing (default 10)

line block has line columns array

column array format text string|array; draw text (required) feed int; x position (required) font string; font style, optional: bold, italic, regular font_file string; path to font file (optional for use your custom font) font_size int; font size (default 7) align string; text align (also see feed parameter), optional left, right height int;line spacing (default 10)

Parameters
\Zend_Pdf_Page$page
array$draw
array$pageSettings
Exceptions

Definition at line 973 of file AbstractPdf.php.

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  }
getAlignCenter($string, $x, $columnWidth, \Zend_Pdf_Resource_Font $font, $fontSize)
__()
Definition: __.php:13
static fontWithPath($filePath, $embeddingOptions=0)
Definition: Font.php:590
$page
Definition: pages.php:8
widthForStringUsingFontSize($string, $font, $fontSize)
getAlignRight($string, $x, $columnWidth, \Zend_Pdf_Resource_Font $font, $fontSize, $padding=5)

◆ getAlignCenter()

getAlignCenter (   $string,
  $x,
  $columnWidth,
\Zend_Pdf_Resource_Font  $font,
  $fontSize 
)

Calculate coordinates to draw something in a column aligned to the center

Parameters
string$string
int$x
int$columnWidth
\Zend_Pdf_Resource_Font$font
int$fontSize
Returns
int

Definition at line 223 of file AbstractPdf.php.

224  {
225  $width = $this->widthForStringUsingFontSize($string, $font, $fontSize);
226  return $x + round(($columnWidth - $width) / 2);
227  }
widthForStringUsingFontSize($string, $font, $fontSize)

◆ getAlignRight()

getAlignRight (   $string,
  $x,
  $columnWidth,
\Zend_Pdf_Resource_Font  $font,
  $fontSize,
  $padding = 5 
)

Calculate coordinates to draw something in a column aligned to the right

Parameters
string$string
int$x
int$columnWidth
\Zend_Pdf_Resource_Font$font
int$fontSize
int$padding
Returns
int

Definition at line 207 of file AbstractPdf.php.

208  {
209  $width = $this->widthForStringUsingFontSize($string, $font, $fontSize);
210  return $x + $columnWidth - $width - $padding;
211  }
widthForStringUsingFontSize($string, $font, $fontSize)

◆ getPdf()

getPdf ( )
abstract

Retrieve PDF

Returns
\Zend_Pdf

◆ getRenderer()

getRenderer (   $type)

Public method of protected

See also
_getRenderer()

Retrieve renderer model

Parameters
string$type
Returns
\Magento\Sales\Model\Order\Pdf\Items\AbstractItems

Definition at line 823 of file AbstractPdf.php.

824  {
825  return $this->_getRenderer($type);
826  }
$type
Definition: item.phtml:13

◆ insertAddress()

insertAddress ( $page,
  $store = null 
)
protected

Insert address to pdf page

Parameters
\Zend_Pdf_Page&$page
null$store
Returns
void

Definition at line 291 of file AbstractPdf.php.

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  }
$values
Definition: options.phtml:88
$value
Definition: gender.phtml:16
$page
Definition: pages.php:8
$_value
Definition: tax.phtml:15
getAlignRight($string, $x, $columnWidth, \Zend_Pdf_Resource_Font $font, $fontSize, $padding=5)

◆ insertDocumentNumber()

insertDocumentNumber ( \Zend_Pdf_Page  $page,
  $text 
)

Insert title and number for concrete document type

Parameters
\Zend_Pdf_Page$page
string$text
Returns
void

Definition at line 623 of file AbstractPdf.php.

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  }
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$page
Definition: pages.php:8

◆ insertLogo()

insertLogo ( $page,
  $store = null 
)
protected

Insert logo to pdf page

Parameters
\Zend_Pdf_Page&$page
null$store
Returns
void @SuppressWarnings(PHPMD.CyclomaticComplexity)

Definition at line 237 of file AbstractPdf.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$page
Definition: pages.php:8
static imageWithPath($filePath)
Definition: Image.php:117

◆ insertOrder()

insertOrder ( $page,
  $obj,
  $putOrderId = true 
)
protected

Insert order to pdf page

Parameters
\Zend_Pdf_Page&$page
\Magento\Sales\Model\Order$obj
bool$putOrderId
Returns
void @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity) @SuppressWarnings(PHPMD.ExcessiveMethodLength)

Definition at line 378 of file AbstractPdf.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$track
Definition: details.phtml:12
$billingAddress
Definition: order.php:25
$paymentInfo
$shippingAddress
Definition: order.php:40
$order
Definition: order.php:55
__()
Definition: __.php:13
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$payment
Definition: order.php:17
$value
Definition: gender.phtml:16
$page
Definition: pages.php:8
$shippingMethod
Definition: popup.phtml:12
$_value
Definition: tax.phtml:15
foreach($order->getItems() as $orderItem) $shipment

◆ insertTotals()

insertTotals (   $page,
  $source 
)
protected

Insert totals to pdf page

Parameters
\Zend_Pdf_Page$page
\Magento\Sales\Model\AbstractModel$source
Returns
\Zend_Pdf_Page

Definition at line 678 of file AbstractPdf.php.

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  }
$source
Definition: source.php:23
$order
Definition: order.php:55
$totals
Definition: totalbar.phtml:10
$page
Definition: pages.php:8
drawLineBlocks(\Zend_Pdf_Page $page, array $draw, array $pageSettings=[])

◆ newPage()

newPage ( array  $settings = [])

Create new page and assign to PDF object

Parameters
array$settings
Returns
\Zend_Pdf_Page

Definition at line 935 of file AbstractPdf.php.

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  }
$page
Definition: pages.php:8
$settings
Definition: bootstrap.php:29
const SIZE_A4
Definition: Page.php:54

◆ widthForStringUsingFontSize()

widthForStringUsingFontSize (   $string,
  $font,
  $fontSize 
)

Returns the total width in points of the string using the specified font and size.

This is not the most efficient way to perform this calculation. I'm concentrating optimization efforts on the upcoming layout manager class. Similar calculations exist inside the layout manager class, but widths are generally calculated only after determining line fragments.

Parameters
string$string
\Zend_Pdf_Resource_Font$font
float$fontSizeFont size in points
Returns
float

Definition at line 174 of file AbstractPdf.php.

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  }
$i
Definition: gallery.phtml:31

Field Documentation

◆ $_localeDate

$_localeDate
protected

Definition at line 75 of file AbstractPdf.php.

◆ $_mediaDirectory

$_mediaDirectory
protected

Definition at line 87 of file AbstractPdf.php.

◆ $_paymentData

$_paymentData
protected

Definition at line 65 of file AbstractPdf.php.

◆ $_pdf

$_pdf
protected

Definition at line 51 of file AbstractPdf.php.

◆ $_pdfConfig

$_pdfConfig
protected

Definition at line 97 of file AbstractPdf.php.

◆ $_pdfItemsFactory

$_pdfItemsFactory
protected

Definition at line 107 of file AbstractPdf.php.

◆ $_pdfTotalFactory

$_pdfTotalFactory
protected

Definition at line 102 of file AbstractPdf.php.

◆ $_renderers

$_renderers = []
protected

Definition at line 35 of file AbstractPdf.php.

◆ $_rootDirectory

$_rootDirectory
protected

Definition at line 92 of file AbstractPdf.php.

◆ $_scopeConfig

$_scopeConfig
protected

Definition at line 82 of file AbstractPdf.php.

◆ $addressRenderer

$addressRenderer
protected

Definition at line 117 of file AbstractPdf.php.

◆ $inlineTranslation

$inlineTranslation
protected

Definition at line 112 of file AbstractPdf.php.

◆ $string

$string
protected

Definition at line 70 of file AbstractPdf.php.

◆ $y

$y

Definition at line 26 of file AbstractPdf.php.

◆ XML_PATH_SALES_PDF_CREDITMEMO_PUT_ORDER_ID

const XML_PATH_SALES_PDF_CREDITMEMO_PUT_ORDER_ID = 'sales_pdf/creditmemo/put_order_id'

Definition at line 44 of file AbstractPdf.php.

◆ XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID

const XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID = 'sales_pdf/invoice/put_order_id'

Predefined constants

Definition at line 40 of file AbstractPdf.php.

◆ XML_PATH_SALES_PDF_SHIPMENT_PUT_ORDER_ID

const XML_PATH_SALES_PDF_SHIPMENT_PUT_ORDER_ID = 'sales_pdf/shipment/put_order_id'

Definition at line 42 of file AbstractPdf.php.


The documentation for this class was generated from the following file: