Creating download link for a pdf

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adrian

    Creating download link for a pdf

    Using:
    Mysql 5.0.15
    php 5.1.6
    apache 2.2.3

    I am trying to create a download link for various pdfs which I have stored in a
    mysql DB.

    I am having trouble with the header fields when clicking the link.

    Here is the code that sends the pdf called using
    <a href="download. php?id=4">Blah</a>

    Output is delayed from php.ini (output_bufferi ng = 4096)

    What http headers should I be sending.

    This works most of the time like this, but in IE6 I get few strange results
    sometimes.

    1. Clicking on a new page after downloading the pdf - the page is blank unless I
    refresh
    2. The URL in the address bar does not update even though the page does.
    3. If I leave out the Content_Disposi tion header and set the Content-Type:
    application/pdf. The browser just displays the PDF as text.

    <?php
    include($_SERVE R["DOCUMENT_R OOT"] . "/../php/common.inc");
    include($_SERVE R["DOCUMENT_R OOT"] . "/../php/prepend.php");

    $db=new Interlex_DB;

    if(isset($_GET["id"]))
    {
    $query=sprintf( "SELECT pdf, filename, length(pdf) AS len FROM broadsheets
    WHERE broadsheet_id=' %d' AND active=1", $_GET["id"]);
    $db->query($query );
    if($db->next_record( ))
    {
    if(!headers_sen t())
    {
    header("Accept-Ranges: bytes");
    header("Content-Transfer-Encoding: Binary");
    header("Content-Type: application/force-download");
    header(sprintf( "Content-Length: %d", $db->f("len")));
    header(sprintf( "Content-Disposition: inline; filename=\"%s\" ",
    $db->f(filename)) );
    echo $db->f("pdf");
    //exit;
    }
    }
    }
    ?>

    --

    Adrian

    Think you know a language? Post to comp.lang... and find out!
  • Benjamin

    #2
    Re: Creating download link for a pdf

    I would set output buffering to just on because 4096 just buffers the
    first 4096 bytes; not always what you want. Also, if you're displaying
    a PDF, many browsers have built in plugins to handle pdfs. If the users
    wants to download it, then they can just use the save function in their
    browsers to download the pdf.
    Adrian wrote:
    Using:
    Mysql 5.0.15
    php 5.1.6
    apache 2.2.3
    >
    I am trying to create a download link for various pdfs which I have stored in a
    mysql DB.
    >
    I am having trouble with the header fields when clicking the link.
    >
    Here is the code that sends the pdf called using
    <a href="download. php?id=4">Blah</a>
    >
    Output is delayed from php.ini (output_bufferi ng = 4096)
    >
    What http headers should I be sending.
    >
    This works most of the time like this, but in IE6 I get few strange results
    sometimes.
    >
    1. Clicking on a new page after downloading the pdf - the page is blank unless I
    refresh
    2. The URL in the address bar does not update even though the page does.
    3. If I leave out the Content_Disposi tion header and set the Content-Type:
    application/pdf. The browser just displays the PDF as text.
    >
    <?php
    include($_SERVE R["DOCUMENT_R OOT"] . "/../php/common.inc");
    include($_SERVE R["DOCUMENT_R OOT"] . "/../php/prepend.php");
    >
    $db=new Interlex_DB;
    >
    if(isset($_GET["id"]))
    {
    $query=sprintf( "SELECT pdf, filename, length(pdf) AS len FROM broadsheets
    WHERE broadsheet_id=' %d' AND active=1", $_GET["id"]);
    $db->query($query );
    if($db->next_record( ))
    {
    if(!headers_sen t())
    {
    header("Accept-Ranges: bytes");
    header("Content-Transfer-Encoding: Binary");
    header("Content-Type: application/force-download");
    header(sprintf( "Content-Length: %d", $db->f("len")));
    header(sprintf( "Content-Disposition: inline; filename=\"%s\" ",
    $db->f(filename)) );
    echo $db->f("pdf");
    //exit;
    }
    }
    }
    ?>
    >
    --
    >
    Adrian
    >
    Think you know a language? Post to comp.lang... and find out!

    Comment

    Working...