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

  command, php, symfony4, twig

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.


public function streamAction()
    $process = new Process(["pwd"]);

    $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;


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