Skip to content

Commit

Permalink
feat(pdfengines/libreoffice): add flatten feature
Browse files Browse the repository at this point in the history
  • Loading branch information
gulien committed Jan 29, 2025
1 parent 2cf43a9 commit f466c8d
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 12 deletions.
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@
"phpstan analyse -l max src tests"
],
"lint:fix": "phpcbf",
"tests": "XDEBUG_MODE=coverage pest --coverage --coverage-html coverage_html --coverage-clover coverage.xml"
"tests": "XDEBUG_MODE=coverage pest --coverage --coverage-html coverage_html --coverage-clover coverage.xml",
"all": [
"@composer run lint:fix",
"@composer run lint",
"@composer run tests"
]
},
"config": {
"sort-packages": true,
Expand Down
10 changes: 10 additions & 0 deletions src/Modules/LibreOffice.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ public function split(SplitMode $mode): self
return $this;
}

/**
* Defines whether the resulting PDF should be flattened.
*/
public function flatten(): self
{
$this->formValue('flatten', true);

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
35 changes: 31 additions & 4 deletions src/Modules/PdfEngines.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ public function metadata(array $metadata): self
return $this;
}

/**
* Defines whether the resulting PDF should be flattened.
* Prefer the flatten method if you only want to flatten one or more PDFs.
*/
public function flattening(): self
{
$this->formValue('flatten', true);

return $this;
}

/**
* Merges PDFs into a unique PDF.
*
Expand All @@ -89,25 +100,41 @@ public function merge(Stream ...$pdfs): RequestInterface
}

/**
* Splits PDFs.
* Splits PDF(s).
* Gotenberg will return the PDF or a ZIP archive with the 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->formFile($pdf->getFilename(), $pdf->getStream());
}

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

return $this->request();
}

/**
* Flatten PDF(s).
* Gotenberg will return the PDF or a ZIP archive with the PDFs.
*/
public function flatten(Stream ...$pdfs): RequestInterface
{
$this->formValue('flatten', true);

foreach ($pdfs as $pdf) {
$this->formFile($pdf->getFilename(), $pdf->getStream());
}

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

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
7 changes: 7 additions & 0 deletions tests/Modules/LibreOfficeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function (
bool $pdfua = false,
array $metadata = [],
bool $merge = false,
bool $flatten = false,
): void {
$libreOffice = Gotenberg::libreOffice('');

Expand Down Expand Up @@ -156,6 +157,10 @@ function (
->merge();
}

if ($flatten) {
$libreOffice->flatten();
}

$request = $libreOffice->convert(...$files);
$body = sanitize($request->getBody()->getContents());

Expand Down Expand Up @@ -192,6 +197,7 @@ function (
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'));
expect($body)->unless($flatten === false, fn ($body) => $body->toContainFormValue('flatten', '1'));

if (count($metadata) > 0) {
$json = json_encode($metadata);
Expand Down Expand Up @@ -247,5 +253,6 @@ function (
true,
[ 'Producer' => 'Gotenberg' ],
true,
true,
],
]);
89 changes: 82 additions & 7 deletions tests/Modules/PdfEnginesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @param Stream[] $pdfs
* @param array<string,string|bool|float|int|array<string>> $metadata
*/
function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $metadata = []): void {
function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $metadata = [], bool $flatten = false): void {
$pdfEngines = Gotenberg::pdfEngines('')->index(new DummyIndex());

if ($pdfa !== null) {
Expand All @@ -29,12 +29,16 @@ function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $met
$pdfEngines->metadata($metadata);
}

if ($flatten) {
$pdfEngines->flattening();
}

$request = $pdfEngines->merge(...$pdfs);
$body = sanitize($request->getBody()->getContents());

expect($request->getUri()->getPath())->toBe('/forms/pdfengines/merge');
expect($body)->unless($pdfa === null, fn ($body) => $body->toContainFormValue('pdfa', $pdfa));
expect($body)->unless($pdfua === false, fn ($body) => $body->toContainFormValue('pdfa', $pdfa));
expect($body)->unless($pdfua === false, fn ($body) => $body->toContainFormValue('pdfua', '1'));

if (count($metadata) > 0) {
$json = json_encode($metadata);
Expand All @@ -45,6 +49,8 @@ function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $met
expect($body)->toContainFormValue('metadata', $json);
}

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

foreach ($pdfs as $pdf) {
$pdf->getStream()->rewind();
expect($body)->toContainFormFile('foo_' . $pdf->getFilename(), $pdf->getStream()->getContents(), 'application/pdf');
Expand All @@ -66,14 +72,31 @@ function (array $pdfs, string|null $pdfa = null, bool $pdfua = false, array $met
'PDF/A-1a',
true,
[ 'Producer' => 'Gotenberg' ],
true,
],
]);

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());
function (array $pdfs, SplitMode $mode, string|null $pdfa = null, bool $pdfua = false, array $metadata = [], bool $flatten = false): void {
$pdfEngines = Gotenberg::pdfEngines('');

if ($pdfa !== null) {
$pdfEngines->pdfa($pdfa);
}

if ($pdfua) {
$pdfEngines->pdfua();
}

if (count($metadata) > 0) {
$pdfEngines->metadata($metadata);
}

if ($flatten) {
$pdfEngines->flattening();
}

$request = $pdfEngines->split($mode, ...$pdfs);
$body = sanitize($request->getBody()->getContents());
Expand All @@ -82,10 +105,23 @@ function (array $pdfs, SplitMode $mode): void {
expect($body)->toContainFormValue('splitMode', $mode->mode);
expect($body)->toContainFormValue('splitSpan', $mode->span);
expect($body)->toContainFormValue('splitUnify', $mode->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'));

if (count($metadata) > 0) {
$json = json_encode($metadata);
if ($json === false) {
throw NativeFunctionErrored::createFromLastPhpError();
}

expect($body)->toContainFormValue('metadata', $json);
}

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

foreach ($pdfs as $pdf) {
$pdf->getStream()->rewind();
expect($body)->toContainFormFile('foo_' . $pdf->getFilename(), $pdf->getStream()->getContents(), 'application/pdf');
expect($body)->toContainFormFile($pdf->getFilename(), $pdf->getStream()->getContents(), 'application/pdf');
}
},
)->with([
Expand All @@ -102,17 +138,28 @@ function (array $pdfs, SplitMode $mode): void {
Stream::string('my_third.pdf', 'Third PDF content'),
],
SplitMode::pages('1-2', true),
'PDF/A-1a',
true,
[ 'Producer' => 'Gotenberg' ],
true,
],
]);

it(
'creates a valid request for the "/forms/pdfengines/convert" endpoint',
function (string $pdfa, Stream ...$pdfs): void {
$request = Gotenberg::pdfEngines('')->convert($pdfa, ...$pdfs);
function (string $pdfa, bool $pdfua, Stream ...$pdfs): void {
$pdfEngines = Gotenberg::pdfEngines('');

if ($pdfua) {
$pdfEngines->pdfua();
}

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

expect($request->getUri()->getPath())->toBe('/forms/pdfengines/convert');
expect($body)->toContainFormValue('pdfa', $pdfa);
expect($body)->unless($pdfua === false, fn ($body) => $body->toContainFormValue('pdfua', '1'));

foreach ($pdfs as $pdf) {
$pdf->getStream()->rewind();
Expand All @@ -122,15 +169,43 @@ function (string $pdfa, Stream ...$pdfs): void {
)->with([
[
'PDF/A-1a',
false,
Stream::string('my.pdf', 'PDF content'),
],
[
'PDF/A-1a',
true,
Stream::string('my.pdf', 'PDF content'),
Stream::string('my_second.pdf', 'Second PDF content'),
],
]);

it(
'creates a valid request for the "/forms/pdfengines/flatten" endpoint',
/** @param Stream[] $pdfs */
function (array $pdfs): void {
$pdfEngines = Gotenberg::pdfEngines('');

$request = $pdfEngines->flatten(...$pdfs);
$body = sanitize($request->getBody()->getContents());

expect($request->getUri()->getPath())->toBe('/forms/pdfengines/flatten');
expect($body)->toContainFormValue('flatten', '1');

foreach ($pdfs as $pdf) {
$pdf->getStream()->rewind();
expect($body)->toContainFormFile($pdf->getFilename(), $pdf->getStream()->getContents(), 'application/pdf');
}
},
)->with([
[
[
Stream::string('my.pdf', 'PDF content'),
Stream::string('my_second.pdf', 'Second PDF content'),
],
],
]);

it(
'creates a valid request for the "/forms/pdfengines/metadata/read" endpoint',
/** @param Stream[] $pdfs */
Expand Down

0 comments on commit f466c8d

Please sign in to comment.