Home » Php » php – Force Downloading a PDF file, corrupt file

php – Force Downloading a PDF file, corrupt file

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’ve got a problem that has risen many times on SO, but I can’t seem to find the solution to mine! I’m trying to deliver a pdf file to the client without it opening in the browser, the file downloads but it is corrupt when I open it and is missing quite a few bytes from the original file. I’ve tried several such methods for downloading the file but I’ll just show you the latest I’ve used and hopefully get some feedback.

I have also opened the downloaded PDF in a text editor and there are no php errors at the top of it that I can see!

I’m also aware that readfile() is much quicker but for testing purposes I am desperate to get anything working so I used the while(!feof()) approach!

Anyway enough rambling, heres the code (taken from why my downloaded file is alwayes damaged or corrupted?):

$file     = __DIR__ . '/reports/somepdf.pdf';
$basename = basename($file);
$length   = sprintf("%u", filesize($file));

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $basename . '"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length);

ob_clean();
set_time_limit(0);
readfile($file);

Also to note was the difference in file size:

Original: 351,873 bytes
Downloaded: 329,163 bytes
How to&Answers:

Make sure you’re not running any compression output buffering handlers, such as ob_gzhandler. I had a similar case and I had to disable output buffering for this to work properly

Answer:

You are using the the ob_gzhandler on the output buffer.

It works by gzencoding chunks of output. The output then is a stream of the encoded chunks.

Each chunk needs to get some bytes to get encoded, so the output is a little bit buffered until enough bytes are available.

However at the end of your script you discard the remaining buffer instead of flushing it.

Use ob_end_flush() instead of ob_clean() and the file gets through fully and not corrupted.

You are to use the transfer encoding of ob_gzhandler with file-uploads not having any problems when you don’t destroy the output-buffer before it could have done it’s work.

This is also the same if any other output buffering that works chunked would have been
enabled.

Example code:

$file     = __DIR__ . '/somepdf.pdf';
$basename = basename($file);
$length   = sprintf("%u", filesize($file));

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $basename . '"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length);

ob_end_flush();   // <--- instead of ob_clean()

set_time_limit(0);
readfile($file);

return;

(FYI: actually even the ob_end_flush(); is not necessary, the important part is not to just kick the output-buffer before it could have done it’s work)

Answer:

I fought with using content-disposition for pushing a PDF download for two days before finding a solution to my problem. My PDF files were also smaller in size and corrupt – however, I could open them in Windows Preview – just not Adobe. After much troubleshooting, I discovered that Adobe expects the %PDF in the first 1024 bytes of the file. I was doing all my file type checks in my php code before creating the headers. I took out the majority of code before the headers and my PDF file was fixed.

You might not be setting it up the same way I did, but it might be the same problem:

http://helpx.adobe.com/acrobat/kb/pdf-error-1015-11001-update.html