Home » Htaccess » A better way to use PDF files online

by 9 comments

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.

if(!isset($_GET['file']))die('LOGGED! no file specified');


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');

    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename="'.$file_name.'"');
    header("Content-length: $file_size");

    while(!feof($fp)) {
        $file_buffer = fread($fp, 2048);
        echo $file_buffer;

} else {
    die('LOGGED! bad file '.$file_path);

Final Working Example


Comments Welcome

Information is freedom. Freedom is non-negotiable. So please feel free to modify, copy, republish, sell, or use anything on this site in any way at any time ;)

My Online Tools

Popular Articles
Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two.
-- Richard M. Stallman

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


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. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | |

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service | @Htaccess

↑ TOPMain