James
James

Reputation: 21

PHP serving pdf files works at first, but begins to fail over time?

My friend and I are serving pdf files among our group via php on a link, off of a shared host. Basically, the php file doing the serving is just setting the doc type and name, and then doing a readfile on the pdf file, nothing to complicated.

At any rate, it worked great for a long time, but over time it's like the files decay or something. One at a time, they start having the following adobe reader error: "There was an error opening this document. The file is damaged and could not be repaired"

This is true of any computer downloading the file (even first time downloading to that computer, no cache issues on the client side). The source file on the server is still in perfect condition, opens just fine, and can be copied via FTP and open on the same computer that was having issues with the downloaded version.

So, what's the deal? Is it possible the host is caching a version of the pdf that's corrupt and not releasing it? Any idea what on earth would cause this?

Thank you, James

Upvotes: 2

Views: 602

Answers (3)

jClark
jClark

Reputation: 128

I ran into this same problem -- some files opened normally, some had graphical glitches, and a few wouldn't open at all.

At the top of the PHP file I was using to serve the files, I included my site's main config file. When I removed it and included only what was necessary (path & database info), the PDFs began to serve normally.

Long story short, ensure any unnecessary code in the PHP file is removed.

Upvotes: 0

Igor
Igor

Reputation: 2659

Just download the corrupted PDF, open it with text editor like notepad++ and check that there is nothing prepended/appended to file like html or spaces.

It should start with somthing like that

%PDF-1.4
%âãÏÓ
1 0 obj
<</DecodeParms

And ends with %%EOF

<</Info 6 0 R/Root 5 0 R/Size 7/ID [<2dc4e4e34299742156136c9f3e72d3db><1b914aa93d42277e939b341233d3e66b>]>>
startxref
3540
%%EOF

No spaces, stange chars after %%EOF. Hope it helps!

Upvotes: 3

powtac
powtac

Reputation: 41080

Adobe PDF Reader (as browser plugin) has a suspect way of caching files from the same URL, even if the content has changed.

Try to add a dummy timestamp or random number to the URL, so Reader is forced to load the file again.

Example:

 example.com/invoiceDownload.php?rand=123123123123

Upvotes: 2

Related Questions