Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]
1 <?php 2 3 namespace PhpOffice\PhpSpreadsheet\Reader\Xml; 4 5 use PhpOffice\PhpSpreadsheet\Spreadsheet; 6 use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; 7 use SimpleXMLElement; 8 use stdClass; 9 10 class PageSettings 11 { 12 /** 13 * @var stdClass 14 */ 15 private $printSettings; 16 17 public function __construct(SimpleXMLElement $xmlX, array $namespaces) 18 { 19 $printSettings = $this->pageSetup($xmlX, $namespaces, $this->getPrintDefaults()); 20 $this->printSettings = $this->printSetup($xmlX, $printSettings); 21 } 22 23 public function loadPageSettings(Spreadsheet $spreadsheet): void 24 { 25 $spreadsheet->getActiveSheet()->getPageSetup() 26 ->setPaperSize($this->printSettings->paperSize) 27 ->setOrientation($this->printSettings->orientation) 28 ->setScale($this->printSettings->scale) 29 ->setVerticalCentered($this->printSettings->verticalCentered) 30 ->setHorizontalCentered($this->printSettings->horizontalCentered) 31 ->setPageOrder($this->printSettings->printOrder); 32 $spreadsheet->getActiveSheet()->getPageMargins() 33 ->setTop($this->printSettings->topMargin) 34 ->setHeader($this->printSettings->headerMargin) 35 ->setLeft($this->printSettings->leftMargin) 36 ->setRight($this->printSettings->rightMargin) 37 ->setBottom($this->printSettings->bottomMargin) 38 ->setFooter($this->printSettings->footerMargin); 39 } 40 41 private function getPrintDefaults(): stdClass 42 { 43 return (object) [ 44 'paperSize' => 9, 45 'orientation' => PageSetup::ORIENTATION_DEFAULT, 46 'scale' => 100, 47 'horizontalCentered' => false, 48 'verticalCentered' => false, 49 'printOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, 50 'topMargin' => 0.75, 51 'headerMargin' => 0.3, 52 'leftMargin' => 0.7, 53 'rightMargin' => 0.7, 54 'bottomMargin' => 0.75, 55 'footerMargin' => 0.3, 56 ]; 57 } 58 59 private function pageSetup(SimpleXMLElement $xmlX, array $namespaces, stdClass $printDefaults): stdClass 60 { 61 if (isset($xmlX->WorksheetOptions->PageSetup)) { 62 foreach ($xmlX->WorksheetOptions->PageSetup as $pageSetupData) { 63 foreach ($pageSetupData as $pageSetupKey => $pageSetupValue) { 64 $pageSetupAttributes = $pageSetupValue->attributes($namespaces['x']); 65 switch ($pageSetupKey) { 66 case 'Layout': 67 $this->setLayout($printDefaults, $pageSetupAttributes); 68 69 break; 70 case 'Header': 71 $printDefaults->headerMargin = (float) $pageSetupAttributes->Margin ?: 1.0; 72 73 break; 74 case 'Footer': 75 $printDefaults->footerMargin = (float) $pageSetupAttributes->Margin ?: 1.0; 76 77 break; 78 case 'PageMargins': 79 $this->setMargins($printDefaults, $pageSetupAttributes); 80 81 break; 82 } 83 } 84 } 85 } 86 87 return $printDefaults; 88 } 89 90 private function printSetup(SimpleXMLElement $xmlX, stdClass $printDefaults): stdClass 91 { 92 if (isset($xmlX->WorksheetOptions->Print)) { 93 foreach ($xmlX->WorksheetOptions->Print as $printData) { 94 foreach ($printData as $printKey => $printValue) { 95 switch ($printKey) { 96 case 'LeftToRight': 97 $printDefaults->printOrder = PageSetup::PAGEORDER_OVER_THEN_DOWN; 98 99 break; 100 case 'PaperSizeIndex': 101 $printDefaults->paperSize = (int) $printValue ?: 9; 102 103 break; 104 case 'Scale': 105 $printDefaults->scale = (int) $printValue ?: 100; 106 107 break; 108 } 109 } 110 } 111 } 112 113 return $printDefaults; 114 } 115 116 private function setLayout(stdClass $printDefaults, SimpleXMLElement $pageSetupAttributes): void 117 { 118 $printDefaults->orientation = (string) strtolower($pageSetupAttributes->Orientation) ?: PageSetup::ORIENTATION_PORTRAIT; 119 $printDefaults->horizontalCentered = (bool) $pageSetupAttributes->CenterHorizontal ?: false; 120 $printDefaults->verticalCentered = (bool) $pageSetupAttributes->CenterVertical ?: false; 121 } 122 123 private function setMargins(stdClass $printDefaults, SimpleXMLElement $pageSetupAttributes): void 124 { 125 $printDefaults->leftMargin = (float) $pageSetupAttributes->Left ?: 1.0; 126 $printDefaults->rightMargin = (float) $pageSetupAttributes->Right ?: 1.0; 127 $printDefaults->topMargin = (float) $pageSetupAttributes->Top ?: 1.0; 128 $printDefaults->bottomMargin = (float) $pageSetupAttributes->Bottom ?: 1.0; 129 } 130 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body