Skip to content

Commit

Permalink
feat(split): add the split feature
Browse files Browse the repository at this point in the history
  • Loading branch information
gulien committed Dec 22, 2024
1 parent 00efc25 commit dbbb6c0
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Modules/ChromiumPdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Gotenberg\Modules;

use Gotenberg\Exceptions\NativeFunctionErrored;
use Gotenberg\SplitMode;
use Gotenberg\Stream;
use Psr\Http\Message\RequestInterface;

Expand Down Expand Up @@ -153,6 +154,18 @@ public function footer(Stream $footer): self
return $this;
}

/**
* Splits the resulting PDF.
*/
public function split(SplitMode $mode): self
{
$this->formValue('splitMode', $mode->mode);
$this->formValue('splitSpan', $mode->span);
$this->formValue('splitUnify', $mode->unify ?: '0');

return $this;
}

/**
* Sets the PDF/A format of the resulting PDF.
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Modules/LibreOffice.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Gotenberg\HrtimeIndex;
use Gotenberg\Index;
use Gotenberg\MultipartFormDataModule;
use Gotenberg\SplitMode;
use Gotenberg\Stream;
use Psr\Http\Message\RequestInterface;

Expand Down Expand Up @@ -324,6 +325,18 @@ public function merge(): self
return $this;
}

/**
* Splits the resulting PDFs.
*/
public function split(SplitMode $mode): self
{
$this->formValue('splitMode', $mode->mode);
$this->formValue('splitSpan', $mode->span);
$this->formValue('splitUnify', $mode->unify ?: '0');

return $this;
}

/**
* Converts the given document(s) to PDF(s). Gotenberg will return either
* a unique PDF if you request a merge or a ZIP archive with the PDFs.
Expand Down
21 changes: 21 additions & 0 deletions src/Modules/PdfEngines.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Gotenberg\HrtimeIndex;
use Gotenberg\Index;
use Gotenberg\MultipartFormDataModule;
use Gotenberg\SplitMode;
use Gotenberg\Stream;
use Psr\Http\Message\RequestInterface;

Expand Down Expand Up @@ -87,6 +88,26 @@ public function merge(Stream ...$pdfs): RequestInterface
return $this->request();
}

/**
* Splits PDFs.
*/
public function split(SplitMode $mode, Stream ...$pdfs): RequestInterface
{
$this->formValue('splitMode', $mode->mode);
$this->formValue('splitSpan', $mode->span);
$this->formValue('splitUnify', $mode->unify ?: '0');

$index = $this->index ?? new HrtimeIndex();

foreach ($pdfs as $pdf) {
$this->formFile($index->create() . '_' . $pdf->getFilename(), $pdf->getStream());
}

$this->endpoint = '/forms/pdfengines/split';

return $this->request();
}

/**
* Converts PDF(s) to a specific PDF/A format.
* Gotenberg will return the PDF or a ZIP archive with the PDFs.
Expand Down
33 changes: 33 additions & 0 deletions src/SplitMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Gotenberg;

class SplitMode
{
public function __construct(
public readonly string $mode,
public readonly string $span,
public readonly bool $unify,
) {
}

public static function intervals(int $span): self
{
return new self(
'intervals',
$span . '',
false,
);
}

public static function pages(string $span, bool $unify = false): self
{
return new self(
'pages',
$span,
$unify,
);
}
}
26 changes: 26 additions & 0 deletions tests/Modules/ChromiumPdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Gotenberg\Gotenberg;
use Gotenberg\Modules\ChromiumCookie;
use Gotenberg\Modules\ChromiumPdf;
use Gotenberg\SplitMode;
use Gotenberg\Stream;

it(
Expand Down Expand Up @@ -47,6 +48,7 @@ function (
bool $failOnResourceLoadingFailed = false,
bool $failOnConsoleExceptions = false,
bool|null $skipNetworkIdleEvent = null,
SplitMode|null $splitMode = null,
string|null $pdfa = null,
bool $pdfua = false,
array $metadata = [],
Expand Down Expand Up @@ -82,6 +84,7 @@ function (
$failOnResourceLoadingFailed,
$failOnConsoleExceptions,
$skipNetworkIdleEvent,
$splitMode,
$pdfa,
$pdfua,
$metadata,
Expand Down Expand Up @@ -123,6 +126,7 @@ function (
$failOnResourceLoadingFailed,
$failOnConsoleExceptions,
$skipNetworkIdleEvent,
$splitMode,
$pdfa,
$pdfua,
$metadata,
Expand Down Expand Up @@ -167,6 +171,7 @@ function (
true,
true,
true,
SplitMode::intervals(1),
'PDF/A-1a',
true,
[ 'Producer' => 'Gotenberg' ],
Expand Down Expand Up @@ -215,6 +220,7 @@ function (
bool $failOnResourceLoadingFailed = false,
bool $failOnConsoleExceptions = false,
bool|null $skipNetworkIdleEvent = null,
SplitMode|null $splitMode = null,
string|null $pdfa = null,
bool $pdfua = false,
array $metadata = [],
Expand Down Expand Up @@ -250,6 +256,7 @@ function (
$failOnResourceLoadingFailed,
$failOnConsoleExceptions,
$skipNetworkIdleEvent,
$splitMode,
$pdfa,
$pdfua,
$metadata,
Expand Down Expand Up @@ -293,6 +300,7 @@ function (
$failOnResourceLoadingFailed,
$failOnConsoleExceptions,
$skipNetworkIdleEvent,
$splitMode,
$pdfa,
$pdfua,
$metadata,
Expand Down Expand Up @@ -336,6 +344,7 @@ function (
true,
true,
true,
SplitMode::intervals(1),
'PDF/A-1a',
true,
[ 'Producer' => 'Gotenberg' ],
Expand Down Expand Up @@ -386,6 +395,7 @@ function (
bool $failOnResourceLoadingFailed = false,
bool $failOnConsoleExceptions = false,
bool|null $skipNetworkIdleEvent = null,
SplitMode|null $splitMode = null,
string|null $pdfa = null,
bool $pdfua = false,
array $metadata = [],
Expand Down Expand Up @@ -421,6 +431,7 @@ function (
$failOnResourceLoadingFailed,
$failOnConsoleExceptions,
$skipNetworkIdleEvent,
$splitMode,
$pdfa,
$pdfua,
$metadata,
Expand Down Expand Up @@ -469,6 +480,7 @@ function (
$failOnResourceLoadingFailed,
$failOnConsoleExceptions,
$skipNetworkIdleEvent,
$splitMode,
$pdfa,
$pdfua,
$metadata,
Expand Down Expand Up @@ -521,6 +533,7 @@ function (
true,
true,
true,
SplitMode::intervals(1),
'PDF/A-1a',
true,
[ 'Producer' => 'Gotenberg' ],
Expand Down Expand Up @@ -567,6 +580,7 @@ function hydrateChromiumPdfFormData(
bool $failOnResourceLoadingFailed = false,
bool $failOnConsoleExceptions = false,
bool|null $skipNetworkIdleEvent = null,
SplitMode|null $splitMode = null,
string|null $pdfa = null,
bool $pdfua = false,
array $metadata = [],
Expand Down Expand Up @@ -668,6 +682,10 @@ function hydrateChromiumPdfFormData(
$chromium->skipNetworkIdleEvent($skipNetworkIdleEvent);
}

if ($splitMode !== null) {
$chromium->split($splitMode);
}

if ($pdfa !== null) {
$chromium->pdfa($pdfa);
}
Expand Down Expand Up @@ -724,6 +742,7 @@ function expectChromiumPdfOptions(
bool $failOnResourceLoadingFailed,
bool $failOnConsoleExceptions,
bool|null $skipNetworkIdleEvent,
SplitMode|null $splitMode,
string|null $pdfa,
bool $pdfua,
array $metadata,
Expand Down Expand Up @@ -808,6 +827,13 @@ function expectChromiumPdfOptions(
expect($body)->unless($failOnResourceLoadingFailed === false, fn ($body) => $body->toContainFormValue('failOnResourceLoadingFailed', '1'));
expect($body)->unless($failOnConsoleExceptions === false, fn ($body) => $body->toContainFormValue('failOnConsoleExceptions', '1'));
expect($body)->unless($skipNetworkIdleEvent === null, fn ($body) => $body->toContainFormValue('skipNetworkIdleEvent', $skipNetworkIdleEvent === true ? '1' : '0'));

if ($splitMode !== null) {
expect($body)->toContainFormValue('splitMode', $splitMode->mode);
expect($body)->toContainFormValue('splitSpan', $splitMode->span);
expect($body)->toContainFormValue('splitUnify', $splitMode->unify ? '1' : '0');
}

expect($body)->unless($pdfa === null, fn ($body) => $body->toContainFormValue('pdfa', $pdfa));
expect($body)->unless($pdfua === false, fn ($body) => $body->toContainFormValue('pdfua', '1'));

Expand Down
14 changes: 14 additions & 0 deletions tests/Modules/LibreOfficeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Gotenberg\Exceptions\NativeFunctionErrored;
use Gotenberg\Gotenberg;
use Gotenberg\SplitMode;
use Gotenberg\Stream;
use Gotenberg\Test\DummyIndex;

Expand Down Expand Up @@ -37,6 +38,7 @@ function (
int|null $quality = null,
bool $reduceImageResolution = false,
int|null $maxImageResolution = null,
SplitMode|null $splitMode = null,
string|null $pdfa = null,
bool $pdfua = false,
array $metadata = [],
Expand Down Expand Up @@ -136,6 +138,10 @@ function (
$libreOffice->pdfa($pdfa);
}

if ($splitMode !== null) {
$libreOffice->split($splitMode);
}

if ($pdfua) {
$libreOffice->pdfua();
}
Expand Down Expand Up @@ -176,6 +182,13 @@ function (
expect($body)->unless($quality === null, fn ($body) => $body->toContainFormValue('quality', $quality));
expect($body)->unless($reduceImageResolution === false, fn ($body) => $body->toContainFormValue('reduceImageResolution', '1'));
expect($body)->unless($maxImageResolution === null, fn ($body) => $body->toContainFormValue('maxImageResolution', $maxImageResolution));

if ($splitMode !== null) {
expect($body)->toContainFormValue('splitMode', $splitMode->mode);
expect($body)->toContainFormValue('splitSpan', $splitMode->span);
expect($body)->toContainFormValue('splitUnify', $splitMode->unify ? '1' : '0');
}

expect($body)->unless($pdfa === null, fn ($body) => $body->toContainFormValue('pdfa', $pdfa));
expect($body)->unless($pdfua === false, fn ($body) => $body->toContainFormValue('pdfua', '1'));
expect($body)->unless($merge === false, fn ($body) => $body->toContainFormValue('merge', '1'));
Expand Down Expand Up @@ -229,6 +242,7 @@ function (
100,
true,
150,
SplitMode::intervals(1),
'PDF/A-1a',
true,
[ 'Producer' => 'Gotenberg' ],
Expand Down
37 changes: 37 additions & 0 deletions tests/Modules/PdfEnginesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Gotenberg\Exceptions\NativeFunctionErrored;
use Gotenberg\Gotenberg;
use Gotenberg\SplitMode;
use Gotenberg\Stream;
use Gotenberg\Test\DummyIndex;

Expand Down Expand Up @@ -68,6 +69,42 @@ function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $met
],
]);

it(
'creates a valid request for the "/forms/pdfengines/split" endpoint',
/** @param Stream[] $pdfs */
function (array $pdfs, SplitMode $mode): void {
$pdfEngines = Gotenberg::pdfEngines('')->index(new DummyIndex());

$request = $pdfEngines->split($mode, ...$pdfs);
$body = sanitize($request->getBody()->getContents());

expect($request->getUri()->getPath())->toBe('/forms/pdfengines/split');
expect($body)->toContainFormValue('splitMode', $mode->mode);
expect($body)->toContainFormValue('splitSpan', $mode->span);
expect($body)->toContainFormValue('splitUnify', $mode->unify ? '1' : '0');

foreach ($pdfs as $pdf) {
$pdf->getStream()->rewind();
expect($body)->toContainFormFile('foo_' . $pdf->getFilename(), $pdf->getStream()->getContents(), 'application/pdf');
}
},
)->with([
[
[
Stream::string('my.pdf', 'PDF content'),
],
SplitMode::intervals(1),
],
[
[
Stream::string('my.pdf', 'PDF content'),
Stream::string('my_second.pdf', 'Second PDF content'),
Stream::string('my_third.pdf', 'Third PDF content'),
],
SplitMode::pages('1-2', true),
],
]);

it(
'creates a valid request for the "/forms/pdfengines/convert" endpoint',
function (string $pdfa, Stream ...$pdfs): void {
Expand Down
25 changes: 25 additions & 0 deletions tests/SplitModeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Gotenberg\SplitMode;

it(
'creates an intervals split mode',
function (): void {
$mode = SplitMode::intervals(1);
expect($mode->mode)->toBe('intervals');
expect($mode->span)->toBe('1');
expect($mode->unify)->toBeFalse();
},
);

it(
'creates a pages split mode',
function (): void {
$mode = SplitMode::pages('1-2', true);
expect($mode->mode)->toBe('pages');
expect($mode->span)->toBe('1-2');
expect($mode->unify)->toBeTrue();
},
);

0 comments on commit dbbb6c0

Please sign in to comment.