Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc/processor #141

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class YourController
### Advanced Usage

1. [Configuration](./docs/configuration.md)
2. [Processing (saving for example)](./docs/processing.md)
2. [Working with assets](./docs/assets.md)
3. [Builders API](./docs/builders_api.md)
4. [Async & Webhooks](./docs/webhook.md)
Expand Down
11 changes: 10 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ The default configuration for the bundle looks like :

Assuming you have the following client configured.

```yaml
<details>
<summary>app/config/framework.yaml</summary>

```yaml
# app/config/framework.yaml

framework:
Expand All @@ -19,8 +21,13 @@ framework:
base_uri: 'http://localhost:3000'
```

</details>

Then

<details>
<summary>app/config/sensiolabs_gotenberg.yaml</summary>

```yaml
# app/config/sensiolabs_gotenberg.yaml

Expand Down Expand Up @@ -1237,6 +1244,8 @@ sensiolabs_gotenberg:
# - { name: 'X-Custom-Header', value: 'custom-header-value' }
```

</details>

> [!TIP]
> For more information about the [PDF properties](https://gotenberg.dev/docs/routes#page-properties-chromium)
> or [screenshot properties](https://gotenberg.dev/docs/routes#screenshots-route).
Expand Down
80 changes: 80 additions & 0 deletions docs/processing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Processing

Let's say you want to save the PDF or Screenshot as a file, you will need to use a `Sensiolabs\GotenbergBundle\Processor\ProcessorInterface`.
To avoid loading the whole file content in memory you can stream it to the browser.

You can also hook on the stream and save the file chunk by chunk. To do so we leverage the [`->stream`](https://symfony.com/doc/current/http_client.html#streaming-responses) method from the HttpClientInterface and use a powerful feature from PHP : [`->send`](https://www.php.net/manual/en/generator.send.php).
Neirda24 marked this conversation as resolved.
Show resolved Hide resolved

## Using FileProcessor

Useful if you want to store the file.
Neirda24 marked this conversation as resolved.
Show resolved Hide resolved
Example when generating a PDF :
```php
use Sensiolabs\GotenbergBundle\GotenbergPdfInterface;
use Sensiolabs\GotenbergBundle\Processor\FileProcessor;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Response;

#[Route(path: '/my-pdf', name: 'my_pdf')]
public function pdf(GotenbergPdfInterface $gotenbergPdf): Response
{
return $gotenbergPdf->html()
//
->fileName('my_pdf')
->processor(new FileProcessor(
new Filesystem(),
$this->getParameter('kernel.project_dir').'/var/pdf',
Neirda24 marked this conversation as resolved.
Show resolved Hide resolved
))
->generate()
->stream()
;
}
```

This will save the file under `%kernel.project_dir%/var/pdf/my_pdf.pdf` once the file has been fully streamed to the browser.
If you are not streaming to a browser you can still process the file this way :
Neirda24 marked this conversation as resolved.
Show resolved Hide resolved

```php
use Sensiolabs\GotenbergBundle\GotenbergPdfInterface;
use Sensiolabs\GotenbergBundle\Processor\FileProcessor;
use Symfony\Component\Filesystem\Filesystem;

class SomeService
{
public function __construct(private readonly GotenbergPdfInterface $gotenbergPdf) {}

public function pdf(): \SplFileInfo
{
return $this->gotenbergPdf->html()
//
->fileName('my_pdf')
->processor(new FileProcessor(
new Filesystem(),
$this->getParameter('kernel.project_dir').'/var/pdf',
))
->generate()
->process()
;
}
}
```

This will return a SplFileInfo of the generated file store at `%kernel.project_dir%/var/pdf/my_pdf.pdf`.
Neirda24 marked this conversation as resolved.
Show resolved Hide resolved

## Other processors

* `Sensiolabs\GotenbergBundle\Processor\ChainProcessor` : Apply multiple processor. Each chunk will be sent to each processor sequentially.
Neirda24 marked this conversation as resolved.
Show resolved Hide resolved
* `Sensiolabs\GotenbergBundle\Processor\NullProcessor` : Empty processor. Does nothing.

## Custom processor

A custom processor must implement `Sensiolabs\GotenbergBundle\Processor\ProcessorInterface` which require that your `__invoke` method is a `\Generator`. To receive a chunk you must assign `yield` to a variable like so : `$chunk = yield`.

The basic needed code is the following :

```php
do {
$chunk = yield;
// do something with it
} while (!$chunk->isLast());
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also explain that the returned value of the processor will be returned by the process method

StevenRenaux marked this conversation as resolved.
Show resolved Hide resolved
Loading