Skip to content

Commit

Permalink
feat(libreoffice): add a bunch of new form fields
Browse files Browse the repository at this point in the history
  • Loading branch information
gulien committed Jul 3, 2024
1 parent 43d44cd commit 241005e
Show file tree
Hide file tree
Showing 2 changed files with 288 additions and 12 deletions.
183 changes: 176 additions & 7 deletions src/Modules/LibreOffice.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public function nativePageRanges(string $ranges): self
}

/**
* Sets whether to export the form fields or to use the inputted/selected
* content of the fields.
* Specifies whether form fields are exported as widgets or only their fixed
* print representation is exported.
*/
public function exportFormFields(bool $export = true): self
{
Expand All @@ -66,7 +66,150 @@ public function exportFormFields(bool $export = true): self
}

/**
* Sets whether to render the entire spreadsheet as a single page.
* Specifies whether multiple form fields exported are allowed to have the
* same field name.
*/
public function allowDuplicateFieldNames(): self
{
$this->formValue('allowDuplicateFieldNames', true);

return $this;
}

/**
* Specifies if bookmarks are exported to PDF.
*/
public function exportBookmarks(bool $export = true): self
{
$this->formValue('exportBookmarks', $export ?: '0');

return $this;
}

/**
* Specifies that the bookmarks contained in the source LibreOffice file
* should be exported to the PDF file as Named Destination.
*/
public function exportBookmarksToPdfDestination(): self
{
$this->formValue('exportBookmarksToPdfDestination', true);

return $this;
}

/**
* Exports the placeholders fields visual markings only. The exported
* placeholder is ineffective.
*/
public function exportPlaceholders(): self
{
$this->formValue('exportPlaceholders', true);

return $this;
}

/**
* Specifies if notes are exported to PDF.
*/
public function exportNotes(): self
{
$this->formValue('exportNotes', true);

return $this;
}

/**
* Specifies if notes pages are exported to PDF. Notes pages are available
* in Impress documents only.
*/
public function exportNotesPages(): self
{
$this->formValue('exportNotesPages', true);

return $this;
}

/**
* Specifies, if the form field exportNotesPages is set to true, if only
* notes pages are exported to PDF.
*/
public function exportOnlyNotesPages(): self
{
$this->formValue('exportOnlyNotesPages', true);

return $this;
}

/**
* Specifies if notes in margin are exported to PDF.
*/
public function exportNotesInMargin(): self
{
$this->formValue('exportNotesInMargin', true);

return $this;
}

/**
* Specifies that the target documents with .od[tpgs] extension, will have
* that extension changed to .pdf when the link is exported to PDF. The
* source document remains untouched.
*/
public function convertOooTargetToPdfTarget(): self
{
$this->formValue('convertOooTargetToPdfTarget', true);

return $this;
}

/**
* Specifies that the file system related hyperlinks (file:// protocol)
* present in the document will be exported as relative to the source
* document location.
*/
public function exportLinksRelativeFsys(): self
{
$this->formValue('exportLinksRelativeFsys', true);

return $this;
}

/**
* Exports, for LibreOffice Impress, slides that are not included in slide
* shows.
*/
public function exportHiddenSlides(): self
{
$this->formValue('exportHiddenSlides', true);

return $this;
}

/**
* Specifies that automatically inserted empty pages are suppressed. This
* option is active only if storing Writer documents.
*/
public function skipEmptyPages(): self
{
$this->formValue('skipEmptyPages', true);

return $this;
}

/**
* Specifies that a stream is inserted to the PDF file which contains the
* original document for archiving purposes.
*/
public function addOriginalDocumentAsStream(): self
{
$this->formValue('addOriginalDocumentAsStream', true);

return $this;
}

/**
* Ignores each sheet’s paper size, print ranges and shown/hidden status
* and puts every sheet (even hidden sheets) on exactly one page.
*/
public function singlePageSheets(): self
{
Expand All @@ -76,7 +219,8 @@ public function singlePageSheets(): self
}

/**
* Turns on lossless compression to tweak image conversion performance.
* Specifies if images are exported to PDF using a lossless compression
* format like PNG or compressed using the JPEG format.
*/
public function losslessImageCompression(): self
{
Expand All @@ -86,11 +230,36 @@ public function losslessImageCompression(): self
}

/**
* Turns on or off image resolution reduction to tweak image conversion performance.
* Specifies the quality of the JPG export. A higher value produces a
* higher-quality image and a larger file. Between 1 and 100.
*/
public function quality(int $quality): self
{
$this->formValue('quality', $quality);

return $this;
}

/**
* Specifies if the resolution of each image is reduced to the resolution
* specified by the form field maxImageResolution.
* FIXME: parameter not used.
*/
public function reduceImageResolution(bool $notUsedAnymore = true): self
{
$this->formValue('reduceImageResolution', true);

return $this;
}

/**
* If the form field reduceImageResolution is set to true, tells if all
* images will be reduced to the given value in DPI. Possible values are:
* 75, 150, 300, 600 and 1200.
*/
public function reduceImageResolution(bool $reduce = true): self
public function maxImageResolution(int $dpi): self
{
$this->formValue('reduceImageResolution', $reduce ?: '0');
$this->formValue('maxImageResolution', $dpi);

return $this;
}
Expand Down
117 changes: 112 additions & 5 deletions tests/Modules/LibreOfficeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,24 @@ function (
bool $landscape = false,
string|null $nativePageRanges = null,
bool|null $exportFormFields = null,
bool $allowDuplicateFieldNames = false,
bool|null $exportBookmarks = null,
bool $exportBookmarksToPdfDestination = false,
bool $exportPlaceholders = false,
bool $exportNotes = false,
bool $exportNotesPages = false,
bool $exportOnlyNotesPages = false,
bool $exportNotesInMargin = false,
bool $convertOooTargetToPdfTarget = false,
bool $exportLinksRelativeFsys = false,
bool $exportHiddenSlides = false,
bool $skipEmptyPages = false,
bool $addOriginalDocumentAsStream = false,
bool $singlePageSheets = false,
bool $losslessImageCompression = false,
bool|null $reduceImageResolution = true,
int|null $quality = null,
bool $reduceImageResolution = false,
int|null $maxImageResolution = null,
string|null $pdfa = null,
bool $pdfua = false,
array $metadata = [],
Expand All @@ -40,6 +55,58 @@ function (
$libreOffice->exportFormFields($exportFormFields);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

if ($singlePageSheets) {
$libreOffice->singlePageSheets();
}
Expand All @@ -48,8 +115,16 @@ function (
$libreOffice->losslessImageCompression();
}

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

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

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

if ($pdfa !== null) {
Expand Down Expand Up @@ -77,9 +152,26 @@ function (
expect($body)->unless($landscape === false, fn ($body) => $body->toContainFormValue('landscape', '1'));
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($allowDuplicateFieldNames === false, fn ($body) => $body->toContainFormValue('allowDuplicateFieldNames', '1'));
expect($body)->unless($exportBookmarks === null, fn ($body) => $body->toContainFormValue('exportBookmarks', $exportBookmarks === true ? '1' : '0'));
expect($body)->unless($exportBookmarksToPdfDestination === false, fn ($body) => $body->toContainFormValue('exportBookmarksToPdfDestination', '1'));
expect($body)->unless($exportPlaceholders === false, fn ($body) => $body->toContainFormValue('exportPlaceholders', '1'));
expect($body)->unless($exportNotes === false, fn ($body) => $body->toContainFormValue('exportNotes', '1'));
expect($body)->unless($exportNotesPages === false, fn ($body) => $body->toContainFormValue('exportNotesPages', '1'));
expect($body)->unless($exportOnlyNotesPages === false, fn ($body) => $body->toContainFormValue('exportOnlyNotesPages', '1'));
expect($body)->unless($exportNotesInMargin === false, fn ($body) => $body->toContainFormValue('exportNotesInMargin', '1'));
expect($body)->unless($convertOooTargetToPdfTarget === false, fn ($body) => $body->toContainFormValue('convertOooTargetToPdfTarget', '1'));
expect($body)->unless($exportLinksRelativeFsys === false, fn ($body) => $body->toContainFormValue('exportLinksRelativeFsys', '1'));
expect($body)->unless($exportHiddenSlides === false, fn ($body) => $body->toContainFormValue('exportHiddenSlides', '1'));
expect($body)->unless($skipEmptyPages === false, fn ($body) => $body->toContainFormValue('skipEmptyPages', '1'));
expect($body)->unless($addOriginalDocumentAsStream === false, fn ($body) => $body->toContainFormValue('addOriginalDocumentAsStream', '1'));
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($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));
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'));

if (count($metadata) > 0) {
Expand Down Expand Up @@ -113,8 +205,23 @@ function (
'1-2',
false,
true,
true,
false,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
100,
true,
150,
'PDF/A-1a',
true,
[ 'Producer' => 'Gotenberg' ],
Expand Down

0 comments on commit 241005e

Please sign in to comment.