Skip to content
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

feat: improve response by using a stream #90

Merged
merged 9 commits into from
Jul 18, 2024
Merged

feat: improve response by using a stream #90

merged 9 commits into from
Jul 18, 2024

Conversation

Jean-Beru
Copy link
Contributor

@Jean-Beru Jean-Beru commented Jul 3, 2024

Usage

In a controller

#[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()
        ;
    }
}

In a command

<?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;
    }
}

Issue

The FilesystemProcessor partially fix #26.

src/Processor/CallbackProcessor.php Outdated Show resolved Hide resolved
src/Builder/GotenbergResult.php Outdated Show resolved Hide resolved
src/Builder/GotenbergResult.php Outdated Show resolved Hide resolved
src/Builder/GotenbergResult.php Outdated Show resolved Hide resolved
src/Client/GotenbergResponse.php Show resolved Hide resolved
src/Client/GotenbergResponse.php Show resolved Hide resolved
src/Client/GotenbergResponse.php Outdated Show resolved Hide resolved
src/Processor/CallbackProcessor.php Outdated Show resolved Hide resolved
src/Processor/FileProcessor.php Outdated Show resolved Hide resolved
src/Processor/FileProcessor.php Outdated Show resolved Hide resolved
src/Processor/CallbackProcessor.php Outdated Show resolved Hide resolved
src/Processor/ChainProcessor.php Show resolved Hide resolved
@Jean-Beru Jean-Beru force-pushed the feat/use-stream branch 4 times, most recently from aca2c1b to 49a9e60 Compare July 8, 2024 14:05
@Jean-Beru Jean-Beru marked this pull request as ready for review July 9, 2024 09:13
@Jean-Beru Jean-Beru force-pushed the feat/use-stream branch 3 times, most recently from 7f8aafa to fada934 Compare July 12, 2024 10:38
@Jean-Beru Jean-Beru merged commit cb17959 into main Jul 18, 2024
7 checks passed
@Jean-Beru Jean-Beru deleted the feat/use-stream branch July 18, 2024 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create a "saveTo" mechanism
3 participants