Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a possibility of customizing with the standard paper format #28

Merged
merged 7 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions docs/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
--------------------
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\PaperSize;
use Sensiolabs\GotenbergBundle\Enum\PdfPart;
use Sensiolabs\GotenbergBundle\Exception\ExtraHttpHeadersJsonEncodingException;
use Sensiolabs\GotenbergBundle\Exception\InvalidBuilderConfiguration;
Expand Down Expand Up @@ -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;
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
Jean-Beru marked this conversation as resolved.
Show resolved Hide resolved
{
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
Neirda24 marked this conversation as resolved.
Show resolved Hide resolved
{
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,
};
}
}
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(PaperSize::A3->width(), $multipartFormData[1]['paperWidth']);
self::assertSame(PaperSize::A3->height(), $multipartFormData[2]['paperHeight']);
}

public function testWithHeader(): void
{
$client = $this->createMock(GotenbergClientInterface::class);
Expand Down