Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PdfWriter Mpdf orientation bug #1691

Closed
sirion89 opened this issue Oct 28, 2020 · 1 comment · Fixed by #2410
Closed

PdfWriter Mpdf orientation bug #1691

sirion89 opened this issue Oct 28, 2020 · 1 comment · Fixed by #2410

Comments

@sirion89
Copy link

sirion89 commented Oct 28, 2020

I have already found the solution i think, but i'm not practical with pull request. I'm not used to it :( sorry
This is:

- [x] a bug report
- [ ] a feature request
- [x] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

The orientation to be what i've set when i created the pdf writer

What is the current behavior?

The orientation get overwritten mistakenly by the code

What are the steps to reproduce?

Quite easy,

$writerPdf = IOFactory::createWriter( $spreadsheetPdf, 'Mpdf' );
$writerPdf->setOrientation( PageSetup::ORIENTATION_LANDSCAPE );
$writerPdf->writeAllSheets();
$writerPdf->save( $path );

The fix should also be easy i guess. File is: src/PhpSpreadsheet/Writer/Pdf/Mpdf.php
This is an extract of the save function:

  //  Check for paper size and page orientation
        if (null === $this->getSheetIndex()) {
            $orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation()
                == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
            $printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
        } else {
            $orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
                == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
            $printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
        }
        $this->setOrientation($orientation); //<------- this kill what we set with $writerPdf->setOrientation...
        //  Override Page Orientation
        if (null !== $this->getOrientation()) { // At this point $this->getOrientation() will ALWAYS have $orientation value
            $orientation = ($this->getOrientation() == PageSetup::ORIENTATION_DEFAULT)
                ? PageSetup::ORIENTATION_PORTRAIT
                : $this->getOrientation();
        }
        $orientation = strtoupper($orientation);

Which versions of PhpSpreadsheet and PHP are affected?

composer.json:
"phpoffice/phpspreadsheet": "^1.10",
PHP version: 7.3.21

@oleibman
Copy link
Collaborator

PhpSpreadsheet allows the setting of different page size and orientation on each sheet. So, instead of setting the orientation on the PDF writer, you could instead do something like the following before doing your write:

foreach ($spreadsheetPDF->getAllSheets() as $worksheet) {
    $worksheet->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
}

I agree that this is not quite so convenient as your existing code, but it should produce the result you want.

I will look into what is needed to make the writer override the orientation for a sheet only when the sheet hasn't explicitly specified a non-default orientation. The same should probably happen for paper size, which is slightly more difficult.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Nov 22, 2021
Fix PHPOffice#1691. PhpSpreadsheet allows the setting of different page size and orientation on each worksheet. It also allows the setting of page size and orientation on the PDF writer. It isn't clear which is supposed to prevail when the two are in conflict. In the cited issue, the user expects the PDF writer setting to prevail, and I tend to agree. Code is changed to do this, and handling things in this manner is now explicitly documented.

PhpSpreadsheet uses a default paper size of Letter, and a default orientation of Default (which Excel treats as Portrait). New static routines are added to change the default for sheets created subsequent to such calls. This could allow users to configure these defaults better for their environments. The new functions are added to the documentation.
oleibman added a commit that referenced this issue Dec 2, 2021
Fix #1691. PhpSpreadsheet allows the setting of different page size and orientation on each worksheet. It also allows the setting of page size and orientation on the PDF writer. It isn't clear which is supposed to prevail when the two are in conflict. In the cited issue, the user expects the PDF writer setting to prevail, and I tend to agree. Code is changed to do this, and handling things in this manner is now explicitly documented.

PhpSpreadsheet uses a default paper size of Letter, and a default orientation of Default (which Excel treats as Portrait). New static routines are added to change the default for sheets created subsequent to such calls. This could allow users to configure these defaults better for their environments. The new functions are added to the documentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants