Closed Jean-Beru closed 2 months ago
#[AsController] final class PdfController { #[Route(path: '/pdf', name: 'pdf', methods: ['GET'])] public function __invoke( GotenbergPdfInterface $gotenberg, Filesystem $filesystem, #[Autowire(env: 'resolve:PDF_DIR')] string $directory, LoggerInterface $logger, ): Response { return $gotenberg->html() ->header('pdf/header.html.twig') ->footer('pdf/footer.html.twig') ->content('pdf/invoice/pdf.html.twig', ['name' => 'Hubert']) ->landscape() ->fileName('my_pdf.pdf') ->processor( new ChainProcessor([ new FileProcessor($filesystem, $directory, $logger), new DebugProcessor($logger), ]), ) ->generate() ->stream() ; } }
<?php declare(strict_types=1); namespace App\Command; use Sensiolabs\GotenbergBundle\GotenbergPdfInterface; use Sensiolabs\GotenbergBundle\Processor\FileProcessor; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Filesystem\Filesystem; #[AsCommand( name: 'app:generate:invoice', description: 'Generates an invoice in PDF', )] class GenerateInvoiceCommand extends Command { public function __construct( private readonly GotenbergPdfInterface $pdf, private readonly Filesystem $filesystem, #[Autowire(env: 'resolve:PDF_DIR')] private readonly string $directory, ) { parent::__construct(); } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); // Intro $intro = $this->pdf->html() ->content('pdf/invoice/pdf.html.twig', ['name' => 'Hubert']) ->processor(new FileProcessor($this->filesystem, $this->directory)) ->generate() ->process() ; $io->info(sprintf('PDF dumped to "%s".', $intro->getRealPath())); // Content $content = $this->pdf->html() ->content('pdf/invoice/pdf.html.twig', ['name' => 'Hubert']) ->processor(new FileProcessor($this->filesystem, $this->directory)) ->generate() ->process() ; $io->info(sprintf('PDF dumped to "%s".', $content->getRealPath())); // Merge $merge = $this->pdf->merge() ->fileName('merge.pdf') ->files((string) $intro, (string) $content) ->processor(new FileProcessor($this->filesystem, $this->directory)) ->generate() ->process() ; $io->info(sprintf('PDF dumped to "%s".', $merge->getRealPath())); // Cleanup $this->filesystem->remove([$intro, $content]); $io->info('Intermediate PDF removed.'); $io->success('PDF generated successfully.'); return Command::SUCCESS; } }
The FilesystemProcessor partially fix https://github.com/sensiolabs/GotenbergBundle/issues/26.
Usage
In a controller
In a command
Issue
The FilesystemProcessor partially fix https://github.com/sensiolabs/GotenbergBundle/issues/26.