From a51ebbfa66ee775315285b6742355521871807fa Mon Sep 17 00:00:00 2001 From: Steven Renaux Date: Tue, 9 Apr 2024 14:04:08 +0200 Subject: [PATCH 1/6] Add a possibility of customizing the standard paper format --- docs/customization.rst | 38 +++++++++++++------- src/Builder/AbstractChromiumPdfBuilder.php | 9 +++++ src/Enum/PaperSize.php | 40 ++++++++++++++++++++++ tests/Builder/HtmlPdfBuilderTest.php | 21 ++++++++++++ 4 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 src/Enum/PaperSize.php diff --git a/docs/customization.rst b/docs/customization.rst index f93ddca9..0cf9139c 100644 --- a/docs/customization.rst +++ b/docs/customization.rst @@ -58,7 +58,19 @@ The path provided can be relative as well as absolute. Paper size ---------- -You can override the default paper size with width and height (in inches): +You can override the default paper size with standard paper size using the +`PaperSize` enum : + +.. code-block:: php + + use Sensiolabs\GotenbergBundle\Pdf\Gotenberg; + + $twigPdfBuilder = $gotenberg->twig(); + $twigPdfBuilder + ->content('path/to/template.html.twig') + ->paperStandardSize(PaperSize::A3); + +Or, you can even override with your proper width and height (in inches): .. code-block:: php @@ -69,17 +81,19 @@ You can override the default paper size with width and height (in inches): ->content('path/to/template.html.twig') ->paperSize(8.5, 11); -* Letter - 8.5 x 11 (default) -* Legal - 8.5 x 14 -* Tabloid - 11 x 17 -* Ledger - 17 x 11 -* A0 - 33.1 x 46.8 -* A1 - 23.4 x 33.1 -* A2 - 16.54 x 23.4 -* A3 - 11.7 x 16.54 -* A4 - 8.27 x 11.7 -* A5 - 5.83 x 8.27 -* A6 - 4.13 x 5.83 +.. tip:: + + * Letter - 8.5 x 11 (default) + * Legal - 8.5 x 14 + * Tabloid - 11 x 17 + * Ledger - 17 x 11 + * A0 - 33.1 x 46.8 + * A1 - 23.4 x 33.1 + * A2 - 16.54 x 23.4 + * A3 - 11.7 x 16.54 + * A4 - 8.27 x 11.7 + * A5 - 5.83 x 8.27 + * A6 - 4.13 x 5.83 Prefer CSS page size -------------------- diff --git a/src/Builder/AbstractChromiumPdfBuilder.php b/src/Builder/AbstractChromiumPdfBuilder.php index da2fb213..c67bdc93 100644 --- a/src/Builder/AbstractChromiumPdfBuilder.php +++ b/src/Builder/AbstractChromiumPdfBuilder.php @@ -3,6 +3,7 @@ namespace Sensiolabs\GotenbergBundle\Builder; use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface; +use Sensiolabs\GotenbergBundle\Enum\PaperSize; use Sensiolabs\GotenbergBundle\Enum\PdfPart; use Sensiolabs\GotenbergBundle\Exception\ExtraHttpHeadersJsonEncodingException; use Sensiolabs\GotenbergBundle\Exception\InvalidBuilderConfiguration; @@ -63,6 +64,14 @@ public function paperSize(float $width, float $height): static return $this; } + public function paperStandardSize(PaperSize $paperSize): static + { + $this->paperWidth($paperSize->width()); + $this->paperHeight($paperSize->height()); + + return $this; + } + public function paperWidth(float $width): static { $this->formFields['paperWidth'] = $width; diff --git a/src/Enum/PaperSize.php b/src/Enum/PaperSize.php new file mode 100644 index 00000000..2ca65e79 --- /dev/null +++ b/src/Enum/PaperSize.php @@ -0,0 +1,40 @@ + 33.1, + PaperSize::A1 => 23.4, + PaperSize::A2 => 16.54, + PaperSize::A3 => 11.7, + PaperSize::A4 => 8.27, + PaperSize::A5 => 5.83, + PaperSize::A6 => 4.13, + }; + } + + public function height(): float + { + return match ($this) { + PaperSize::A0 => 46.8, + PaperSize::A1 => 33.1, + PaperSize::A2 => 23.4, + PaperSize::A3 => 16.54, + PaperSize::A4 => 11.7, + PaperSize::A5 => 8.27, + PaperSize::A6 => 5.83, + }; + } +} diff --git a/tests/Builder/HtmlPdfBuilderTest.php b/tests/Builder/HtmlPdfBuilderTest.php index c9054495..d173a8ba 100644 --- a/tests/Builder/HtmlPdfBuilderTest.php +++ b/tests/Builder/HtmlPdfBuilderTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\Attributes\UsesClass; use Sensiolabs\GotenbergBundle\Builder\HtmlPdfBuilder; use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface; +use Sensiolabs\GotenbergBundle\Enum\PaperSize; use Sensiolabs\GotenbergBundle\Exception\ExtraHttpHeadersJsonEncodingException; use Sensiolabs\GotenbergBundle\Exception\PdfPartRenderingException; use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter; @@ -96,6 +97,26 @@ public function testWithAssets(): void self::assertSame('image/png', $multipartFormData[1]['files']->getContentType()); } + public function testWithPaperStandardSize(): void + { + $client = $this->createMock(GotenbergClientInterface::class); + $assetBaseDirFormatter = new AssetBaseDirFormatter(new Filesystem(), self::FIXTURE_DIR, self::FIXTURE_DIR); + + $builder = new HtmlPdfBuilder($client, $assetBaseDirFormatter); + $builder->contentFile('content.html'); + $builder->paperStandardSize(PaperSize::A3); + + $multipartFormData = $builder->getMultipartFormData(); + + self::assertCount(3, $multipartFormData); + + self::assertArrayHasKey('paperWidth', $multipartFormData[1]); + self::assertArrayHasKey('paperHeight', $multipartFormData[2]); + + self::assertSame(PaperSize::A3->width(), $multipartFormData[1]['paperWidth']); + self::assertSame(PaperSize::A3->height(), $multipartFormData[2]['paperHeight']); + } + public function testWithHeader(): void { $client = $this->createMock(GotenbergClientInterface::class); From 2d8142cf27b38be30d40119b6ac1893042974d2e Mon Sep 17 00:00:00 2001 From: Steven Renaux Date: Tue, 9 Apr 2024 14:32:20 +0200 Subject: [PATCH 2/6] Remove backed enum --- src/Enum/PaperSize.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Enum/PaperSize.php b/src/Enum/PaperSize.php index 2ca65e79..13d87be6 100644 --- a/src/Enum/PaperSize.php +++ b/src/Enum/PaperSize.php @@ -2,15 +2,15 @@ namespace Sensiolabs\GotenbergBundle\Enum; -enum PaperSize: string +enum PaperSize { - case A0 = 'A0'; - case A1 = 'A1'; - case A2 = 'A2'; - case A3 = 'A3'; - case A4 = 'A4'; - case A5 = 'A5'; - case A6 = 'A6'; + case A0; + case A1; + case A2; + case A3; + case A4; + case A5; + case A6; public function width(): float { From 0538c445ff97ad37f7a670c1247f9d1546c7234e Mon Sep 17 00:00:00 2001 From: Steven Renaux Date: Tue, 9 Apr 2024 16:49:33 +0200 Subject: [PATCH 3/6] Add test and interface --- docs/customization.rst | 43 ++++++++++++++++++++++ src/Builder/AbstractChromiumPdfBuilder.php | 6 +-- src/Enum/PaperSize.php | 2 +- src/Enum/PaperSizeInterface.php | 10 +++++ tests/Enum/PaperSizeTest.php | 27 ++++++++++++++ 5 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 src/Enum/PaperSizeInterface.php create mode 100644 tests/Enum/PaperSizeTest.php diff --git a/docs/customization.rst b/docs/customization.rst index 0cf9139c..daada68c 100644 --- a/docs/customization.rst +++ b/docs/customization.rst @@ -70,6 +70,49 @@ You can override the default paper size with standard paper size using the ->content('path/to/template.html.twig') ->paperStandardSize(PaperSize::A3); +Or if you want you can create your own logic, you just need to implements `PaperSizeInterface` + +.. code-block:: php + + use Sensiolabs\GotenbergBundle\Enum\PaperSizeInterface; + + enum PdfSize implements PaperSizeInterface + { + case Letter; + case Legal; + case Tabloid; + case Ledger; + + public function width(): float + { + return match ($this) { + PdfSize::Letter, PdfSize::Legal => 8.5, + PdfSize::Tabloid => 11, + PdfSize::Ledger => 17, + }; + } + + public function height(): float + { + return match ($this) { + PdfSize::Letter, PdfSize::Ledger => 11, + PdfSize::Legal => 14, + PdfSize::Tabloid => 17, + }; + } + } + +And then use it with paperStandardSize. + +.. code-block:: php + + use Sensiolabs\GotenbergBundle\Pdf\Gotenberg; + + $twigPdfBuilder = $gotenberg->twig(); + $twigPdfBuilder + ->content('path/to/template.html.twig') + ->paperStandardSize(PdfSize::Tabloid); + Or, you can even override with your proper width and height (in inches): .. code-block:: php diff --git a/src/Builder/AbstractChromiumPdfBuilder.php b/src/Builder/AbstractChromiumPdfBuilder.php index c67bdc93..f94d161f 100644 --- a/src/Builder/AbstractChromiumPdfBuilder.php +++ b/src/Builder/AbstractChromiumPdfBuilder.php @@ -3,7 +3,7 @@ namespace Sensiolabs\GotenbergBundle\Builder; use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface; -use Sensiolabs\GotenbergBundle\Enum\PaperSize; +use Sensiolabs\GotenbergBundle\Enum\PaperSizeInterface; use Sensiolabs\GotenbergBundle\Enum\PdfPart; use Sensiolabs\GotenbergBundle\Exception\ExtraHttpHeadersJsonEncodingException; use Sensiolabs\GotenbergBundle\Exception\InvalidBuilderConfiguration; @@ -64,7 +64,7 @@ public function paperSize(float $width, float $height): static return $this; } - public function paperStandardSize(PaperSize $paperSize): static + public function paperStandardSize(PaperSizeInterface $paperSize): static { $this->paperWidth($paperSize->width()); $this->paperHeight($paperSize->height()); @@ -122,7 +122,7 @@ public function marginLeft(float $left): static return $this; } - public function marginRight(float $right): static + public function marginRight(string $right): static { $this->formFields['marginRight'] = $right; diff --git a/src/Enum/PaperSize.php b/src/Enum/PaperSize.php index 13d87be6..9ca2ed5a 100644 --- a/src/Enum/PaperSize.php +++ b/src/Enum/PaperSize.php @@ -2,7 +2,7 @@ namespace Sensiolabs\GotenbergBundle\Enum; -enum PaperSize +enum PaperSize implements PaperSizeInterface { case A0; case A1; diff --git a/src/Enum/PaperSizeInterface.php b/src/Enum/PaperSizeInterface.php new file mode 100644 index 00000000..3f1c718e --- /dev/null +++ b/src/Enum/PaperSizeInterface.php @@ -0,0 +1,10 @@ +width(); + self::assertIsFloat($height); + } + } + + public function testHeight(): void + { + foreach (PaperSize::cases() as $size) { + $height = $size->height(); + self::assertIsFloat($height); + } + } +} From c3910b0907c7c68e44bd4f590e1a92608eca216e Mon Sep 17 00:00:00 2001 From: Steven Renaux Date: Tue, 9 Apr 2024 17:19:56 +0200 Subject: [PATCH 4/6] revert marginRight method to float --- src/Builder/AbstractChromiumPdfBuilder.php | 2 +- tests/Enum/PaperSizeTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Builder/AbstractChromiumPdfBuilder.php b/src/Builder/AbstractChromiumPdfBuilder.php index f94d161f..66819079 100644 --- a/src/Builder/AbstractChromiumPdfBuilder.php +++ b/src/Builder/AbstractChromiumPdfBuilder.php @@ -122,7 +122,7 @@ public function marginLeft(float $left): static return $this; } - public function marginRight(string $right): static + public function marginRight(float $right): static { $this->formFields['marginRight'] = $right; diff --git a/tests/Enum/PaperSizeTest.php b/tests/Enum/PaperSizeTest.php index 7cc7be27..6ff9681d 100644 --- a/tests/Enum/PaperSizeTest.php +++ b/tests/Enum/PaperSizeTest.php @@ -12,8 +12,8 @@ final class PaperSizeTest extends TestCase public function testWidth(): void { foreach (PaperSize::cases() as $size) { - $height = $size->width(); - self::assertIsFloat($height); + $width = $size->width(); + self::assertIsFloat($width); } } From 0edab769ec5b6989862e6b326158e1a96d51f334 Mon Sep 17 00:00:00 2001 From: Steven Renaux Date: Thu, 11 Apr 2024 17:47:25 +0200 Subject: [PATCH 5/6] Update test --- tests/Enum/PaperSizeTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Enum/PaperSizeTest.php b/tests/Enum/PaperSizeTest.php index 6ff9681d..fd3ed425 100644 --- a/tests/Enum/PaperSizeTest.php +++ b/tests/Enum/PaperSizeTest.php @@ -12,16 +12,16 @@ final class PaperSizeTest extends TestCase public function testWidth(): void { foreach (PaperSize::cases() as $size) { - $width = $size->width(); - self::assertIsFloat($width); + $size->width(); + self::addToAssertionCount(1); } } public function testHeight(): void { foreach (PaperSize::cases() as $size) { - $height = $size->height(); - self::assertIsFloat($height); + $size->height(); + self::addToAssertionCount(1); } } } From 95706c0708c5fdf42d58eb35a2f1e1481c163ede Mon Sep 17 00:00:00 2001 From: Steven Renaux Date: Thu, 11 Apr 2024 17:55:34 +0200 Subject: [PATCH 6/6] Update test --- tests/Builder/HtmlPdfBuilderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Builder/HtmlPdfBuilderTest.php b/tests/Builder/HtmlPdfBuilderTest.php index 5b48e478..aa724ea9 100644 --- a/tests/Builder/HtmlPdfBuilderTest.php +++ b/tests/Builder/HtmlPdfBuilderTest.php @@ -113,8 +113,8 @@ public function testWithPaperStandardSize(): void self::assertArrayHasKey('paperWidth', $multipartFormData[1]); self::assertArrayHasKey('paperHeight', $multipartFormData[2]); - self::assertSame(PaperSize::A3->width(), $multipartFormData[1]['paperWidth']); - self::assertSame(PaperSize::A3->height(), $multipartFormData[2]['paperHeight']); + self::assertSame((string) PaperSize::A3->width(), $multipartFormData[1]['paperWidth']); + self::assertSame((string) PaperSize::A3->height(), $multipartFormData[2]['paperHeight']); } public function testWithHeader(): void