in reply to Opening PDF through CGI

You might also need a Content-Length header, since otherwise the browser wouldn't know when to stop reading. So try throwing in a length($buffer).