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

Update README.md #546

Merged
merged 3 commits into from
May 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,71 @@ The dompdf wrapper dispatches events to conveniently get the inner dompdf instan

See [Symfony Events and Event Listeners](https://symfony.com/doc/current/event_dispatcher.html) for more info.

#### Dom PDF use

Use listeners if you want to hook to DomPDF page_script and/or callbacks.

Simple implementation with twig rendering, add page number to the footer with `page_script`:

DomPDF documentation: [Page Script](https://github.com/dompdf/dompdf/wiki/Usage#page_script)
```php
namespace App\EventListener;

use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

final class DomPdfListener {
#[AsEventListener(event: 'dompdf.stream')]
public function onDompdfOutput($event): void
{
// Get DOMPDF instance
$domPdf = $event->getPdf();
// Get canvas instance
$canvas = $domPdf->getCanvas();

$canvas->page_script(function ($pageNumber, $pageCount, $canvas, $fontMetrics) {
$pageWidth = $canvas->get_width();
$pageHeight = $canvas->get_height();

// page number
$text = "Page $pageNumber of $pageCount";
$font = $fontMetrics->getFont('Helvetica');
$size = 10;
$width = $fontMetrics->getTextWidth($text, $font, $size);
$canvas->text($pageWidth - $width - 20, $pageHeight - 20, $text, $font, $size);
});
}
```
`callback` example to add a watermark (can be achieved with `page_script` as well):
DomPDF documentation: [Callbacks](https://github.com/dompdf/dompdf/wiki/Usage#callbacks)
```php
[...]
$domPdf = $event->getPdf();
$canvas = $domPdf->getCanvas();

$domPdf->setCallbacks([
[
'event' => 'begin_page_render',
'f' => function ($frame, $canvas, $fontMetrics) {
// watermark
$text = 'WaterMak text';
$font = $fontMetrics->getFont('Helvetica', 'bold');
$size = 48;
$width = $fontMetrics->getTextWidth($text, $font, $size);
$x = ($canvas->get_width() - $width) / 2;
$y = ($canvas->get_height() - $size) / 2;
$canvas->page_text($x, $y, $text, $font, $size, [0.5, 0.5, 0.5, "alpha" => 0.4], 20, 0, -45);
},
],
]);
```
There are 6 events available, see [documentation](https://github.com/dompdf/dompdf/wiki/Usage#callbacks) for more details:
- `begin_page_reflow`
- `begin_frame`
- `end_frame`
- `begin_page_render`
- `end_page_render`
- `end_document`

## License

This bundle is under the [MIT license](LICENSE.md).
Expand Down
Loading