try using
Content-Type: application/pdf
and include TWO
's.
Also, you might want to try generating the file first, THEN in a
separate step, returning the filename to the web browser. I am
not familiar enough with htmldoc but I am wary of the way you are
specifying the file name. Maybe more items are showing up in
the text returned to the client than the $filename?