I am rendering a TWIG template to generate a CSV file to download. In order to show a progress bar for the download, the server must return the Content-Length header.
I tried rendering the TWIG template into a variable, calculating the length of this string, then outputting the content-length header immediately before echo'ing the rendered template:
$output = $twig->render(...);
header('Content-Length', strlen($output));
echo $output;
But this throws a server 500 error with the message "malformed header from script 'index.php': Bad header: Content-Length".
Am I missing something here? Seems this should be trivial.