Skip to content

Commit e462a63

Browse files
author
Oleksii Korshenko
authored
MAGETWO-85660: #11743: [GitHub] AbstractPdf - ZendException font is not set #1016
2 parents 322bb01 + edf2b41 commit e462a63

File tree

2 files changed

+129
-19
lines changed

2 files changed

+129
-19
lines changed

app/code/Magento/Sales/Model/Order/Pdf/AbstractPdf.php

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ public function newPage(array $settings = [])
953953
* feed int; x position (required)
954954
* font string; font style, optional: bold, italic, regular
955955
* font_file string; path to font file (optional for use your custom font)
956-
* font_size int; font size (default 7)
956+
* font_size int; font size (default 10)
957957
* align string; text align (also see feed parametr), optional left, right
958958
* height int;line spacing (default 10)
959959
*
@@ -1005,24 +1005,8 @@ public function drawLineBlocks(\Zend_Pdf_Page $page, array $draw, array $pageSet
10051005
foreach ($lines as $line) {
10061006
$maxHeight = 0;
10071007
foreach ($line as $column) {
1008-
$fontSize = empty($column['font_size']) ? 10 : $column['font_size'];
1009-
if (!empty($column['font_file'])) {
1010-
$font = \Zend_Pdf_Font::fontWithPath($column['font_file']);
1011-
$page->setFont($font, $fontSize);
1012-
} else {
1013-
$fontStyle = empty($column['font']) ? 'regular' : $column['font'];
1014-
switch ($fontStyle) {
1015-
case 'bold':
1016-
$font = $this->_setFontBold($page, $fontSize);
1017-
break;
1018-
case 'italic':
1019-
$font = $this->_setFontItalic($page, $fontSize);
1020-
break;
1021-
default:
1022-
$font = $this->_setFontRegular($page, $fontSize);
1023-
break;
1024-
}
1025-
}
1008+
$font = $this->setFont($page, $column);
1009+
$fontSize = $column['font_size'];
10261010

10271011
if (!is_array($column['text'])) {
10281012
$column['text'] = [$column['text']];
@@ -1033,6 +1017,8 @@ public function drawLineBlocks(\Zend_Pdf_Page $page, array $draw, array $pageSet
10331017
foreach ($column['text'] as $part) {
10341018
if ($this->y - $lineSpacing < 15) {
10351019
$page = $this->newPage($pageSettings);
1020+
$font = $this->setFont($page, $column);
1021+
$fontSize = $column['font_size'];
10361022
}
10371023

10381024
$feed = $column['feed'];
@@ -1066,4 +1052,42 @@ public function drawLineBlocks(\Zend_Pdf_Page $page, array $draw, array $pageSet
10661052

10671053
return $page;
10681054
}
1055+
1056+
/**
1057+
* Set page font.
1058+
*
1059+
* column array format
1060+
* font string; font style, optional: bold, italic, regular
1061+
* font_file string; path to font file (optional for use your custom font)
1062+
* font_size int; font size (default 10)
1063+
*
1064+
* @param \Zend_Pdf_Page $page
1065+
* @param array $column
1066+
* @return \Zend_Pdf_Resource_Font
1067+
* @throws \Zend_Pdf_Exception
1068+
*/
1069+
private function setFont($page, &$column)
1070+
{
1071+
$fontSize = empty($column['font_size']) ? 10 : $column['font_size'];
1072+
$column['font_size'] = $fontSize;
1073+
if (!empty($column['font_file'])) {
1074+
$font = \Zend_Pdf_Font::fontWithPath($column['font_file']);
1075+
$page->setFont($font, $fontSize);
1076+
} else {
1077+
$fontStyle = empty($column['font']) ? 'regular' : $column['font'];
1078+
switch ($fontStyle) {
1079+
case 'bold':
1080+
$font = $this->_setFontBold($page, $fontSize);
1081+
break;
1082+
case 'italic':
1083+
$font = $this->_setFontItalic($page, $fontSize);
1084+
break;
1085+
default:
1086+
$font = $this->_setFontRegular($page, $fontSize);
1087+
break;
1088+
}
1089+
}
1090+
1091+
return $font;
1092+
}
10691093
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Model\Order\Pdf;
7+
8+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
9+
10+
/**
11+
* Tests Sales Order PDF abstract model.
12+
*
13+
* @see \Magento\Sales\Model\Order\Pdf\AbstarctPdf
14+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15+
*/
16+
class AbstractPdfTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* Tests Draw lines method.
20+
* Test case when text block cover more than one page.
21+
*/
22+
public function testDrawLineBlocks()
23+
{
24+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
25+
26+
// Setup most constructor dependencies
27+
$paymentData = $objectManager->create(\Magento\Payment\Helper\Data::class);
28+
$string = $objectManager->create(\Magento\Framework\Stdlib\StringUtils::class);
29+
$scopeConfig = $objectManager->create(\Magento\Framework\App\Config\ScopeConfigInterface::class);
30+
$filesystem = $objectManager->create(\Magento\Framework\Filesystem::class);
31+
$config = $objectManager->create(\Magento\Sales\Model\Order\Pdf\Config::class);
32+
$pdfTotalFactory = $objectManager->create(\Magento\Sales\Model\Order\Pdf\Total\Factory::class);
33+
$pdfItemsFactory = $objectManager->create(\Magento\Sales\Model\Order\Pdf\ItemsFactory::class);
34+
$locale = $objectManager->create(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
35+
$translate = $objectManager->create(\Magento\Framework\Translate\Inline\StateInterface::class);
36+
$addressRenderer = $objectManager->create(\Magento\Sales\Model\Order\Address\Renderer::class);
37+
38+
// Test model
39+
/** @var \Magento\Sales\Model\Order\Pdf\AbstractPdf|MockObject $model */
40+
$model = $this->getMockForAbstractClass(
41+
\Magento\Sales\Model\Order\Pdf\AbstractPdf::class,
42+
[
43+
$paymentData,
44+
$string,
45+
$scopeConfig,
46+
$filesystem,
47+
$config,
48+
$pdfTotalFactory,
49+
$pdfItemsFactory,
50+
$locale,
51+
$translate,
52+
$addressRenderer,
53+
],
54+
'',
55+
true,
56+
true,
57+
true,
58+
['getPdf', '_getPdf']
59+
);
60+
$pdf = new \Zend_Pdf();
61+
$model->expects($this->any())->method('getPdf')->will($this->returnValue($pdf));
62+
$model->expects($this->any())->method('_getPdf')->will($this->returnValue($pdf));
63+
64+
/** Generate multiline block, that cover more than one page */
65+
$lines = [];
66+
for ($lineNumber = 1; $lineNumber <= 100; $lineNumber++) {
67+
$lines[] = [[
68+
'feed' => 0,
69+
'font_size' => 10,
70+
'text' => 'Text line ' . $lineNumber,
71+
]];
72+
}
73+
$draw = [[
74+
'height' => 12,
75+
'lines' => $lines,
76+
]];
77+
78+
$page = $model->newPage(['page_size' => \Zend_Pdf_Page::SIZE_A4]);
79+
80+
$model->drawLineBlocks($page, $draw);
81+
$this->assertEquals(
82+
3,
83+
count($pdf->pages)
84+
);
85+
}
86+
}

0 commit comments

Comments
 (0)