-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathLibreOffice.php
More file actions
403 lines (329 loc) · 9.75 KB
/
LibreOffice.php
File metadata and controls
403 lines (329 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
declare(strict_types=1);
namespace Gotenberg\Modules;
use Gotenberg\Exceptions\NativeFunctionErrored;
use Gotenberg\HrtimeIndex;
use Gotenberg\Index;
use Gotenberg\MultipartFormDataModule;
use Gotenberg\SplitMode;
use Gotenberg\Stream;
use Psr\Http\Message\RequestInterface;
use function json_encode;
class LibreOffice
{
use MultipartFormDataModule;
private Index|null $index = null;
private bool $merge = false;
/**
* Overrides the default index generator for ordering
* files we want to merge.
*/
public function index(Index $index): self
{
$this->index = $index;
return $this;
}
/**
* Sets the password for opening the source file.
*/
public function password(string $password): self
{
$this->formValue('password', $password);
return $this;
}
/**
* Sets the paper orientation to landscape.
*/
public function landscape(): self
{
$this->formValue('landscape', true);
return $this;
}
/**
* Sets the page ranges to print, e.g., "1-4"'.
* Empty means all pages.
*
* Note: the page ranges are applied to all files independently.
*/
public function nativePageRanges(string $ranges): self
{
$this->formValue('nativePageRanges', $ranges);
return $this;
}
/**
* Specifies whether to update the indexes before conversion, keeping in
* mind that doing so might result in missing links in the final PDF.
*/
public function updateIndexes(bool $update = true): self
{
$this->formValue('updateIndexes', $update ?: '0');
return $this;
}
/**
* Specifies whether form fields are exported as widgets or only their fixed
* print representation is exported.
*/
public function exportFormFields(bool $export = true): self
{
$this->formValue('exportFormFields', $export ?: '0');
return $this;
}
/**
* 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
{
$this->formValue('singlePageSheets', true);
return $this;
}
/**
* Specifies if images are exported to PDF using a lossless compression
* format like PNG or compressed using the JPEG format.
*/
public function losslessImageCompression(): self
{
$this->formValue('losslessImageCompression', true);
return $this;
}
/**
* 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 maxImageResolution(int $dpi): self
{
$this->formValue('maxImageResolution', $dpi);
return $this;
}
/**
* Sets the PDF/A format of the resulting PDF.
*/
public function pdfa(string $format): self
{
$this->formValue('pdfa', $format);
return $this;
}
/**
* Enables PDF for Universal Access for optimal accessibility.
*/
public function pdfua(): self
{
$this->formValue('pdfua', true);
return $this;
}
/**
* Sets the metadata to write.
*
* @param array<string,string|bool|float|int|array<string>> $metadata
*
* @throws NativeFunctionErrored
*/
public function metadata(array $metadata): self
{
$json = json_encode($metadata);
if ($json === false) {
throw NativeFunctionErrored::createFromLastPhpError();
}
$this->formValue('metadata', $json);
return $this;
}
/**
* Merges the resulting PDFs.
*/
public function merge(): self
{
$this->merge = true;
$this->formValue('merge', true);
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;
}
/**
* Defines whether the resulting PDF should be flattened.
*/
public function flatten(): self
{
$this->formValue('flatten', true);
return $this;
}
/**
* Defines whether the resulting PDF should be encrypted.
*/
public function encrypt(string $userPassword, string $ownerPassword = ''): self
{
$this->formValue('userPassword', $userPassword);
$this->formValue('ownerPassword', $ownerPassword);
return $this;
}
/**
* Sets the file to embed in the resulting PDF.
*/
public function embeds(Stream ...$embeds): self
{
foreach ($embeds as $embed) {
$this->formFile($embed->getFilename(), $embed->getStream(), 'embeds');
}
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.
*
* Note: if you requested a merge, the merging order is determined by the
* order of the arguments.
*/
public function convert(Stream ...$files): RequestInterface
{
$index = $this->index ?? new HrtimeIndex();
foreach ($files as $file) {
$filename = $this->merge ? $index->create() . '_' . $file->getFilename() : $file->getFilename();
$this->formFile($filename, $file->getStream());
}
$this->endpoint = '/forms/libreoffice/convert';
return $this->request();
}
}