Return get_header and get_footer string instead of echo it

Use an output buffer!

ob_start();
get_header();
$header = ob_get_contents();
ob_end_clean();

The header is now in $header and you can do with it as you please.