title: Show a PDF files in users browser via PHP/Perl
body: <p>I want to show my users PDF files. The reason why I use CGI to show the PDF is I want to track the clicks for the PDF, and cloak the real location of the saved PDF.</p>
  <p>I've been searching on the Internet and only found how to show save dialog to the users and creating a PDF, not show the files to the users.</p>
  <p>What I wanted for is show the users my PDF files, not creating or download the PDF.</p>
  <p>Here is what I got form the official PHP documentation:</p>
  <pre><code>&lt;?php
  header('Content-type: application/pdf');
  readfile('the.pdf');
  ?&gt;
  </code></pre>
  <p>Also my google-search-result perl code:</p>
  <pre><code>open(PDF, &quot;the.pdf&quot;) or die &quot;could not open PDF [$!]&quot;;
  binmode PDF;
  my $output = do { local $/; &lt;PDF&gt; };
  close (PDF);
   
  print &quot;Content-Type: application/pdf\n&quot;;
  print &quot;Content-Length: &quot; .length($output) . &quot;\n\n&quot;;
  print $output
  </code></pre>
  <p>if you do it on ruby, please say it to me. But I'm not sure if my server support rails.</p>
  <p>Sorry if my code is too far away from the method to show the pdf, since I don't know anything about pdf processing and how to implement this problem.</p>
  <p>Lets assume that the users have the Adobe Reader plug-in. So, how to fix my problem?</p>
  <p><strong>edit</strong> : I want to show plain PDF file. My primary purpose: track my pdf files and use some fancy urls.</p>
  <p><strong>edit</strong> : Here's my main php code:</p>
  <pre><code>&lt;?php
  $file='/files/the.pdf';
  header('Content-type: application/pdf');
  header('Content-Disposition: inline; filename=&quot;the.pdf&quot;');
  @readfile($file);
  ?&gt;
  </code></pre>
  <p><strong>edit</strong> : Now the code is working. But the loading progress bar (on Adobe Reader X plugin) doesn't shows up. Why? Anyone can help me? Here's my main code:</p>
  <pre><code>&lt;?php
  $file='./files/the.pdf';
  header('Content-type: application/pdf');
  header('Content-Disposition: inline; filename=&quot;the.pdf&quot;');
  header('Content-Transfer-Encoding: binary');
  header('Content-Length: ' . filesize($file));
  @readfile($file);
  ?&gt;
  </code></pre>
  <p><strong>edit</strong> : All my problems solved. Here's the final code:</p>
  <pre><code>&lt;?php
  $file = './path/to/the.pdf';
  $filename = 'Custom file name for the.pdf'; /* Note: Always use .pdf at the end. */
  
  header('Content-type: application/pdf');
  header('Content-Disposition: inline; filename=&quot;' . $filename . '&quot;');
  header('Content-Transfer-Encoding: binary');
  header('Content-Length: ' . filesize($file));
  header('Accept-Ranges: bytes');
  
  @readfile($file);
  ?&gt;
  </code></pre>
  <p>Thanks! :)</p>