-1

I have a pdf file called newdesign.pdf, which I am serving up with the code below. It's fine on my own machine, but when I go on the server, the problem is that instead of opening a PDF file, the browser just shows up all the garbled PDF source text.

This is my code:

header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Length: ' . filesize("newdesign.pdf"));
// to open in browser
header('Content-Disposition: inline; filename=' . basename("newdesign.pdf"));
readfile("newdesign.pdf");

And this is the beginning of the output:

Warning: Cannot modify header information - headers already sent by (output started at /home/zeejfl5/public_html/ui_test/savedesign.php:8) in /home/zeejfl5/public_html/ui_test/savedesign.php on line 71
%PDF-1.4 1 0 obj << /Title (��) /Creator (��) /Producer (��wkhtmltopdf) /CreationDate (D:20120903012618-07'00') >> endobj 3 0 obj << /Type /ExtGState /SA true /SM 0.02 /ca 1.0 /CA 1.0 /AIS false /SMask /None>> endobj 4 0 obj [/Pattern /DeviceRGB] endobj
 8 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 5 0 obj << /Type /Page /Parent 2 0 R /Contents 9 0 R /Resources 11 0 R /Annots 12 0 R /MediaBox [0 0 454 227] >> 
endobj 11 0 obj << /ColorSpace << /PCSp 4 0 R /CSp /DeviceRGB /CSpg /DeviceGray >> /ExtGState << /GSa 3 0 R >> /Pattern << >> /Font << /F6 6 0 R /F7 7 0 R >> /XObject << >> >> endobj 12 0 obj [ ] endobj 9 0 obj << /Length 10 0 R /Filter /FlateDecode 
>> stream x��[M��0��W̹`E}YPzhh=B=�J�۲$K�=��wl'Y9�d74�F��$��4�����������t��v:J*���[5,Vb k13�m۵�

My PDF file itself is fine, if you open using Adobe Reader. But how to open it in the browser for the user's viewing? I don't think there's anything wrong in the code because it works fine on my local machine. It only acts up when I upload the script to the server.

user961627
  • 12,379
  • 42
  • 136
  • 210
  • possible duplicate of ["Header Already Sent" error in PHP](http://stackoverflow.com/questions/8028957/header-already-sent-error-in-php) – deceze Sep 03 '12 at 08:34
  • But I haven't sent any headers or echo before the lines I've displayed. The only PHP code before that is file i/o and shell commands. The PDF file *is* showing up too, just garbled source. – user961627 Sep 03 '12 at 08:48
  • 4
    You are clearly getting the warning *Cannot modify header information - headers already sent*, which is the source of your problem, which means the linked question/answer applies and will fix your problem. Read it in its entirety and double check your code. – deceze Sep 03 '12 at 08:50
  • you were right... i ended up deleting all comments and extra new lines and now it works. – user961627 Sep 03 '12 at 09:01
  • [There you go.](https://i.chzbgr.com/completestore/12/9/3/R5Uw4vWgGkaBhaQPlKZb7g2.jpg) – deceze Sep 03 '12 at 09:03

2 Answers2

1

check you php file if there isn't an echo before your headers are sent. If any data is sent to the browser before the headers, then the headers are useless.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
0

PHP renders this error message because there is at least 1 character echoed to the page before the PDF file. In your case, you haven't explicitly echo-ed something to the page, but the PHP engine has.

There is a difference between how PHP is set up locally and on your server.
It appears as if your server has a stricter error_reporting level defined in its php.ini file.

(More on this subject: http://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors)

You can resolve this just to see it working by completely disabling the PHP warnings:
<?php ini_set('display_errors', 0); ?>
But this is not a long-term solution.

To fix this once and for all, inspect lines 8 and 71 of /home/zeejfl5/public_html/ui_test/savedesign.php to determine where the PHP engine is throwing that warning.

cassi.lup
  • 1,261
  • 7
  • 23