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