1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Reader\Gnumeric; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\PageMargins; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup as WorksheetPageSetup; |
9
|
|
|
use SimpleXMLElement; |
10
|
|
|
|
11
|
|
|
class PageSetup |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Spreadsheet |
15
|
|
|
*/ |
16
|
|
|
private $spreadsheet; |
17
|
|
|
|
18
|
10 |
|
public function __construct(Spreadsheet $spreadsheet) |
19
|
|
|
{ |
20
|
10 |
|
$this->spreadsheet = $spreadsheet; |
21
|
10 |
|
} |
22
|
|
|
|
23
|
10 |
|
public function printInformation(SimpleXMLElement $sheet): self |
24
|
|
|
{ |
25
|
10 |
|
if (isset($sheet->PrintInformation)) { |
26
|
10 |
|
$printInformation = $sheet->PrintInformation[0]; |
27
|
10 |
|
if (!$printInformation) { |
|
|
|
|
28
|
|
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
10 |
|
$scale = (string) $printInformation->Scale->attributes()['percentage']; |
32
|
10 |
|
$pageOrder = (string) $printInformation->order; |
33
|
10 |
|
$orientation = (string) $printInformation->orientation; |
34
|
10 |
|
$horizontalCentered = (string) $printInformation->hcenter->attributes()['value']; |
35
|
10 |
|
$verticalCentered = (string) $printInformation->vcenter->attributes()['value']; |
36
|
|
|
|
37
|
10 |
|
$this->spreadsheet->getActiveSheet()->getPageSetup() |
38
|
10 |
|
->setPageOrder($pageOrder === 'r_then_d' ? WorksheetPageSetup::PAGEORDER_OVER_THEN_DOWN : WorksheetPageSetup::PAGEORDER_DOWN_THEN_OVER) |
39
|
10 |
|
->setScale((int) $scale) |
40
|
10 |
|
->setOrientation($orientation ?? WorksheetPageSetup::ORIENTATION_DEFAULT) |
41
|
10 |
|
->setHorizontalCentered((bool) $horizontalCentered) |
42
|
10 |
|
->setVerticalCentered((bool) $verticalCentered); |
43
|
|
|
} |
44
|
|
|
|
45
|
10 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
10 |
|
public function sheetMargins(SimpleXMLElement $sheet): self |
49
|
|
|
{ |
50
|
10 |
|
if (isset($sheet->PrintInformation, $sheet->PrintInformation->Margins)) { |
51
|
|
|
$marginSet = [ |
52
|
|
|
// Default Settings |
53
|
10 |
|
'top' => 0.75, |
54
|
|
|
'header' => 0.3, |
55
|
|
|
'left' => 0.7, |
56
|
|
|
'right' => 0.7, |
57
|
|
|
'bottom' => 0.75, |
58
|
|
|
'footer' => 0.3, |
59
|
|
|
]; |
60
|
|
|
|
61
|
10 |
|
$marginSet = $this->buildMarginSet($sheet, $marginSet); |
62
|
10 |
|
$this->adjustMargins($marginSet); |
63
|
|
|
} |
64
|
|
|
|
65
|
10 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
10 |
|
private function buildMarginSet(SimpleXMLElement $sheet, array $marginSet): array |
69
|
|
|
{ |
70
|
10 |
|
foreach ($sheet->PrintInformation->Margins->children(Gnumeric::NAMESPACE_GNM) as $key => $margin) { |
71
|
10 |
|
$marginAttributes = $margin->attributes(); |
72
|
10 |
|
$marginSize = ($marginAttributes['Points']) ?? 72; // Default is 72pt |
73
|
|
|
// Convert value in points to inches |
74
|
10 |
|
$marginSize = PageMargins::fromPoints((float) $marginSize); |
75
|
10 |
|
$marginSet[$key] = $marginSize; |
76
|
|
|
} |
77
|
|
|
|
78
|
10 |
|
return $marginSet; |
79
|
|
|
} |
80
|
|
|
|
81
|
10 |
|
private function adjustMargins(array $marginSet): void |
82
|
|
|
{ |
83
|
10 |
|
foreach ($marginSet as $key => $marginSize) { |
84
|
|
|
// Gnumeric is quirky in the way it displays the header/footer values: |
85
|
|
|
// header is actually the sum of top and header; footer is actually the sum of bottom and footer |
86
|
|
|
// then top is actually the header value, and bottom is actually the footer value |
87
|
|
|
switch ($key) { |
88
|
10 |
|
case 'left': |
89
|
10 |
|
case 'right': |
90
|
10 |
|
$this->sheetMargin($key, $marginSize); |
91
|
|
|
|
92
|
10 |
|
break; |
93
|
10 |
|
case 'top': |
94
|
10 |
|
$this->sheetMargin($key, $marginSet['header'] ?? 0); |
95
|
|
|
|
96
|
10 |
|
break; |
97
|
10 |
|
case 'bottom': |
98
|
10 |
|
$this->sheetMargin($key, $marginSet['footer'] ?? 0); |
99
|
|
|
|
100
|
10 |
|
break; |
101
|
10 |
|
case 'header': |
102
|
10 |
|
$this->sheetMargin($key, ($marginSet['top'] ?? 0) - $marginSize); |
103
|
|
|
|
104
|
10 |
|
break; |
105
|
10 |
|
case 'footer': |
106
|
10 |
|
$this->sheetMargin($key, ($marginSet['bottom'] ?? 0) - $marginSize); |
107
|
|
|
|
108
|
10 |
|
break; |
109
|
|
|
} |
110
|
|
|
} |
111
|
10 |
|
} |
112
|
|
|
|
113
|
10 |
|
private function sheetMargin(string $key, float $marginSize): void |
114
|
|
|
{ |
115
|
|
|
switch ($key) { |
116
|
10 |
|
case 'top': |
117
|
10 |
|
$this->spreadsheet->getActiveSheet()->getPageMargins()->setTop($marginSize); |
118
|
|
|
|
119
|
10 |
|
break; |
120
|
10 |
|
case 'bottom': |
121
|
10 |
|
$this->spreadsheet->getActiveSheet()->getPageMargins()->setBottom($marginSize); |
122
|
|
|
|
123
|
10 |
|
break; |
124
|
10 |
|
case 'left': |
125
|
10 |
|
$this->spreadsheet->getActiveSheet()->getPageMargins()->setLeft($marginSize); |
126
|
|
|
|
127
|
10 |
|
break; |
128
|
10 |
|
case 'right': |
129
|
10 |
|
$this->spreadsheet->getActiveSheet()->getPageMargins()->setRight($marginSize); |
130
|
|
|
|
131
|
10 |
|
break; |
132
|
10 |
|
case 'header': |
133
|
10 |
|
$this->spreadsheet->getActiveSheet()->getPageMargins()->setHeader($marginSize); |
134
|
|
|
|
135
|
10 |
|
break; |
136
|
10 |
|
case 'footer': |
137
|
10 |
|
$this->spreadsheet->getActiveSheet()->getPageMargins()->setFooter($marginSize); |
138
|
|
|
|
139
|
10 |
|
break; |
140
|
|
|
} |
141
|
10 |
|
} |
142
|
|
|
} |
143
|
|
|
|