Skip to content

Commit d52dbaa

Browse files
authored
Merge pull request #4666 from oleibman/tcpdfnodie
Offer Tcpdf Interface Which Throws Exception Rather than Die
2 parents 7c9f42e + b1c5c64 commit d52dbaa

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ public function __construct(Spreadsheet $spreadsheet)
3030
*/
3131
protected function createExternalWriterInstance(string $orientation, string $unit, $paperSize): \TCPDF
3232
{
33+
$this->defines();
34+
3335
return new \TCPDF($orientation, $unit, $paperSize);
3436
}
3537

38+
protected function defines(): void
39+
{
40+
}
41+
3642
/**
3743
* Save Spreadsheet to file.
3844
*
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
4+
5+
class TcpdfNoDie extends Tcpdf
6+
{
7+
/**
8+
* By default, Tcpdf will die sometimes rather than throwing exception.
9+
* And this is controlled by a defined constant in the global namespace,
10+
* not by an instance property. Ugh!
11+
* Using this class instead of the class which it extends will probably
12+
* be suitable for most users. But not for those who have customized
13+
* their config file. Which is why this isn't the default, so that
14+
* there is no breaking change for those users.
15+
* Note that if both Tcpdf and TcpdfNoDie are used in the same process,
16+
* the first one used "wins" the battle of the defines.
17+
*/
18+
protected function defines(): void
19+
{
20+
if (!defined('K_TCPDF_EXTERNAL_CONFIG')) {
21+
define('K_TCPDF_EXTERNAL_CONFIG', true);
22+
}
23+
if (!defined('K_TCPDF_THROW_EXCEPTION_ERROR')) {
24+
define('K_TCPDF_THROW_EXCEPTION_ERROR', true);
25+
}
26+
}
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Tcpdf;
4+
5+
use Exception;
6+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7+
use PhpOffice\PhpSpreadsheet\Writer\Pdf\TcpdfNoDie;
8+
use PHPUnit\Framework\Attributes;
9+
10+
class NoDieTest extends \PHPUnit\Framework\TestCase
11+
{
12+
private Spreadsheet $spreadsheet;
13+
14+
protected function setUp(): void
15+
{
16+
$this->spreadsheet = new Spreadsheet();
17+
}
18+
19+
protected function tearDown(): void
20+
{
21+
unset($this->spreadsheet);
22+
}
23+
24+
// Separate processes because of global defined names
25+
#[Attributes\RunInSeparateProcess]
26+
#[Attributes\PreserveGlobalState(false)]
27+
public function testExceptionRatherThanDie(): void
28+
{
29+
$this->expectException(Exception::class);
30+
$this->expectExceptionMessage('Could not include font definition file');
31+
$sheet = $this->spreadsheet->getActiveSheet();
32+
$sheet->setCellValue('A1', 'cell');
33+
$writer = new TcpdfNoDie($this->spreadsheet);
34+
$writer->setFont('xyz');
35+
$writer->save('php://memory');
36+
}
37+
}

0 commit comments

Comments
 (0)