PHPOffice / PhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files
https://phpspreadsheet.readthedocs.io
MIT License
13.34k stars 3.47k forks source link

"Invalid page format" Exception when exporting as a PDF with $sheet->getPageSetup()->setPaperSize('USLETTER'); #3491

Closed lindymad closed 1 year ago

lindymad commented 1 year ago

This is:

- [x] a bug report
- [ ] a feature request
- [ ] **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 paper size should be set to USLETTER and no error should be thrown

What is the current behavior?

An error is thrown with the message "Invalid page format: ". Note - If I change the code to $sheet->getPageSetup()->setPaperSize(1); then it works

What are the steps to reproduce?

<?php

require __DIR__ . '/vendor/autoload.php';

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

// add code that show the issue here...
$sheet = $spreadsheet->getActiveSheet();
$sheet->getPageSetup()->setPaperSize('USLETTER');

\PhpOffice\PhpSpreadsheet\IOFactory::registerWriter('Pdf', \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class);
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Pdf");

$writer->save('php://output');

What features do you think are causing the issue

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

PDF

Which versions of PhpSpreadsheet and PHP are affected?

I found the issue in PHPSpreadsheet 1.28.0 / PHP 7.4. It was working fine in PHPSpreadsheet 1.2.1 / PHP 7.4

oleibman commented 1 year ago

setPaperSize takes an integer argument, not a string. The following should work:

$sheet->getPageSetup()->setPaperSize(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::PAPERSIZE_LETTER);

Unsurprisingly, that constant has a value of 1, which is what you said above that you were using as a workaround.

MarkBaker commented 1 year ago

You can find the set of valid constants defined in the PageSetup class

lindymad commented 1 year ago

@oleibman - ah so this is a compatibility issue from a previous version (it worked with the string in 1.2.1, which I just upgraded from). I will change to use the constant. Thanks!