force pdf download corrupt file problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

force pdf download corrupt file problem

Post by lettie_dude »

Hi

I know there are various posts on this on the forum but most are dated pre 2005 and don't really give a definate solution. Since time has moved on I wonder if anyone has come up with one.

Trying to force a pdf to be opened or downloaded rather than opening in a browser window. However adobe keeps telling me the file is not recognised or corrupted and if I save it there is 0bytes showing.

Code I am using is as below and I am trying to get this to work in IE7.

Code: Select all

$filename = $row['fromdatabase'];
$filepath = "/folder/".$row['fromdatabase'];
                        
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/pdf");
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath));
readfile($filename);
exit();
I am also trying to get the pop-up details window to close afterwards and noticed this was also an unresolved forum post as well so any help with this also appreciated.

Cheers
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: force pdf download corrupt file problem

Post by koen.h »

Code: Select all

$file = "export_exhibitors.csv";
 
                header("Content-Description: File Transfer");
                header("Content-Type: application/force-download");
                header("Content-Disposition: attachment; filename=".basename($file));
                header("Content-Length: ".filesize($file));
                @readfile($file);
 
This code from this thread http://www.flashfocus.nl/forum/showthread.php?t=19012 once save me with a similar problem. I only needed to adapted it to my needs and it worked.
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: force pdf download corrupt file problem

Post by lettie_dude »

Hi

Tried the above but to no avail. Adobe Professional gives the following error message:

Acrobat could not open '102[1].pdf' because it is either not a supported file type or becasuse the file has been damaged(for example, it was sent as an email attachment and wasn't correctly decoded).

Also note the [1] added to the file name. This may be an acrobat setting as it does not appear if you click save but as I said it saves it with no data 0B.

Could this be anything to do with the file being held in a folder?

Anyone else any ideas!
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: force pdf download corrupt file problem

Post by lettie_dude »

Fixed it!

It only works if you include the full path to the file i.e. where it sits on the server. Once this is included it works a treat.

Just need to work out how to close the pop-up window after instigating the download.

Any thoughts anyone?
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

window close after download

Post by lettie_dude »

Ok found some code for this

Code: Select all

var gWindowCloseWait = 1;
 
function SetupWindowClose()
{
   window.setTimeout("window.close()",gWindowCloseWait*1000);
}
Which is called onclick. Pdf launches great, window closes after small delay great. Does not work on IE6 (theres a surprise!) Any suggestions?
Post Reply