Skip to content

Commit 2629726

Browse files
committed
magento#11743: [GitHub] AbstractPdf - ZendException font is not set
1 parent d865ef1 commit 2629726

File tree

2 files changed

+128
-19
lines changed

2 files changed

+128
-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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
*/
15+
class AbstractTest extends \PHPUnit\Framework\TestCase
16+
{
17+
/**
18+
* Tests Draw lines method.
19+
* Test case when text block cover more than one page.
20+
*/
21+
public function testDrawLineBlocks()
22+
{
23+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
24+
25+
// Setup most constructor dependencies
26+
$paymentData = $objectManager->create(\Magento\Payment\Helper\Data::class);
27+
$string = $objectManager->create(\Magento\Framework\Stdlib\StringUtils::class);
28+
$scopeConfig = $objectManager->create(\Magento\Framework\App\Config\ScopeConfigInterface::class);
29+
$filesystem = $objectManager->create(\Magento\Framework\Filesystem::class);
30+
$config = $objectManager->create(\Magento\Sales\Model\Order\Pdf\Config::class);
31+
$pdfTotalFactory = $objectManager->create(\Magento\Sales\Model\Order\Pdf\Total\Factory::class);
32+
$pdfItemsFactory = $objectManager->create(\Magento\Sales\Model\Order\Pdf\ItemsFactory::class);
33+
$locale = $objectManager->create(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
34+
$translate = $objectManager->create(\Magento\Framework\Translate\Inline\StateInterface::class);
35+
$addressRenderer = $objectManager->create(\Magento\Sales\Model\Order\Address\Renderer::class);
36+
37+
// Test model
38+
/** @var \Magento\Sales\Model\Order\Pdf\AbstractPdf|MockObject $model */
39+
$model = $this->getMockForAbstractClass(
40+
\Magento\Sales\Model\Order\Pdf\AbstractPdf::class,
41+
[
42+
$paymentData,
43+
$string,
44+
$scopeConfig,
45+
$filesystem,
46+
$config,
47+
$pdfTotalFactory,
48+
$pdfItemsFactory,
49+
$locale,
50+
$translate,
51+
$addressRenderer,
52+
],
53+
'',
54+
true,
55+
true,
56+
true,
57+
['getPdf', '_getPdf']
58+
);
59+
$pdf = new \Zend_Pdf();
60+
$model->expects($this->any())->method('getPdf')->will($this->returnValue($pdf));
61+
$model->expects($this->any())->method('_getPdf')->will($this->returnValue($pdf));
62+
63+
/** Generate multiline block, that cover more than one page */
64+
$lines = [];
65+
for ($lineNumber = 1; $lineNumber <= 100; $lineNumber++ ) {
66+
$lines[] = [[
67+
'feed' => 0,
68+
'font_size' => 10,
69+
'text' => 'Text line ' . $lineNumber,
70+
]];
71+
}
72+
$draw = [[
73+
'height' => 12,
74+
'lines' => $lines,
75+
]];
76+
77+
$page = $model->newPage(['page_size' => \Zend_Pdf_Page::SIZE_A4]);
78+
79+
$model->drawLineBlocks($page, $draw);
80+
$this->assertEquals(
81+
3,
82+
count($pdf->pages)
83+
);
84+
}
85+
}

0 commit comments

Comments
 (0)