Skip to content

Commit

Permalink
feat(chromium): add singlePage form field support
Browse files Browse the repository at this point in the history
  • Loading branch information
gulien committed Feb 23, 2024
1 parent 2825943 commit f8aebad
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions src/Modules/ChromiumPdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Modules/ChromiumPdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
function (
string $url,
bool $singlePage = false,
float|null $paperWidth = null,
float $paperHeight = 0,
float|null $marginTop = null,
Expand Down Expand Up @@ -44,6 +45,7 @@ function (
$chromium = Gotenberg::chromium('')->pdf();
$chromium = hydrateChromiumPdfFormData(
$chromium,
$singlePage,
$paperWidth,
$paperHeight,
$marginTop,
Expand Down Expand Up @@ -78,6 +80,7 @@ function (

expectChromiumPdfOptions(
$body,
$singlePage,
$paperWidth,
$paperHeight,
$marginTop,
Expand Down Expand Up @@ -109,6 +112,7 @@ function (
['https://my.url'],
[
'https://my.url',
true,
8.27,
11.7,
2,
Expand Down Expand Up @@ -150,6 +154,7 @@ function (
*/
function (
Stream $index,
bool $singlePage = false,
float|null $paperWidth = null,
float $paperHeight = 0,
float|null $marginTop = null,
Expand Down Expand Up @@ -178,6 +183,7 @@ function (
$chromium = Gotenberg::chromium('')->pdf();
$chromium = hydrateChromiumPdfFormData(
$chromium,
$singlePage,
$paperWidth,
$paperHeight,
$marginTop,
Expand Down Expand Up @@ -214,6 +220,7 @@ function (

expectChromiumPdfOptions(
$body,
$singlePage,
$paperWidth,
$paperHeight,
$marginTop,
Expand Down Expand Up @@ -244,6 +251,7 @@ function (
[Stream::string('my.html', 'HTML content')],
[
Stream::string('my.html', 'HTML content'),
true,
8.27,
11.7,
2,
Expand Down Expand Up @@ -287,6 +295,7 @@ function (
function (
Stream $index,
array $markdowns,
bool $singlePage = false,
float|null $paperWidth = null,
float $paperHeight = 0,
float|null $marginTop = null,
Expand Down Expand Up @@ -315,6 +324,7 @@ function (
$chromium = Gotenberg::chromium('')->pdf();
$chromium = hydrateChromiumPdfFormData(
$chromium,
$singlePage,
$paperWidth,
$paperHeight,
$marginTop,
Expand Down Expand Up @@ -356,6 +366,7 @@ function (

expectChromiumPdfOptions(
$body,
$singlePage,
$paperWidth,
$paperHeight,
$marginTop,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -434,6 +446,7 @@ function (
*/
function hydrateChromiumPdfFormData(
ChromiumPdf $chromium,
bool $singlePage = false,
float|null $paperWidth = null,
float $paperHeight = 0,
float|null $marginTop = null,
Expand All @@ -459,6 +472,10 @@ function hydrateChromiumPdfFormData(
bool $pdfua = false,
array $assets = [],
): ChromiumPdf {
if ($singlePage) {
$chromium->singlePage();
}

if ($paperWidth !== null) {
$chromium->paperSize($paperWidth, $paperHeight);
}
Expand Down Expand Up @@ -553,6 +570,7 @@ function hydrateChromiumPdfFormData(
*/
function expectChromiumPdfOptions(
string $body,
bool $singlePage,
float|null $paperWidth,
float $paperHeight,
float|null $marginTop,
Expand All @@ -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 . '')
Expand Down

0 comments on commit f8aebad

Please sign in to comment.