diff --git a/README.md b/README.md index bf29c0c..be40372 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,9 @@ This package is a PHP client for [Gotenberg](https://gotenberg.dev), a developer tools like Chromium and LibreOffice for converting numerous document formats (HTML, Markdown, Word, Excel, etc.) into PDF files, and more! -| Gotenberg version | Client | -|-------------------|---------------------------------------------------------------------------------------------------| -| `8.x` | `v2.x` **(current)** | +| Gotenberg version | Client | +|---------------------|---------------------------------------------------------------------------------------------------| +| `8.x` **(current)** | `v2.x` **(current)** | | `7.x` | `v1.x` | | `6.x` | [thecodingmachine/gotenberg-php-client](https://github.com/thecodingmachine/gotenberg-php-client) | @@ -112,6 +112,7 @@ Gotenberg::chromium($apiUrl) If the route requires form files, use the `Stream` class to create them: ```php +use Gotenberg\DownloadFrom; use Gotenberg\Gotenberg; use Gotenberg\Stream; @@ -126,6 +127,13 @@ Gotenberg::chromium($apiUrl) // Or create your stream from scratch. Gotenberg::libreOffice($apiUrl) ->convert(new Stream('document.docx', $stream)); + +// Or even tell Gotenberg to download the files for you. +Gotenberg::libreOffice($apiUrl) + ->downloadFrom([ + new DownloadFrom('https://url.to.document.docx', ['MyHeader' => 'MyValue']) + ]) + ->convert(); ``` ## Send a request to the API diff --git a/src/Modules/ChromiumPdf.php b/src/Modules/ChromiumPdf.php index b3eacd0..48b7acf 100644 --- a/src/Modules/ChromiumPdf.php +++ b/src/Modules/ChromiumPdf.php @@ -201,9 +201,12 @@ public function url(string $url): RequestInterface * Note: it automatically sets the index filename to "index.html", as * required by Gotenberg. */ - public function html(Stream $index): RequestInterface + public function html(Stream|null $index): RequestInterface { - $this->formFile('index.html', $index->getStream()); + if ($index !== null) { + $this->formFile('index.html', $index->getStream()); + } + $this->endpoint = '/forms/chromium/convert/html'; return $this->request(); @@ -215,10 +218,11 @@ public function html(Stream $index): RequestInterface * Note: it automatically sets the index filename to "index.html", as * required by Gotenberg. */ - public function markdown(Stream $index, Stream $markdown, Stream ...$markdowns): RequestInterface + public function markdown(Stream|null $index, Stream ...$markdowns): RequestInterface { - $this->formFile('index.html', $index->getStream()); - $this->formFile($markdown->getFilename(), $markdown->getStream()); + if ($index !== null) { + $this->formFile('index.html', $index->getStream()); + } foreach ($markdowns as $markdown) { $this->formFile($markdown->getFilename(), $markdown->getStream()); diff --git a/src/Modules/ChromiumScreenshot.php b/src/Modules/ChromiumScreenshot.php index f55b918..296faf9 100644 --- a/src/Modules/ChromiumScreenshot.php +++ b/src/Modules/ChromiumScreenshot.php @@ -113,9 +113,12 @@ public function url(string $url): RequestInterface * Note: it automatically sets the index filename to "index.html", as * required by Gotenberg. */ - public function html(Stream $index): RequestInterface + public function html(Stream|null $index): RequestInterface { - $this->formFile('index.html', $index->getStream()); + if ($index !== null) { + $this->formFile('index.html', $index->getStream()); + } + $this->endpoint = '/forms/chromium/screenshot/html'; return $this->request(); @@ -127,10 +130,11 @@ public function html(Stream $index): RequestInterface * Note: it automatically sets the index filename to "index.html", as * required by Gotenberg. */ - public function markdown(Stream $index, Stream $markdown, Stream ...$markdowns): RequestInterface + public function markdown(Stream|null $index, Stream ...$markdowns): RequestInterface { - $this->formFile('index.html', $index->getStream()); - $this->formFile($markdown->getFilename(), $markdown->getStream()); + if ($index !== null) { + $this->formFile('index.html', $index->getStream()); + } foreach ($markdowns as $markdown) { $this->formFile($markdown->getFilename(), $markdown->getStream()); diff --git a/src/Modules/LibreOffice.php b/src/Modules/LibreOffice.php index e455a94..58700df 100644 --- a/src/Modules/LibreOffice.php +++ b/src/Modules/LibreOffice.php @@ -331,12 +331,9 @@ public function merge(): self * Note: if you requested a merge, the merging order is determined by the * order of the arguments. */ - public function convert(Stream $file, Stream ...$files): RequestInterface + public function convert(Stream ...$files): RequestInterface { - $index = $this->index ?? new HrtimeIndex(); - $filename = $this->merge ? $index->create() . '_' . $file->getFilename() : $file->getFilename(); - $this->formFile($filename, $file->getStream()); - + $index = $this->index ?? new HrtimeIndex(); foreach ($files as $file) { $filename = $this->merge ? $index->create() . '_' . $file->getFilename() : $file->getFilename(); $this->formFile($filename, $file->getStream()); diff --git a/src/Modules/PdfEngines.php b/src/Modules/PdfEngines.php index 8faafd2..8b79814 100644 --- a/src/Modules/PdfEngines.php +++ b/src/Modules/PdfEngines.php @@ -91,10 +91,9 @@ public function merge(Stream ...$pdfs): RequestInterface * Converts PDF(s) to a specific PDF/A format. * Gotenberg will return the PDF or a ZIP archive with the PDFs. */ - public function convert(string $pdfa, Stream $pdf, Stream ...$pdfs): RequestInterface + public function convert(string $pdfa, Stream ...$pdfs): RequestInterface { $this->pdfa($pdfa); - $this->formFile($pdf->getFilename(), $pdf->getStream()); foreach ($pdfs as $pdf) { $this->formFile($pdf->getFilename(), $pdf->getStream()); @@ -109,10 +108,8 @@ public function convert(string $pdfa, Stream $pdf, Stream ...$pdfs): RequestInte * Retrieves the metadata of specified PDFs, returning a JSON formatted * response with the structure filename => metadata. */ - public function readMetadata(Stream $pdf, Stream ...$pdfs): RequestInterface + public function readMetadata(Stream ...$pdfs): RequestInterface { - $this->formFile($pdf->getFilename(), $pdf->getStream()); - foreach ($pdfs as $pdf) { $this->formFile($pdf->getFilename(), $pdf->getStream()); } @@ -129,10 +126,9 @@ public function readMetadata(Stream $pdf, Stream ...$pdfs): RequestInterface * * @throws NativeFunctionErrored */ - public function writeMetadata(array $metadata, Stream $pdf, Stream ...$pdfs): RequestInterface + public function writeMetadata(array $metadata, Stream ...$pdfs): RequestInterface { $this->metadata($metadata); - $this->formFile($pdf->getFilename(), $pdf->getStream()); foreach ($pdfs as $pdf) { $this->formFile($pdf->getFilename(), $pdf->getStream());