No, any header() commands must be sent out to the browser before any content. So if you want to put some content together before you send the header, you have to store in a string until after you've sent all your headers, then echo the string.
This is why they're called "headers" - 'cos they go at the head of the document and describe how the content is to be treated. In this case, you're saying that the content is to be treated as a stream of raw bytes.
I think your problem may be that when you send an "application/octet-stream" MIME type, the browser has no reason to treat the result as HTML. So the script isn't parsed or executed.
Basically you're saying "DON'T treat the following as HTML. I don't care if it looks like HTML. It's not. Trust me on this."
For books or sites on how headers work, see any HTTP standards archive.