简体   繁体   中英

PDF Download gives corrupt PDF php

I am saving a pdf file, and then attempting to download it using php. The script seemed to work fine, but all of the sudden not anymore.

Can anybody see what I am missing?

PS: the file I am downloading is only 4.3kb big, so I assume that would be because it is not downloading at all. The actual file size should be bigger than this.

$pdf->output(ROOTDIR.'/modules/addons/statement_generator/reports/statement.pdf');
    if($action=='print'){
        $file_name = 'statement.pdf';
        $file_url = "http://".$_SERVER['SERVER_NAME']."/modules/addons/statement_generator/reports/" . $file_name;
        header('Content-Type: application/pdf');
        header("Content-Transfer-Encoding: Binary"); 
        header("Content-disposition: attachment; filename=\"".$file_name."\""); 
        readfile($file_url);
        exit;
    }

The $pdf->output() call will already send the PDF to the client. The file will not be saved to your local folder (Didn't you checked at least this?) because you have to pass "F" as the snd parameter.

After that you try to read from an URL (!!!!) that does not exists and which maybe return a nicely styled 404 html response. Two issues here:

  1. Why are you using http when you have the local path used some lines above? Use the local path only!
  2. The content returned by the URL is append to the already send PDF which ends in a document mixed of PDF and HTML (the 404 response) -> corrupted PDF

Conclusion: Use "F" as the 2nd parameter and use the same path for both writing and reading and not a mix of local path and URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM