Skip to content

Commit

Permalink
feat(libreoffice): add image conversion performance methods
Browse files Browse the repository at this point in the history
  • Loading branch information
meetic-julienneuhart committed Jun 13, 2024
1 parent da5b899 commit bfaddfb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 23 additions & 3 deletions src/Modules/LibreOffice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
{
Expand All @@ -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.
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/Modules/LibreOfficeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
Expand All @@ -42,6 +44,14 @@ function (
$libreOffice->singlePageSheets();
}

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

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

if ($pdfa !== null) {
$libreOffice->pdfa($pdfa);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -101,6 +113,8 @@ function (
'1-2',
false,
true,
true,
false,
'PDF/A-1a',
true,
[ 'Producer' => 'Gotenberg' ],
Expand Down

0 comments on commit bfaddfb

Please sign in to comment.