diff --git a/spec/Generator/TwigToPdfGeneratorSpec.php b/spec/Generator/TwigToPdfGeneratorSpec.php index 52efaeba..0cc42c3e 100644 --- a/spec/Generator/TwigToPdfGeneratorSpec.php +++ b/spec/Generator/TwigToPdfGeneratorSpec.php @@ -30,6 +30,7 @@ function let( $twig, $pdfGenerator, $fileLocator, + ['allow' => 'allowed_file_in_knp_snappy_config.png'], ['swans.png'] ); } @@ -52,7 +53,7 @@ function it_generates_pdf_from_twig_template( $pdfGenerator ->getOutputFromHtml( 'I am a pdf file generated from twig template', - ['allow' => ['located-path/swans.png']] + ['allow' => ['allowed_file_in_knp_snappy_config.png', 'located-path/swans.png']] ) ->willReturn('PDF FILE') ; diff --git a/src/Generator/TwigToPdfGenerator.php b/src/Generator/TwigToPdfGenerator.php index 7f1733dc..fbab2a12 100644 --- a/src/Generator/TwigToPdfGenerator.php +++ b/src/Generator/TwigToPdfGenerator.php @@ -23,6 +23,7 @@ public function __construct( private Environment $twig, private GeneratorInterface $pdfGenerator, private FileLocatorInterface $fileLocator, + private array $knpSnappyOptions, private array $allowedFiles ) { } @@ -37,12 +38,23 @@ public function generate(string $templateName, array $templateParams): string private function getOptions(): array { + $options = $this->knpSnappyOptions; + if (empty($this->allowedFiles)) { - return []; + return $options; + } + + if (!isset($options['allow'])) { + $options['allow'] = []; + } elseif (!is_array($options['allow'])) { + $options['allow'] = [$options['allow']]; } - return [ - 'allow' => array_map(fn ($file) => $this->fileLocator->locate($file), $this->allowedFiles), - ]; + $options['allow'] = array_merge( + $options['allow'], + array_map(fn ($file) => $this->fileLocator->locate($file), $this->allowedFiles) + ); + + return $options; } } diff --git a/src/Resources/config/services/generators.xml b/src/Resources/config/services/generators.xml index 26dbe4ae..8420175d 100644 --- a/src/Resources/config/services/generators.xml +++ b/src/Resources/config/services/generators.xml @@ -54,6 +54,7 @@ + %knp_snappy.pdf.options% %sylius_invoicing.pdf_generator.allowed_files%