File tree Expand file tree Collapse file tree 3 files changed +70
-0
lines changed
src/PhpSpreadsheet/Writer/Pdf
tests/PhpSpreadsheetTests/Writer/Tcpdf Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments