|
18 | 18 |
|
19 | 19 | namespace PhpOffice\PhpWord\Style; |
20 | 20 |
|
| 21 | +use InvalidArgumentException; |
21 | 22 | use PhpOffice\PhpWord\Shared\Converter; |
22 | 23 |
|
23 | 24 | /** |
@@ -97,16 +98,21 @@ class Paper extends AbstractStyle |
97 | 98 | /** |
98 | 99 | * Paper sizes. |
99 | 100 | * |
100 | | - * @var array |
| 101 | + * @var array<string, array{0: float|int, 1: float|int, 2: string}> |
101 | 102 | */ |
102 | 103 | private $sizes = [ |
103 | 104 | 'A3' => [297, 420, 'mm'], |
104 | 105 | 'A4' => [210, 297, 'mm'], |
105 | 106 | 'A5' => [148, 210, 'mm'], |
| 107 | + 'B4' => [250, 353, 'mm'], |
106 | 108 | 'B5' => [176, 250, 'mm'], |
| 109 | + 'Executive' => [7.25, 10.5, 'in'], |
107 | 110 | 'Folio' => [8.5, 13, 'in'], |
| 111 | + 'Ledger' => [17, 11, 'in'], |
108 | 112 | 'Legal' => [8.5, 14, 'in'], |
109 | 113 | 'Letter' => [8.5, 11, 'in'], |
| 114 | + 'Statement' => [5.5, 8.5, 'in'], |
| 115 | + 'Tabloid' => [11, 17, 'in'], |
110 | 116 | ]; |
111 | 117 |
|
112 | 118 | /** |
@@ -151,24 +157,37 @@ public function getSize() |
151 | 157 | } |
152 | 158 |
|
153 | 159 | /** |
154 | | - * Set size. |
| 160 | + * Set size. Normally called with 1 parameter, corresponding to a key |
| 161 | + * in the $sizes array. However, for sizes not in that table, |
| 162 | + * it can be called with explicit width, height, and unit. |
155 | 163 | * |
156 | 164 | * @param string $size |
| 165 | + * @param float|int $width |
| 166 | + * @param float|int $height |
| 167 | + * @param string $unit |
157 | 168 | * |
158 | 169 | * @return self |
159 | 170 | */ |
160 | | - public function setSize($size) |
| 171 | + public function setSize($size, $width = 0, $height = 0, $unit = '') |
161 | 172 | { |
162 | | - $this->size = $this->setEnumVal($size, array_keys($this->sizes), $this->size); |
163 | | - |
164 | | - [$width, $height, $unit] = $this->sizes[$this->size]; |
| 173 | + if ($width != 0 || $height != 0 || $unit !== '') { |
| 174 | + $this->size = $size; |
| 175 | + } else { |
| 176 | + $this->size = $this->setEnumVal($size, array_keys($this->sizes), $this->size); |
| 177 | + [$width, $height, $unit] = $this->sizes[$this->size]; |
| 178 | + } |
165 | 179 |
|
166 | | - if ($unit == 'mm') { |
| 180 | + if ($width <= 0 || $height <= 0) { |
| 181 | + throw new InvalidArgumentException('width and height must be positive'); |
| 182 | + } |
| 183 | + if ($unit === 'mm') { |
167 | 184 | $this->width = Converter::cmToTwip($width / 10); |
168 | 185 | $this->height = Converter::cmToTwip($height / 10); |
169 | | - } else { |
| 186 | + } elseif ($unit === 'in') { |
170 | 187 | $this->width = Converter::inchToTwip($width); |
171 | 188 | $this->height = Converter::inchToTwip($height); |
| 189 | + } else { |
| 190 | + throw new InvalidArgumentException('unit must be mm or in'); |
172 | 191 | } |
173 | 192 |
|
174 | 193 | return $this; |
|
0 commit comments