From f8aebad9eee912c565158c2df5f3af56ebe5cadf Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Fri, 23 Feb 2024 22:02:15 +0100 Subject: [PATCH] feat(chromium): add singlePage form field support --- README.md | 13 +++++++++++++ src/Modules/ChromiumPdf.php | 10 ++++++++++ tests/Modules/ChromiumPdfTest.php | 20 ++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/README.md b/README.md index 101cc9b..5b2d8ea 100644 --- a/README.md +++ b/README.md @@ -344,6 +344,19 @@ $request = Gotenberg::chromium($apiUrl) ); ``` +### Single page + +You may print the entire content in one single page with: + +```php +use Gotenberg\Gotenberg; + +$request = Gotenberg::chromium($apiUrl) + ->pdf() + ->singlePage() + ->url('https://my.url'); +``` + #### Paper size You may override the default paper size with: diff --git a/src/Modules/ChromiumPdf.php b/src/Modules/ChromiumPdf.php index 8b75c72..a0c2fb4 100644 --- a/src/Modules/ChromiumPdf.php +++ b/src/Modules/ChromiumPdf.php @@ -12,6 +12,16 @@ class ChromiumPdf { use ChromiumMultipartFormDataModule; + /** + * Defines whether to print the entire content in one single page. + */ + public function singlePage(): self + { + $this->formValue('singlePage', true); + + return $this; + } + /** * Overrides the default paper size, in inches. * diff --git a/tests/Modules/ChromiumPdfTest.php b/tests/Modules/ChromiumPdfTest.php index c6cf7d4..5430307 100644 --- a/tests/Modules/ChromiumPdfTest.php +++ b/tests/Modules/ChromiumPdfTest.php @@ -16,6 +16,7 @@ */ function ( string $url, + bool $singlePage = false, float|null $paperWidth = null, float $paperHeight = 0, float|null $marginTop = null, @@ -44,6 +45,7 @@ function ( $chromium = Gotenberg::chromium('')->pdf(); $chromium = hydrateChromiumPdfFormData( $chromium, + $singlePage, $paperWidth, $paperHeight, $marginTop, @@ -78,6 +80,7 @@ function ( expectChromiumPdfOptions( $body, + $singlePage, $paperWidth, $paperHeight, $marginTop, @@ -109,6 +112,7 @@ function ( ['https://my.url'], [ 'https://my.url', + true, 8.27, 11.7, 2, @@ -150,6 +154,7 @@ function ( */ function ( Stream $index, + bool $singlePage = false, float|null $paperWidth = null, float $paperHeight = 0, float|null $marginTop = null, @@ -178,6 +183,7 @@ function ( $chromium = Gotenberg::chromium('')->pdf(); $chromium = hydrateChromiumPdfFormData( $chromium, + $singlePage, $paperWidth, $paperHeight, $marginTop, @@ -214,6 +220,7 @@ function ( expectChromiumPdfOptions( $body, + $singlePage, $paperWidth, $paperHeight, $marginTop, @@ -244,6 +251,7 @@ function ( [Stream::string('my.html', 'HTML content')], [ Stream::string('my.html', 'HTML content'), + true, 8.27, 11.7, 2, @@ -287,6 +295,7 @@ function ( function ( Stream $index, array $markdowns, + bool $singlePage = false, float|null $paperWidth = null, float $paperHeight = 0, float|null $marginTop = null, @@ -315,6 +324,7 @@ function ( $chromium = Gotenberg::chromium('')->pdf(); $chromium = hydrateChromiumPdfFormData( $chromium, + $singlePage, $paperWidth, $paperHeight, $marginTop, @@ -356,6 +366,7 @@ function ( expectChromiumPdfOptions( $body, + $singlePage, $paperWidth, $paperHeight, $marginTop, @@ -395,6 +406,7 @@ function ( Stream::string('my.md', 'Markdown content'), Stream::string('my_second.md', 'Second Markdown content'), ], + true, 8.27, 11.7, 2, @@ -434,6 +446,7 @@ function ( */ function hydrateChromiumPdfFormData( ChromiumPdf $chromium, + bool $singlePage = false, float|null $paperWidth = null, float $paperHeight = 0, float|null $marginTop = null, @@ -459,6 +472,10 @@ function hydrateChromiumPdfFormData( bool $pdfua = false, array $assets = [], ): ChromiumPdf { + if ($singlePage) { + $chromium->singlePage(); + } + if ($paperWidth !== null) { $chromium->paperSize($paperWidth, $paperHeight); } @@ -553,6 +570,7 @@ function hydrateChromiumPdfFormData( */ function expectChromiumPdfOptions( string $body, + bool $singlePage, float|null $paperWidth, float $paperHeight, float|null $marginTop, @@ -578,6 +596,8 @@ function expectChromiumPdfOptions( bool $pdfua, array $assets, ): void { + expect($body)->unless($singlePage === false, fn ($body) => $body->toContainFormValue('singlePage', '1')); + if ($paperWidth !== null) { expect($body) ->toContainFormValue('paperWidth', $paperWidth . '')