We are running Apache 2.2.16 and PHP 5.3.3 with DomPDF to generate PDFs. After generating a PDF, the subsequent request occasionally returns a plain text response consisting partially of the HTML used to generate the PDF (i.e. from the previous request), mixed with the HTTP headers and response of the requested page.
Apache is somehow mixing part of the response to the previous request with the new response. It seems like an output buffering issue, although I cannot replicate it locally using the same code. It also seems unrelated to gzip, as the problem still occurs with it disabled. I also tried in both Firefox and Chrome and experienced the same result.
UPDATE: If I turn keep-alive off, it solves the problem. However, I don’t necessarily want to leave keep-alive off. There is obviously still an underlying problem with the output buffer or response not being cleared.
Here are the request headers:
Request URL:https://asdf.com/checkout/5d86a9bdf455cdfd334494183559bf
Request Method:GET
Status Code:200 OK
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:APP=636c8e6b58e991476ed7d38849bde0d4; _
Host:asdf.com
Referer:https://asdf.com/get_pdf/33711
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.34 Safari/536.11
And the truncated response shown in the browser:
<!-- truncated HTML from the previous page (i.e. PDF) -->
</p>
</div>
</body>
</html>HTTP/1.1 200 OK
Date: Tue, 19 Jun 2012 18:06:13 GMT
Server: Apache
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 6457
Keep-Alive: timeout=7, max=96
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
‹í]ksÛ¸’ýìü
ŒfvmׯzP/?µëØNâÄI<‘’ìL*å‚HHBD
Z–gæ¿o7@I-BNrkë~ÐÜ;c‰<xtŸF£›lŠÇ¿œ¿;ëüq}Aj’ëÏ®.ÏHa¯TúT=+•Î;çä_vÞ\‘J±L:’F1W\D4,•.ÞHa Ôø°TšL&ÅIµ(d¿Ôy_ºÃ¾*Ø8ý¸§¬–Å@…Ö“c=àÝ(Œâ“ÝTLke4h=Ù:V\…¬õ{"‹Éßä2RLR_ñ[FN}_$‘âQÿ¸d`€1E v¾Ç¾%üö¤p& I¤ö:Ó1+ß|;)(v§J8ØñTÆL$ª··_ ¥ÖqìK>VDA“ù•ÞRs´@bé›ùÇ ýJïŠ}!ú!£c}1ÒÇJ!ïÆ¥¯ß&§¥J±™~,ŽxTüZÇ
<!-- followed by a bunch more gzipped gibberish -->
It sure sounds like a problem with the webserver, in part caused by the
keep-aliveconnection. Possible remedies:Content-Lengthheader with the PDF if you don’t already. Also, make sure theContent-Lengthis correct: i.e: there is no more output after that.KeepAlivein apache. Problem is this is cannot be done on a per-request or even per-directory basis as far as I know, so that would mean disabling if for you whole site (not recommended), or serve the pdf from a different virtual host.My preference definitely goes to the first option if that solves it, the second one is cumbersome.