-
-
Notifications
You must be signed in to change notification settings - Fork 94
Proposal to generate an invoice with Gotenberg #382
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
base: 2.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,6 +173,7 @@ private function addPdfGeneratorSection(ArrayNodeDefinition $node): void | |
| ->children() | ||
| ->booleanNode('enabled')->defaultTrue() | ||
| ->end() | ||
| ->booleanNode('generate_with_gotenberg')->defaultFalse() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about adding a |
||
| ->end() | ||
| ->end() | ||
| ; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| { | ||
|
|
@@ -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')); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| $definition->setDecoratedService('sylius_invoicing.generator.twig_to_pdf'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
| 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(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,7 +113,7 @@ | |
| <table class="layout"> | ||
| <tr> | ||
| <td> | ||
| <img width="160" src="{{ logoPath }}"> | ||
| <img width="160" src="{{ generateWithGotenberg ? gotenberg_asset(logoPath) : logoPath }}"> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inject the config key |
||
| </td> | ||
| <td class="text-right"> | ||
| {% block header %}{% endblock %} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest / require-dev ?