Downloading PDF files

  • 13 years ago
    I'm a newbie in PHP and need some help

    Just need some help on the codes for downloading PDF files using PHP...

    Thanks in advance....





  • 13 years ago

    Hi there!

     What do you mean by downloading PDF files? Do you mean actually creating them? Or fetching them from a URL and downloading them?

  • 13 years ago

    Yup!! fetching them from a URL then downloading them...

     I've got some codes for downloading but it is not working properly. It downloads the file but once you open the downloaded file, the file has no content at all.

     

     

     

  • 13 years ago

    this is code for pdf downloading

     

    header('Content-type: application/pdf);
    header("Content-Disposition: attachment; filename=Abc.pdf");

     

     

    if u want to download dynamically then you need to do  use pdf function to create file nad data fetching pdf function

    Bagesh Singh

    www.bageshsingh.com

     

     

  • 13 years ago

    this is code for pdf downloading

     

    header('Content-type: application/pdf);
    header("Content-Disposition: attachment; filename=Abc.pdf");

     

     

    if u want to download dynamically then you need to do  use pdf function to create file nad data fetching pdf function

    Bagesh Singh

    www.bageshsingh.com

     

  • 13 years ago

    Hi there,

                I got the answer or the codes in my posted Downloading PDF files, but i have a problem with it. Can you help me regarding this thing, this is my problem, when i'm going to open the downloaded file, the popup menu say that the file is corrupted, what was wrong in  my code?

    here are the codes:

    $fullpath = $filename

    if ($fd = fopen ($fullPath, "r"))
            {
                $fsize = filesize($fullPath);
                $path_parts = pathinfo($fullPath);
                header("Content-type: application/pdf");
                header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
                header("Pragma: public");
                header("Expires: 0");
                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                header("Cache-Control: private",false);
                header("Content-length: $fsize");
                header("Content-Transfer-Encoding: binary");
                if (is_readable($filename)==true)
                {
                    while(!feof($fd))
                    {
                    $buffer = fread($fd, 2048);
                    echo $buffer;
                    }
                }
            }
            fclose ($fd);
            exit;

     Thanks in advance...

  • 13 years ago

    Try replacing  fopen ($fullPath, "r") with  fopen ($fullPath, "rb"))

    That is, open the file in binary mode.

    To be honest, it's really not necessary to have such a long script. The "if (is_readable()...)" part seems odd too. If the file wasn't readable, surely fopen($fullPath,"r") would fail, and you can use fpassthru($fd) to replace the whole while() loop (since all you want to do is dump the file to the output buffer). In fact, try this (on the basis that the fewer moving parts the better). I'm assuming that you're making sure the filename is proper and valid before you get to this stage (you are, right?)

    if (is_readable($filename)) {
      header("Content-type: application/pdf");
      header("Content-Transfer-Encoding: binary");
      header("Content-length: $fsize");
      header("Content-Disposition: attachment; filename=\"".basename($filename)."\"');
      readfile($filename);
      exit;
    } else {
      error_log("User failed to download file {$filename}");
    }

  • 13 years ago

    The other obvious bug is that there's a missing ; at the end of the first line :)

     Drag the corrupted file into a text editor and read the top - there's probably a PHP error there and that's what's corrupting the download.
     

  • 13 years ago

    For those person who reply for my message THANK YOU very much for your concern, and I hope you may help many people, I've got the answer to my problem, even thought some comments are not applicable to my work i'm very thankful because you give me some idea how to work on it.

     

    Thank you again... 

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If Java had true garbage collection, most programs would delete themselves upon execution.” - Robert Sewell