Skip to content

Commit

Permalink
Merge 3cfd574 into bfd9c1c
Browse files Browse the repository at this point in the history
  • Loading branch information
reyostallenberg committed Jul 2, 2018
2 parents bfd9c1c + 3cfd574 commit 40b8b53
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -66,6 +66,14 @@ core23_dompdf:
...
```

### Events

The dompdf wrapper dispatches events to convenient get the inner dompdf instance when creating the pdf.
- `dompdf.output` is dispatched in getPdf
- `dompdf.stream` is dispatched in streamHtml

See [Symfony event dispatcher documentation](https://symfony.com/doc/current/event_dispatcher.html) for more info.

## License

This bundle is under the [MIT license](LICENSE.md).
Expand Down
18 changes: 18 additions & 0 deletions src/Events.php
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\DompdfBundle;

final class Events
{
const OUTPUT = 'dompdf.output';
const STREAM = 'dompdf.stream';
}
3 changes: 3 additions & 0 deletions src/Resources/config/services.xml
Expand Up @@ -5,6 +5,9 @@
<service alias="Core23\DompdfBundle\Factory\DompdfFactory" id="dompdf.factory" public="true"/>
<service id="Core23\DompdfBundle\Wrapper\DompdfWrapper" class="Core23\DompdfBundle\Wrapper\DompdfWrapper" public="true">
<argument type="service" id="dompdf.factory"/>
<call method="setEventDispatcher">
<argument type="service" id="event_dispatcher"/>
</call>
</service>
<service id="Core23\DompdfBundle\Factory\DompdfFactory" class="Core23\DompdfBundle\Factory\DompdfFactory" public="true">
<argument>%core23_dompdf.options%</argument>
Expand Down
29 changes: 29 additions & 0 deletions src/Wrapper/DompdfWrapper.php
Expand Up @@ -11,7 +11,10 @@

namespace Core23\DompdfBundle\Wrapper;

use Core23\DompdfBundle\Events;
use Core23\DompdfBundle\Factory\DompdfFactoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

final class DompdfWrapper implements DompdfWrapperInterface
{
Expand All @@ -20,6 +23,11 @@ final class DompdfWrapper implements DompdfWrapperInterface
*/
private $dompdfFactory;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

/**
* @param DompdfFactoryInterface $dompdfFactory
*/
Expand All @@ -28,6 +36,16 @@ public function __construct(DompdfFactoryInterface $dompdfFactory)
$this->dompdfFactory = $dompdfFactory;
}

/**
* Set the event dispatcher.
*
* @param EventDispatcherInterface $eventDispatcher
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
{
$this->eventDispatcher = $eventDispatcher;
}

/**
* {@inheritdoc}
*/
Expand All @@ -36,6 +54,12 @@ public function streamHtml(string $html, string $filename, array $options = []):
$pdf = $this->dompdfFactory->create($options);
$pdf->loadHtml($html);
$pdf->render();

if ($this->eventDispatcher instanceof EventDispatcherInterface) {
$event = new GenericEvent($pdf, ['filename' => $filename, 'html' => $html]);
$this->eventDispatcher->dispatch(Events::STREAM, $event);
}

$pdf->stream($filename);
}

Expand All @@ -48,6 +72,11 @@ public function getPdf(string $html, array $options = []): string
$pdf->loadHtml($html);
$pdf->render();

if ($this->eventDispatcher instanceof EventDispatcherInterface) {
$event = new GenericEvent($pdf, ['html' => $html]);
$this->eventDispatcher->dispatch(Events::OUTPUT, $event);
}

return $pdf->output();
}
}

0 comments on commit 40b8b53

Please sign in to comment.