Skip to content

Instantly share code, notes, and snippets.

@Gompali
Last active February 19, 2020 15:32
Show Gist options
  • Save Gompali/d53b84b0abfcaa9f25a2c80f9d6a59ba to your computer and use it in GitHub Desktop.
Save Gompali/d53b84b0abfcaa9f25a2c80f9d6a59ba to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace App\Entity\EventListener;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
class ResponseListener
{
/**
* @var PdfConverterInterface
*/
private $converter;
public function __construct(PdfConverterInterface $converter)
{
$this->converter = $converter;
}
public function onKernelResponse(ResponseEvent $event)
{
$response = $event->getResponse();
if ('application/pdf' === $response->headers->get('accept')) {
$content = $response->getContent();
return new BinaryFileResponse(
$this->converter->toPdfStream($content)
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment