diff --git a/src/PhpSpreadsheet/Reader/Ods/PageSettings.php b/src/PhpSpreadsheet/Reader/Ods/PageSettings.php index f3e956c91..fd35de5b9 100644 --- a/src/PhpSpreadsheet/Reader/Ods/PageSettings.php +++ b/src/PhpSpreadsheet/Reader/Ods/PageSettings.php @@ -55,22 +55,22 @@ class PageSettings foreach ($styles as $styleSet) { $styleName = $styleSet->getAttributeNS($this->stylesNs, 'name'); - $pageLayoutProperties = $styleSet->getElementsByTagNameNS($this->stylesNs, 'page-layout-properties')[0]; - $styleOrientation = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'print-orientation'); - $styleScale = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'scale-to'); - $stylePrintOrder = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'print-page-order'); - $centered = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'table-centering'); + $pageLayoutProperties = $styleSet->getElementsByTagNameNS($this->stylesNs, 'page-layout-properties')->item(0); + $styleOrientation = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'print-orientation'); + $styleScale = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'scale-to'); + $stylePrintOrder = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'print-page-order'); + $centered = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'table-centering'); - $marginLeft = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-left'); - $marginRight = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-right'); - $marginTop = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-top'); - $marginBottom = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-bottom'); - $header = $styleSet->getElementsByTagNameNS($this->stylesNs, 'header-style')[0]; - $headerProperties = $header->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')[0]; - $marginHeader = isset($headerProperties) ? $headerProperties->getAttributeNS($this->stylesFo, 'min-height') : null; - $footer = $styleSet->getElementsByTagNameNS($this->stylesNs, 'footer-style')[0]; - $footerProperties = $footer->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')[0]; - $marginFooter = isset($footerProperties) ? $footerProperties->getAttributeNS($this->stylesFo, 'min-height') : null; + $marginLeft = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-left'); + $marginRight = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-right'); + $marginTop = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-top'); + $marginBottom = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-bottom'); + $header = $styleSet->getElementsByTagNameNS($this->stylesNs, 'header-style')->item(0); + $headerProperties = $header?->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')?->item(0); + $marginHeader = $headerProperties?->getAttributeNS($this->stylesFo, 'min-height'); + $footer = $styleSet->getElementsByTagNameNS($this->stylesNs, 'footer-style')->item(0); + $footerProperties = $footer?->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')?->item(0); + $marginFooter = $footerProperties?->getAttributeNS($this->stylesFo, 'min-height'); $this->pageLayoutStyles[$styleName] = (object) [ 'orientation' => $styleOrientation ?: PageSetup::ORIENTATION_DEFAULT, diff --git a/tests/PhpSpreadsheetTests/Reader/Ods/Issue4099Test.php b/tests/PhpSpreadsheetTests/Reader/Ods/Issue4099Test.php new file mode 100644 index 000000000..2b38181a6 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/Ods/Issue4099Test.php @@ -0,0 +1,28 @@ +file . '#styles.xml'; + $contents = (string) file_get_contents($zipFile); + self::assertStringContainsString('page-layout ', $contents); + self::assertStringNotContainsString('header-style', $contents); + self::assertStringNotContainsString('footer-style', $contents); + $reader = new OdsReader(); + $spreadsheet = $reader->load($this->file); + $sheet = $spreadsheet->getActiveSheet(); + self::assertSame('FirstCell', $sheet->getCell('A1')->getValue()); + $spreadsheet->disconnectWorksheets(); + } +} diff --git a/tests/data/Reader/Ods/issue.4099.ods b/tests/data/Reader/Ods/issue.4099.ods new file mode 100644 index 000000000..26b17ecbd Binary files /dev/null and b/tests/data/Reader/Ods/issue.4099.ods differ