From a1ffd3473b256461ab60625c65a077b90495720d Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Mon, 29 Sep 2025 22:25:20 -0700 Subject: [PATCH 1/2] Offer Tcpdf Interface Which Throws Exception Rather than Die By default, TCPDF will die sometimes rather than throwing exception. And this is controlled by a defined constant in the global namespace, not by an instance property. Ugh! Using this class instead of the class which it extends will probably be suitable for most users. But not for those who have customized their config file. Which is why this isn't the default, so that there is no breaking change for those users. Note that if both TCPDF and TcpdfNoDie are used in the same process, the first one used "wins" the battle of the defines. --- src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php | 6 ++++ src/PhpSpreadsheet/Writer/Pdf/TcpdfNoDie.php | 27 ++++++++++++++ .../Writer/Tcpdf/NoDieTest.php | 36 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 src/PhpSpreadsheet/Writer/Pdf/TcpdfNoDie.php create mode 100644 tests/PhpSpreadsheetTests/Writer/Tcpdf/NoDieTest.php diff --git a/src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php b/src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php index fe5cfe25c6..94cbaffcb7 100644 --- a/src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php +++ b/src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php @@ -30,9 +30,15 @@ public function __construct(Spreadsheet $spreadsheet) */ protected function createExternalWriterInstance(string $orientation, string $unit, $paperSize): \TCPDF { + $this->defines(); + return new \TCPDF($orientation, $unit, $paperSize); } + protected function defines(): void + { + } + /** * Save Spreadsheet to file. * diff --git a/src/PhpSpreadsheet/Writer/Pdf/TcpdfNoDie.php b/src/PhpSpreadsheet/Writer/Pdf/TcpdfNoDie.php new file mode 100644 index 0000000000..070e52d043 --- /dev/null +++ b/src/PhpSpreadsheet/Writer/Pdf/TcpdfNoDie.php @@ -0,0 +1,27 @@ +spreadsheet = new Spreadsheet(); + } + + protected function tearDown(): void + { + unset($this->spreadsheet); + } + + public function testExceptionRatherThanDie(): void + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Could not include font definition file'); + $sheet = $this->spreadsheet->getActiveSheet(); + $sheet->setCellValue('A1', 'cell'); + $writer = new TcpdfNoDie($this->spreadsheet); + $writer->setFont('xyz'); + $writer->save('php://memory'); + } +} From b1c5c64060e729fb6cff5505167a852a528780f0 Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Tue, 30 Sep 2025 07:33:04 -0700 Subject: [PATCH 2/2] Tweak --- tests/PhpSpreadsheetTests/Writer/Tcpdf/NoDieTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/PhpSpreadsheetTests/Writer/Tcpdf/NoDieTest.php b/tests/PhpSpreadsheetTests/Writer/Tcpdf/NoDieTest.php index cacfa0bb96..fb1bcfe3d3 100644 --- a/tests/PhpSpreadsheetTests/Writer/Tcpdf/NoDieTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Tcpdf/NoDieTest.php @@ -7,8 +7,6 @@ use PhpOffice\PhpSpreadsheet\Writer\Pdf\TcpdfNoDie; use PHPUnit\Framework\Attributes; -// Separate processes because of global defined names -#[Attributes\RunTestsInSeparateProcesses] class NoDieTest extends \PHPUnit\Framework\TestCase { private Spreadsheet $spreadsheet; @@ -23,6 +21,9 @@ protected function tearDown(): void unset($this->spreadsheet); } + // Separate processes because of global defined names + #[Attributes\RunInSeparateProcess] + #[Attributes\PreserveGlobalState(false)] public function testExceptionRatherThanDie(): void { $this->expectException(Exception::class);