Show a PDF files in users browser via PHP/Perl


Php Problem Overview

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.

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.

What I wanted for is show the users my PDF files, not creating or download the PDF.

Here is what I got form the official PHP documentation:

header('Content-type: application/pdf');

Also my google-search-result perl code:

open(PDF, "the.pdf") or die "could not open PDF [$!]";
binmode PDF;
my $output = do { local $/; <PDF> };
close (PDF);
print "Content-Type: application/pdf\n";
print "Content-Length: " .length($output) . "\n\n";
print $output

if you do it on ruby, please say it to me. But I'm not sure if my server support rails.

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.

Lets assume that the users have the Adobe Reader plug-in. So, how to fix my problem?

edit : I want to show plain PDF file. My primary purpose: track my pdf files and use some fancy urls.

edit : Here's my main php code:

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');

edit : 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:

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

edit : All my problems solved. Here's the final code:

$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="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');


Thanks! :)

Php Solutions

Solution 1 - Php

I assume you want the PDF to display in the browser, rather than forcing a download. If that is the case, try setting the Content-Disposition header with a value of inline.

Also remember that this will also be affected by browser settings - some browsers may be configured to always download PDF files or open them in a different application (e.g. Adobe Reader)

Solution 2 - Php

	$url ="https://yourFile.pdf";
	$content = file_get_contents($url);

	header('Content-Type: application/pdf');
	header('Content-Length: ' . strlen($content));
	header('Content-Disposition: inline; filename="YourFileName.pdf"');
	header('Cache-Control: private, max-age=0, must-revalidate');
	header('Pragma: public');


Tested and works fine. If you want the file to download instead, replace

    Content-Disposition: inline


    Content-Disposition: attachment

Solution 3 - Php

You could modify a PDF renderer such as xpdf or evince to render into a graphics image on your server, and then deliver the image to the user. This is how Google's quick view of PDF files works, they render it locally, then deliver images to the user. No downloaded PDF file, and the source is pretty well obscured. :)

Solution 4 - Php

The safest way to have a PDF display instead of download seems to be embedding it using an object or iframe element. There are also 3rd party solutions like Google's PDF viewer.

See Best Way to Embed PDF in HTML for an overview.

There's also DoPDF, a Java based In-browser PDF viewer. I can't speak to its quality but it looks interesting.

Solution 5 - Php

You can also use fpdf class available at: It gives options for both outputting to a file and displaying on browser.

Solution 6 - Php

There is a simple solution using the embed tag:

<span class="fileShow">
  <a href="aa.pdf" onclick="event.stopPropagation();" target="_blank">
    <embed style="width:450px; height:300px; max-width:450px; max-height:300px" src="aa.pdf">


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondimassonyView Question on Stackoverflow
Solution 1 - PhpStephenView Answer on Stackoverflow
Solution 2 - PhpKareemView Answer on Stackoverflow
Solution 3 - PhpsarnoldView Answer on Stackoverflow
Solution 4 - PhpPekkaView Answer on Stackoverflow
Solution 5 - PhptapananandView Answer on Stackoverflow
Solution 6 - PhpPavel Jiri StrnadView Answer on Stackoverflow