Use output buffering.
Before you render anything out to the browser do
ob_start();
after you do your rendering get the html content.
$html = ob_get_contents();
then to send it to the browser do.
ob_flush();
Then with the $html you can save to file, database or whatever works for your use case