From bfaddfb7321d843766050a38c60240cee0f17c42 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Thu, 13 Jun 2024 12:01:39 +0200 Subject: [PATCH] feat(libreoffice): add image conversion performance methods --- README.md | 14 ++++++++++++++ src/Modules/LibreOffice.php | 26 +++++++++++++++++++++++--- tests/Modules/LibreOfficeTest.php | 14 ++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b84f846..21d81ca 100644 --- a/README.md +++ b/README.md @@ -826,6 +826,20 @@ $request = Gotenberg::libreOffice($apiUrl) ->convert(Stream::path('/path/to/my.xlsx')); ``` +#### Images + +You may tweak image conversion performance with: + +```php +use Gotenberg\Gotenberg; +use Gotenberg\Stream; + +$request = Gotenberg::libreOffice($apiUrl) + ->losslessImageCompression() + ->reduceImageResolution(false) + ->convert(Stream::path('/path/to/my.docx')); +``` + #### PDF/A & PDF/UA See https://gotenberg.dev/docs/routes#pdfa-libreoffice. diff --git a/src/Modules/LibreOffice.php b/src/Modules/LibreOffice.php index 131723c..77bc14a 100644 --- a/src/Modules/LibreOffice.php +++ b/src/Modules/LibreOffice.php @@ -42,7 +42,7 @@ public function landscape(): self } /** - * Set the page ranges to print, e.g., "1-4"'. + * Sets the page ranges to print, e.g., "1-4"'. * Empty means all pages. * * Note: the page ranges are applied to all files independently. @@ -55,7 +55,7 @@ public function nativePageRanges(string $ranges): self } /** - * Set whether to export the form fields or to use the inputted/selected + * Sets whether to export the form fields or to use the inputted/selected * content of the fields. */ public function exportFormFields(bool $export = true): self @@ -66,7 +66,7 @@ public function exportFormFields(bool $export = true): self } /** - * Set whether to render the entire spreadsheet as a single page. + * Sets whether to render the entire spreadsheet as a single page. */ public function singlePageSheets(): self { @@ -75,6 +75,26 @@ public function singlePageSheets(): self return $this; } + /** + * Turns on lossless compression to tweak image conversion performance. + */ + public function losslessImageCompression(): self + { + $this->formValue('losslessImageCompression', true); + + return $this; + } + + /** + * Turns on or off image resolution reduction to tweak image conversion performance. + */ + public function reduceImageResolution(bool $reduce = true): self + { + $this->formValue('reduceImageResolution', $reduce ?: '0'); + + return $this; + } + /** * Sets the PDF/A format of the resulting PDF. */ diff --git a/tests/Modules/LibreOfficeTest.php b/tests/Modules/LibreOfficeTest.php index 20891a8..6b83c04 100644 --- a/tests/Modules/LibreOfficeTest.php +++ b/tests/Modules/LibreOfficeTest.php @@ -19,6 +19,8 @@ function ( string|null $nativePageRanges = null, bool|null $exportFormFields = null, bool $singlePageSheets = false, + bool $losslessImageCompression = false, + bool|null $reduceImageResolution = true, string|null $pdfa = null, bool $pdfua = false, array $metadata = [], @@ -42,6 +44,14 @@ function ( $libreOffice->singlePageSheets(); } + if ($losslessImageCompression) { + $libreOffice->losslessImageCompression(); + } + + if ($reduceImageResolution !== null) { + $libreOffice->reduceImageResolution($reduceImageResolution); + } + if ($pdfa !== null) { $libreOffice->pdfa($pdfa); } @@ -68,6 +78,8 @@ function ( expect($body)->unless($nativePageRanges === null, fn ($body) => $body->toContainFormValue('nativePageRanges', $nativePageRanges)); expect($body)->unless($exportFormFields === null, fn ($body) => $body->toContainFormValue('exportFormFields', $exportFormFields === true ? '1' : '0')); expect($body)->unless($singlePageSheets === false, fn ($body) => $body->toContainFormValue('singlePageSheets', '1')); + expect($body)->unless($losslessImageCompression === false, fn ($body) => $body->toContainFormValue('losslessImageCompression', '1')); + expect($body)->unless($reduceImageResolution === null, fn ($body) => $body->toContainFormValue('reduceImageResolution', $reduceImageResolution === true ? '1' : '0')); expect($body)->unless($merge === false, fn ($body) => $body->toContainFormValue('merge', '1')); if (count($metadata) > 0) { @@ -101,6 +113,8 @@ function ( '1-2', false, true, + true, + false, 'PDF/A-1a', true, [ 'Producer' => 'Gotenberg' ],