You can. The easiest way is to utilize the buffer feature in PHP to capture the buffer to a variable, then write the contents of the variable to a file. Something like the following:
<?php
ob_start();
// Do some file creation stuff in here....
// Put the buffer into a string:
$html = ob_get_flush();
// Write the string to a file:
$fp = fopen('myfile.html', 'w');
if(false === (fwrite($fp, $html)))
echo 'Unable to write to file (myfile.html)';
fclose($fp);
?>
Another way is very similar. You can just take all the "echo" and "print" statements, and assign them to a variable (like $html or $output) and at the end, write the string to a file (like above).
Hope that helps you out.