Jump to content

PDF file not correctly encoded when downloading a pdf file from php


Cyjm1120

Recommended Posts

I have searched for the solution for so long and I still could not solve my problem. I have a localhost server where it stored some pdf files for download. 


I implemented the following function to force the download:



if(isset($_POST['dlPDF']))
{

$file = $_SERVER['DOCUMENT_ROOT'] .'/Upload/'.$pdfName;

header("Content-Type: application/pdf");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$pdfName");
header("Content-Transfer-Encoding: binary");

// read the file from disk
readfile($file);
}

However I could not open the pdf file properly since it says "the pdf file is not correctly encoded". FYI, the downloaded pdf file is always a few Kb larger than the original file.


Please kindly let me know if there is anything I can do to make the pdf readable and I appreciate your help.


Link to comment
Share on other sites

i would open the downloaded pdf file using your programming editor to see what it has extra in it (look at both the start and end of the content in the file.) you either have php error message(s) or some text content from your download code file.

 

also, where are you setting $pdfName at in your code?

 

and, AFAIK, the filename='...' attribute value in the attachment; header should have quotes around the actual file name to get all browser types to recognize the file name.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.