A better way to use PDF files online

FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Give visitors the choice to open or save a pdf using .htaccess and phpNovember 26th, 2007

« The Latest and Greatest php.iniTips on Writing WordPress Plugins »

Adobe ReaderOne of the most annoying things on the Internet for me is when I click on a link to an Adobe PDF file. For me this is annoying to the extreme because the PDF file is openened directly in your browser because of the Adobe PDF Plugin that almost all browsers have installed. If the PDF is multi-page or a larger file my browser takes forever to load and render the PDF and sometimes even freezes!

The solution I came up with is really nice, and will work for all .pdf files on your entire site, without having to change any links. It is a secure and robust solution to my pet peeve. Feel free to add any ideas or improvements using the comment form below.

Serving PDF files for Visitor Satisfaction

For me an ideal solution would be to offer me choices, such as giving me the option to:

  1. Open in an external program that I can choose, such as Acrobat
  2. Save the file on my computer without having to open it.

PDF Before and after Fix – Firefox

PDF Output Broken FirefoxPDF Plugin Not Used Firefox

PDF Before and after Fix – Internet Explorer

PDF Output Broken Internet ExplorerPDF Plugin Not Used Fix Internet Explorer

Because serving PDF files with the above options would make me happy as a visitor, I want to offer those options to visitors on my site. Here is how I do it.

.htaccess pdf rewrite example

This .htaccess mod_rewrite code goes in your sites root .htaccess file and uses a RewriteCond to make sure that the file exists, it then rewrites the request internally to the /cgi-bin/pdf.php file while adding the PDF filename as a http GET variable called file.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+)\.pdf$  /cgi-bin/pdf.php?file=$1 [L,NC,QSA]

php pdf output example

This php code is simple yet sophisticated, it simply locates the pdf file requested using the GET variable called file and then sends the pdf file directly to the clients browser, adding some HTTP headers to make a built-in browser dialog window pop-up giving the client the option to open the pdf in an external program or to save the pdf on the clients computer.

<?php
if(!isset($_GET['file']))die('LOGGED! no file specified');
 
$file_path=$_SERVER['DOCUMENT_ROOT'].'/'.strip_tags(htmlentities($_GET['file'])).'.pdf';
 
if ($fp = fopen ($file_path, "r")) {
    $file_info = pathinfo($file_path);
    $file_name = $file_info["basename"];
    $file_size = filesize($file_path);
    $file_extension = strtolower($file_info["extension"]);
 
    if($file_extension!='pdf') {
        die('LOGGED! bad extension');
    }

    ob_start();
    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename="'.$file_name.'"');
    header("Content-length: $file_size");
    ob_end_flush();
 
    while(!feof($fp)) {
        $file_buffer = fread($fp, 2048);
        echo $file_buffer;
    }

    fclose($fp);
    exit;
    exit();
} else {
    die('LOGGED! bad file '.$file_path);
}
?>

Final Working Example

« The Latest and Greatest php.iniTips on Writing WordPress Plugins »

Reader Comments

  1. Matt says:

    Sounds good, but, um, I don’t see the file…

  2. Saieditor says:

    I like this solution; but where does this pdf file go?

  3. askapache.com commenter Elaine says:

    It doesn’t seem to work quite right if you right-click on the link & save as…which is something I do a lot of the time, if I have a hunch a link is a PDF. Any thoughts? (Because I LOVE the general idea!)

  4. askapache.com commenter Mark Harris says:

    One of the most annoying things on the Internet for me is when I click on a link to a website and it plays music at me. Especially when it’s one of a number of pages I’m opening in a background and I have to go hunting for it. If you’re talking about giving users choice on how to consume content, you might think about your own site.

    Otherwise, good article

  5. askapache.com commenter Steven Clark says:

    Interesting article, thanks for sharing.

    I do find the loud intrusive music on this page to be somewhat as irritating as the PDF issue though… sorry.

  6. Emil says:

    Not to be a pain in the ass (I really appreciate the article) but reading the starting words “One of the most annoying things on the internet for me…” while a song you never asked to hear is blasting out of your speakers late at night is sort of ironic. And a teeny tiny flash player (set to autoplay) that took me ages to find (”What tab did that? And where on the page?”) and shut off. Autoplaying sounds is a big usability no-no, as far as I and most people I know are concerned. Sorry if I sound grumpy, I was just not ready to rock. :)

  7. two questions really:

    - what’s wrong with simply using one of the standard methods that have been out there for ages to force download?

    <Files *.pdf>
    ForceType application/octet-stream
    Header set Content-Disposition attachment
    </Files>

    or just

    AddType application/octet-stream .pdf

    - if it’s just something that annoys you as an end user, why not simply change it at your end? open up your standalone adobe reader, preferences, internet and untick the “Display PDF in browser” checkbox. for every user hates the default PDF behaviour there is probably another that actually really likes it and would hate this solution.

  8. Andy Towler says:

    Why does your site play a song (The Final Countdown I think) without me asking it to? Your article talks about visitor satisfaction – I think you should consider whether this kind of thing is likely to lead to increased satisfaction or intense irritation.

RSS feed for comments on this post.TrackBack URL

Add Your Opinion

Skip to Comments
210

It's very simple -
you read the protocol
and write the code.
-Bill Joy

HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C | WAI | DISA | ICSI | GIAC | SANS RR | GHOST

Authority: 110

↑ TOP

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed according to these terms. "Apache" is a trademark of The ASF.

Site Map | Contact Webmaster | Glossary | License and Disclaimer | Terms of Service