Skip to content

Commit 7b4a56a

Browse files
committed
feat: add embed feature
1 parent 1c4e926 commit 7b4a56a

10 files changed

Lines changed: 187 additions & 10 deletions

src/DownloadFrom.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ class DownloadFrom implements JsonSerializable
1212
public function __construct(
1313
public readonly string $url,
1414
public readonly array|null $extraHttpHeaders = null,
15+
public readonly bool $embedded = false,
1516
) {
1617
}
1718

18-
/** @return array<string,string|array<string,string>> */
19+
/** @return array<string, array<string,string>|bool|string> */
1920
public function jsonSerialize(): array
2021
{
2122
$serialized = [
2223
'url' => $this->url,
24+
'embedded' => $this->embedded,
2325
];
2426

2527
if (! empty($this->extraHttpHeaders)) {

src/Modules/ChromiumPdf.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,18 @@ public function encrypt(string $userPassword, string $ownerPassword = ''): self
236236
return $this;
237237
}
238238

239+
/**
240+
* Sets the file to embed in the resulting PDF.
241+
*/
242+
public function embeds(Stream ...$embeds): self
243+
{
244+
foreach ($embeds as $embed) {
245+
$this->formFile($embed->getFilename(), $embed->getStream(), 'embeds');
246+
}
247+
248+
return $this;
249+
}
250+
239251
/**
240252
* Converts a target URL to PDF.
241253
*

src/Modules/LibreOffice.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,18 @@ public function encrypt(string $userPassword, string $ownerPassword = ''): self
369369
return $this;
370370
}
371371

372+
/**
373+
* Sets the file to embed in the resulting PDF.
374+
*/
375+
public function embeds(Stream ...$embeds): self
376+
{
377+
foreach ($embeds as $embed) {
378+
$this->formFile($embed->getFilename(), $embed->getStream(), 'embeds');
379+
}
380+
381+
return $this;
382+
}
383+
372384
/**
373385
* Converts the given document(s) to PDF(s). Gotenberg will return either
374386
* a unique PDF if you request a merge or a ZIP archive with the PDFs.

src/Modules/PdfEngines.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ public function encrypting(string $userPassword, string $ownerPassword = ''): se
9393
return $this;
9494
}
9595

96+
/**
97+
* Sets the file to embed in the resulting PDF.
98+
*/
99+
public function embeds(Stream ...$embeds): self
100+
{
101+
foreach ($embeds as $embed) {
102+
$this->formFile($embed->getFilename(), $embed->getStream(), 'embeds');
103+
}
104+
105+
return $this;
106+
}
107+
96108
/**
97109
* Merges PDFs into a unique PDF.
98110
*
@@ -216,4 +228,24 @@ public function encrypt(string $userPassword, string $ownerPassword = '', Stream
216228

217229
return $this->request();
218230
}
231+
232+
/**
233+
* Allows embedding one or more files to one or more PDF.
234+
*
235+
* @param Stream[] $embeds
236+
*
237+
* @throws NativeFunctionErrored
238+
*/
239+
public function embed(array $embeds, Stream ...$pdfs): RequestInterface
240+
{
241+
foreach ($pdfs as $pdf) {
242+
$this->formFile($pdf->getFilename(), $pdf->getStream());
243+
}
244+
245+
$this->embeds(...$embeds);
246+
247+
$this->endpoint = '/forms/pdfengines/embed';
248+
249+
return $this->request();
250+
}
219251
}

src/MultipartFormDataModule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ protected function formValue(string $name, mixed $value): self
118118
return $this;
119119
}
120120

121-
protected function formFile(string $filename, StreamInterface $stream): void
121+
protected function formFile(string $filename, StreamInterface $stream, string $name = 'files'): void
122122
{
123123
$this->multipartFormData[] = [
124-
'name' => 'files',
124+
'name' => $name,
125125
'filename' => $filename,
126126
'contents' => $stream,
127127
];

tests/Modules/ChromiumPdfTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* @param int[] $failOnHttpStatusCodes
1818
* @param int[] $failOnResourceHttpStatusCodes
1919
* @param array<string,string|bool|float|int|array<string>> $metadata
20+
* @param Stream[] $embeds
2021
* @param Stream[] $assets
2122
*/
2223
function (
@@ -56,6 +57,7 @@ function (
5657
bool $flatten = false,
5758
string $userPassword = '',
5859
string $ownerPassword = '',
60+
array $embeds = [],
5961
array $assets = [],
6062
): void {
6163
$chromium = Gotenberg::chromium('')->pdf();
@@ -96,6 +98,7 @@ function (
9698
$flatten,
9799
$userPassword,
98100
$ownerPassword,
101+
$embeds,
99102
$assets,
100103
);
101104

@@ -142,6 +145,7 @@ function (
142145
$flatten,
143146
$userPassword,
144147
$ownerPassword,
148+
$embeds,
145149
$assets,
146150
);
147151
},
@@ -191,6 +195,10 @@ function (
191195
true,
192196
'my_user_password',
193197
'my_owner_password',
198+
[
199+
Stream::string('my.xml', 'XML content'),
200+
Stream::string('my_second.xml', 'Second XML content'),
201+
],
194202
[
195203
Stream::string('my.jpg', 'Image content'),
196204
],
@@ -205,6 +213,7 @@ function (
205213
* @param int[] $failOnHttpStatusCodes
206214
* @param int[] $failOnResourceHttpStatusCodes
207215
* @param array<string,string|bool|float|int|array<string>> $metadata
216+
* @param Stream[] $embeds
208217
* @param Stream[] $assets
209218
*/
210219
function (
@@ -244,6 +253,7 @@ function (
244253
bool $flatten = false,
245254
string $userPassword = '',
246255
string $ownerPassword = '',
256+
array $embeds = [],
247257
array $assets = [],
248258
): void {
249259
$chromium = Gotenberg::chromium('')->pdf();
@@ -284,6 +294,7 @@ function (
284294
$flatten,
285295
$userPassword,
286296
$ownerPassword,
297+
$embeds,
287298
$assets,
288299
);
289300

@@ -332,6 +343,7 @@ function (
332343
$flatten,
333344
$userPassword,
334345
$ownerPassword,
346+
$embeds,
335347
$assets,
336348
);
337349
},
@@ -380,6 +392,10 @@ function (
380392
true,
381393
'my_user_password',
382394
'my_owner_password',
395+
[
396+
Stream::string('my.xml', 'XML content'),
397+
Stream::string('my_second.xml', 'Second XML content'),
398+
],
383399
[
384400
Stream::string('my.jpg', 'Image content'),
385401
],
@@ -395,6 +411,7 @@ function (
395411
* @param int[] $failOnResourceHttpStatusCodes
396412
* @param Stream[] $markdowns
397413
* @param array<string,string|bool|float|int|array<string>> $metadata
414+
* @param Stream[] $embeds
398415
* @param Stream[] $assets
399416
*/
400417
function (
@@ -435,6 +452,7 @@ function (
435452
bool $flatten = false,
436453
string $userPassword = '',
437454
string $ownerPassword = '',
455+
array $embeds = [],
438456
array $assets = [],
439457
): void {
440458
$chromium = Gotenberg::chromium('')->pdf();
@@ -475,6 +493,7 @@ function (
475493
$flatten,
476494
$userPassword,
477495
$ownerPassword,
496+
$embeds,
478497
$assets,
479498
);
480499

@@ -528,6 +547,7 @@ function (
528547
$flatten,
529548
$userPassword,
530549
$ownerPassword,
550+
$embeds,
531551
$assets,
532552
);
533553
},
@@ -585,6 +605,10 @@ function (
585605
true,
586606
'my_user_password',
587607
'my_owner_password',
608+
[
609+
Stream::string('my.xml', 'XML content'),
610+
Stream::string('my_second.xml', 'Second XML content'),
611+
],
588612
[
589613
Stream::string('my.jpg', 'Image content'),
590614
],
@@ -597,6 +621,7 @@ function (
597621
* @param int[] $failOnHttpStatusCodes
598622
* @param int[] $failOnResourceHttpStatusCodes
599623
* @param array<string,string|bool|float|int|array<string>> $metadata
624+
* @param Stream[] $embeds
600625
* @param Stream[] $assets
601626
*/
602627
function hydrateChromiumPdfFormData(
@@ -636,6 +661,7 @@ function hydrateChromiumPdfFormData(
636661
bool $flatten = false,
637662
string $userPassword = '',
638663
string $ownerPassword = '',
664+
array $embeds = [],
639665
array $assets = [],
640666
): ChromiumPdf {
641667
if ($singlePage) {
@@ -762,6 +788,10 @@ function hydrateChromiumPdfFormData(
762788
$chromium->encrypt($userPassword, $ownerPassword);
763789
}
764790

791+
if (count($embeds) > 0) {
792+
$chromium->embeds(...$embeds);
793+
}
794+
765795
if (count($assets) > 0) {
766796
$chromium->assets(...$assets);
767797
}
@@ -775,6 +805,7 @@ function hydrateChromiumPdfFormData(
775805
* @param int[] $failOnHttpStatusCodes
776806
* @param int[] $failOnResourceHttpStatusCodes
777807
* @param array<string,string|bool|float|int|array<string>> $metadata
808+
* @param Stream[] $embeds
778809
* @param Stream[] $assets
779810
*/
780811
function expectChromiumPdfOptions(
@@ -814,6 +845,7 @@ function expectChromiumPdfOptions(
814845
bool $flatten,
815846
string $userPassword,
816847
string $ownerPassword,
848+
array $embeds,
817849
array $assets,
818850
): void {
819851
expect($body)->unless($singlePage === false, fn ($body) => $body->toContainFormValue('singlePage', '1'));
@@ -919,6 +951,11 @@ function expectChromiumPdfOptions(
919951
expect($body)->unless($userPassword === '', fn ($body) => $body->toContainFormValue('userPassword', $userPassword));
920952
expect($body)->unless($userPassword === '', fn ($body) => $body->toContainFormValue('ownerPassword', $ownerPassword));
921953

954+
foreach ($embeds as $embed) {
955+
$embed->getStream()->rewind();
956+
expect($body)->toContainFormFile($embed->getFilename(), $embed->getStream()->getContents(), 'application/xml', 'embeds');
957+
}
958+
922959
if (count($assets) <= 0) {
923960
return;
924961
}

tests/Modules/LibreOfficeTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
/**
1414
* @param Stream[] $files
1515
* @param array<string,string|bool|float|int|array<string>> $metadata
16+
* @param Stream[] $embeds
1617
*/
1718
function (
1819
array $files,
@@ -47,6 +48,7 @@ function (
4748
bool $flatten = false,
4849
string $userPassword = '',
4950
string $ownerPassword = '',
51+
array $embeds = [],
5052
): void {
5153
$libreOffice = Gotenberg::libreOffice('');
5254

@@ -172,6 +174,10 @@ function (
172174
$libreOffice->encrypt($userPassword, $ownerPassword);
173175
}
174176

177+
if (count($embeds) > 0) {
178+
$libreOffice->embeds(...$embeds);
179+
}
180+
175181
$request = $libreOffice->convert(...$files);
176182
$body = sanitize($request->getBody()->getContents());
177183

@@ -228,6 +234,11 @@ function (
228234

229235
expect($body)->toContainFormFile($filename, $file->getStream()->getContents(), 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
230236
}
237+
238+
foreach ($embeds as $embed) {
239+
$embed->getStream()->rewind();
240+
expect($body)->toContainFormFile($embed->getFilename(), $embed->getStream()->getContents(), 'application/xml', 'embeds');
241+
}
231242
},
232243
)->with([
233244
[
@@ -271,5 +282,9 @@ function (
271282
true,
272283
'my_user_password',
273284
'my_owner_password',
285+
[
286+
Stream::string('my.xml', 'XML content'),
287+
Stream::string('my_second.xml', 'Second XML content'),
288+
],
274289
],
275290
]);

0 commit comments

Comments
 (0)