Skip to content

Commit

Permalink
fix(downloadFrom): non-mandatory arguments Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
gulien committed Sep 23, 2024
1 parent 7a2c187 commit 529bf88
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |

Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down
14 changes: 9 additions & 5 deletions src/Modules/ChromiumPdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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());
Expand Down
14 changes: 9 additions & 5 deletions src/Modules/ChromiumScreenshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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());
Expand Down
7 changes: 2 additions & 5 deletions src/Modules/LibreOffice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
10 changes: 3 additions & 7 deletions src/Modules/PdfEngines.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
}
Expand All @@ -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());
Expand Down

0 comments on commit 529bf88

Please sign in to comment.