Skip to content

Commit

Permalink
Add a possibility of customizing with the standard paper format (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenRenaux committed Apr 12, 2024
1 parent 753149a commit dd8d61d
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 12 deletions.
81 changes: 69 additions & 12 deletions docs/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,62 @@ 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 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
Expand All @@ -69,17 +124,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
--------------------
Expand Down
9 changes: 9 additions & 0 deletions src/Builder/AbstractChromiumPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sensiolabs\GotenbergBundle\Builder;

use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface;
use Sensiolabs\GotenbergBundle\Enum\PaperSizeInterface;
use Sensiolabs\GotenbergBundle\Enum\PdfPart;
use Sensiolabs\GotenbergBundle\Exception\InvalidBuilderConfiguration;
use Sensiolabs\GotenbergBundle\Exception\PdfPartRenderingException;
Expand Down Expand Up @@ -62,6 +63,14 @@ public function paperSize(float $width, float $height): static
return $this;
}

public function paperStandardSize(PaperSizeInterface $paperSize): static
{
$this->paperWidth($paperSize->width());
$this->paperHeight($paperSize->height());

return $this;
}

public function paperWidth(float $width): static
{
$this->formFields['paperWidth'] = $width;
Expand Down
40 changes: 40 additions & 0 deletions src/Enum/PaperSize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Sensiolabs\GotenbergBundle\Enum;

enum PaperSize implements PaperSizeInterface
{
case A0;
case A1;
case A2;
case A3;
case A4;
case A5;
case A6;

public function width(): float
{
return match ($this) {
PaperSize::A0 => 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,
};
}
}
10 changes: 10 additions & 0 deletions src/Enum/PaperSizeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sensiolabs\GotenbergBundle\Enum;

interface PaperSizeInterface
{
public function width(): float;

public function height(): float;
}
21 changes: 21 additions & 0 deletions tests/Builder/HtmlPdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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((string) PaperSize::A3->width(), $multipartFormData[1]['paperWidth']);
self::assertSame((string) PaperSize::A3->height(), $multipartFormData[2]['paperHeight']);
}

public function testWithHeader(): void
{
$client = $this->createMock(GotenbergClientInterface::class);
Expand Down
27 changes: 27 additions & 0 deletions tests/Enum/PaperSizeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Sensiolabs\GotenbergBundle\Tests\Enum;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Sensiolabs\GotenbergBundle\Enum\PaperSize;

#[CoversClass(PaperSize::class)]
final class PaperSizeTest extends TestCase
{
public function testWidth(): void
{
foreach (PaperSize::cases() as $size) {
$size->width();
self::addToAssertionCount(1);
}
}

public function testHeight(): void
{
foreach (PaperSize::cases() as $size) {
$size->height();
self::addToAssertionCount(1);
}
}
}

0 comments on commit dd8d61d

Please sign in to comment.