Show a PDF files in users browser via PHP/Perl

Show a PDF files in users browser via PHP/Perl

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Serving PDFs via CGI: Headers, Tracking, and Browser Behavior

You need to serve a PDF file directly to a user's browser without triggering a download dialog, specifically because you are using a CGI wrapper to track clicks and cloak the original file path. This is a common requirement for analytics-heavy applications where the physical storage location of an asset must remain hidden from the client. The core mechanism relies entirely on HTTP headers. If your server correctly identifies the content and instructs the browser on how to handle it, Adobe Reader (or any compatible PDF viewer) will render the document inline within the page.

The fundamental approach involves sending the application/pdf MIME type followed by instructions on how to display the file. A basic PHP implementation looks like this:

<?php
$file = './path/to/the.pdf';
$filename = 'Custom file name for the.pdf';

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');

readfile($file);
?>

In Perl, the logic remains identical, though the syntax differs due to the language's handling of filehandles and string output. You must open the file in binary mode, read the entire contents into a variable, and then print it after setting the appropriate headers. Failing to set Content-Length can cause issues with progressive loading bars in modern PDF viewers like Adobe Reader X, as the client needs to know the total size of the payload before rendering begins.

A critical distinction exists between attachment and inline. If you set Content-Disposition: attachment, browsers will force a download dialog. Setting it to inline tells the browser that the content is safe to render within the current context, provided the user has a plugin installed. However, simply setting the MIME type and disposition often results in the progress bar not appearing on the first load if the server does not explicitly state the file size. Adding header('Content-Length: ' . filesize($file)); resolves this by giving the browser the data it needs to update its UI elements immediately.

Another important consideration is caching and partial content support. By including header('Accept-Ranges: bytes');, you enable range requests. This allows a user's browser or an intermediate proxy to request only a specific byte range of the PDF file if it has already cached a portion of it previously. While your primary goal might be tracking clicks rather than optimizing bandwidth, this header is a best practice for serving static files and ensures compatibility with modern caching strategies.

Your use case—tracking clicks via a CGI layer—suggests you are likely rewriting the URL or proxying requests before the file reaches the origin server. This pattern effectively cloaks the real path of the.pdf while maintaining analytics integrity at the application level. Ensure that your tracking logic executes before calling readfile() or echoing the Perl output, as sending headers requires an empty response buffer until that point.

If you are building a larger application where routing and file serving are central to your architecture, consider frameworks like Laravel. The laravelcompany.com documentation provides extensive guides on handling file uploads and serving assets securely, which can be adapted for this specific CGI-proxy scenario if you decide to move away from raw PHP/Perl scripts in favor of a structured framework.

Our network