Skip to content

Commit b6a9f7c

Browse files
committed
fix paper size and add tests for Paper class
1 parent 6da9d8a commit b6a9f7c

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

src/PhpWord/Style/Paper.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord\Style;
1919

20+
use PhpOffice\PhpWord\Shared\Converter;
21+
2022
/**
2123
* Paper size from ISO/IEC 29500-1:2012 pg. 1656-1657
2224
*
@@ -100,6 +102,7 @@ class Paper extends AbstractStyle
100102
'A3' => array(297, 420, 'mm'),
101103
'A4' => array(210, 297, 'mm'),
102104
'A5' => array(148, 210, 'mm'),
105+
'B5' => array(176, 250, 'mm'),
103106
'Folio' => array(8.5, 13, 'in'),
104107
'Legal' => array(8.5, 14, 'in'),
105108
'Letter' => array(8.5, 11, 'in'),
@@ -157,11 +160,14 @@ public function setSize($size)
157160
$this->size = $this->setEnumVal($size, array_keys($this->sizes), $this->size);
158161

159162
list($width, $height, $unit) = $this->sizes[$this->size];
160-
$multipliers = array('mm' => 56.5217, 'in' => 1440);
161-
$multiplier = $multipliers[$unit];
162163

163-
$this->width = (int)round($width * $multiplier);
164-
$this->height = (int)round($height * $multiplier);
164+
if ($unit == 'mm') {
165+
$this->width = Converter::cmToTwip($width / 10);
166+
$this->height = Converter::cmToTwip($height / 10);
167+
} else {
168+
$this->width = Converter::inchToTwip($width);
169+
$this->height = Converter::inchToTwip($height);
170+
}
165171

166172
return $this;
167173
}

tests/PhpWord/Style/PaperTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2016 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Style;
19+
20+
use PhpOffice\PhpWord\PhpWord;
21+
use PhpOffice\PhpWord\TestHelperDOCX;
22+
23+
/**
24+
* Test class for PhpOffice\PhpWord\Style\Paper
25+
*
26+
* @runTestsInSeparateProcesses
27+
*/
28+
class PaperTest extends \PHPUnit_Framework_TestCase
29+
{
30+
/**
31+
* Tear down after each test
32+
*/
33+
public function tearDown()
34+
{
35+
TestHelperDOCX::clear();
36+
}
37+
38+
/**
39+
* Test initiation for paper
40+
*/
41+
public function testInitiation()
42+
{
43+
$object = new Paper();
44+
45+
$this->assertEquals('A4', $object->getSize());
46+
}
47+
48+
/**
49+
* Test paper size for B5 format
50+
*/
51+
public function testB5Size()
52+
{
53+
$object = new Paper('B5');
54+
55+
$this->assertEquals('B5', $object->getSize());
56+
$this->assertEquals(9977.9527559055, $object->getWidth(), '', 0.000000001);
57+
$this->assertEquals(14173.228346457, $object->getHeight(), '', 0.000000001);
58+
}
59+
60+
/**
61+
* Test paper size for Folio format
62+
*/
63+
public function testFolioSize()
64+
{
65+
$object = new Paper();
66+
$object->setSize('Folio');
67+
68+
$this->assertEquals('Folio', $object->getSize());
69+
$this->assertEquals(12240, $object->getWidth(), '', 0.1);
70+
$this->assertEquals(18720, $object->getHeight(), '', 0.1);
71+
}
72+
}

0 commit comments

Comments
 (0)