Symfony: Streamed response of external command to the browser to display real time progress of the command

I’m generating a large PDF with 2000 pages in symfony (4.2) framework. What I’m doing is just save the HTML content to the .HTML file by getting content from the twig.

Then I’m using the headless chrome to generate the PDF from the URL using the below command.

/usr/bin/google-chrome --headless --disable-gpu --run-all-compositor-stages-before-draw --print-to-pdf [URL of HTML file] --virtual-time-budget=10000

Now, the requirement is while the above command is running I have to display the loader with the progress bar in the front.

What I did is as below to get the stream response and display them on the browser.

Controller

public function streamAction()
{
    $process = new Process(["pwd"]);
    $process->run();

    $output = new StreamedOutputService(fopen('php://stdout', 'w'));

    $response = new StreamedResponse(function() use ($output, $process) {
        while ($process->isRunning()) {
            $output->writeln('123 n');
        }
    });
    $response->headers->set('X-Accel-Buffering', 'no');

    return $response;
}

Streamed Response Class

protected function doWrite($message, $newline)
{
    if (
        false === @fwrite($this->getStream(), $message) ||
        (
            $newline &&
            (false === @fwrite($this->getStream(), PHP_EOL))
        )
    ) {
        throw new RuntimeException('Unable to write output.');
    }

    echo $message;

    ob_flush();
    flush();
}

So can you guys please suggest to me that how can I run the above command from the controller and display the loader with the LIVE (Realtime progress of the command) progress bar in twig?

Thanks in advance.

Source: Symfony4 Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *