Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"php": "^8.2",
"knplabs/knp-snappy-bundle": "^1.10",
"ramsey/uuid": "^4.7",
"sensiolabs/gotenberg-bundle": "^1.0",

Choose a reason for hiding this comment

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

suggest / require-dev ?

"sylius/grid-bundle": "^1.13",
"sylius/resource-bundle": "^1.12",
"sylius/sylius": "^2.0",
Expand Down
1 change: 1 addition & 0 deletions config/services/generators.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<argument type="service" id="sylius_invoicing.generator.invoice_file_name" />
<argument>@SyliusInvoicingPlugin/shared/download/pdf.html.twig</argument>
<argument>%sylius_invoicing.template.logo_file%</argument>
<argument>%sylius_invoicing.pdf_generator.generate_with_gotenberg%</argument>
</service>
<service id="Sylius\InvoicingPlugin\Generator\InvoicePdfFileGeneratorInterface" alias="sylius_invoicing.generator.invoice_pdf_file" />

Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ private function addPdfGeneratorSection(ArrayNodeDefinition $node): void
->children()
->booleanNode('enabled')->defaultTrue()
->end()
->booleanNode('generate_with_gotenberg')->defaultFalse()

Choose a reason for hiding this comment

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

What about adding a service property to set the PDF generator to use? It could be Wkhtmltopdf by default to avoid BC break and Gotenberg if the bundle is installed.

->end()
->end()
;
Expand Down
12 changes: 12 additions & 0 deletions src/DependencyInjection/SyliusInvoicingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

use Sylius\Bundle\CoreBundle\DependencyInjection\PrependDoctrineMigrationsTrait;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Extension\AbstractResourceExtension;
use Sylius\InvoicingPlugin\Generator\TwigToGotenbergPdfGenerator;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;

final class SyliusInvoicingExtension extends AbstractResourceExtension implements PrependExtensionInterface
{
Expand All @@ -35,6 +38,15 @@ public function load(array $configs, ContainerBuilder $container): void

$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('sylius_invoicing.pdf_generator.allowed_files', $config['pdf_generator']['allowed_files']);
$container->setParameter('sylius_invoicing.pdf_generator.generate_with_gotenberg', $config['pdf_generator']['generate_with_gotenberg']);

if ($config['pdf_generator']['generate_with_gotenberg'] === true) {
$definition = new Definition(TwigToGotenbergPdfGenerator::class);
$definition->addArgument(new Reference('sensiolabs_gotenberg'));

Choose a reason for hiding this comment

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

We should check if the bundle is installed and throw an exception if necessary.
IMO the bundle should be optional.

$definition->setDecoratedService('sylius_invoicing.generator.twig_to_pdf');

Choose a reason for hiding this comment

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

Is decoration needed since we just replace the service?


$container->setDefinition('sylius_invoicing.gotenberg_generator.twig_to_pdf', $definition);
}
}

public function prepend(ContainerBuilder $container): void
Expand Down
2 changes: 2 additions & 0 deletions src/Generator/InvoicePdfFileGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct(
private readonly InvoiceFileNameGeneratorInterface $invoiceFileNameGenerator,
private readonly string $template,
private readonly string $invoiceLogoPath,
private readonly bool $generateWithGotenberg,
) {
}

Expand All @@ -38,6 +39,7 @@ public function generate(InvoiceInterface $invoice): InvoicePdf
'invoice' => $invoice,
'channel' => $invoice->channel(),
'invoiceLogoPath' => $this->fileLocator->locate($this->invoiceLogoPath),
'generateWithGotenberg' => $this->generateWithGotenberg,
],
);

Expand Down
50 changes: 50 additions & 0 deletions src/Generator/TwigToGotenbergPdfGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\InvoicingPlugin\Generator;

use Sensiolabs\GotenbergBundle\GotenbergInterface;
use Sensiolabs\GotenbergBundle\Processor\ProcessorInterface;

final readonly class TwigToGotenbergPdfGenerator implements TwigToPdfGeneratorInterface
{
public function __construct(
private GotenbergInterface $gotenberg
) {
}

public function generate(string $templateName, array $templateParams): string
{
$builder = $this->gotenberg->pdf()
->html()
->content($templateName, $templateParams)
->processor(
new class implements ProcessorInterface {
public function __invoke(?string $fileName): \Generator
{
$file = '';

do {
$chunk = yield;
$file .= $chunk->getContent();
} while (!$chunk->isLast());

return $file;
}
}
)
;

return $builder->generate()->process();
}
}
2 changes: 1 addition & 1 deletion templates/shared/download/pdf_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
<table class="layout">
<tr>
<td>
<img width="160" src="{{ logoPath }}">
<img width="160" src="{{ generateWithGotenberg ? gotenberg_asset(logoPath) : logoPath }}">

Choose a reason for hiding this comment

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

inject the config key $config['pdf_generator']['generate_with_gotenberg'] to wrap this logic in an if maybe ?

</td>
<td class="text-right">
{% block header %}{% endblock %}
Expand Down
Loading