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:
<?php
header('Content-type: application/pdf');
readfile('the.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:
<?php
$file='/files/the.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
@readfile($file);
?>
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:
<?php
$file='./files/the.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
@readfile($file);
?>
edit : All my problems solved. Here's the final code:
<?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="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
@readfile($file);
?>
Thanks! :)
PHP uses a standard code to display the pdf file in web browser.
At the top right, click More Settings. At the bottom, click Show advanced settings. Under “Privacy”, click Content settings. Under “PDF Documents," check the box next to "Open PDF files in the default PDF viewer application.” (Uncheck this box if you want PDFs to open automatically when you click them.)
php $file = 'dummy. pdf'; $filename = 'dummy. pdf'; header('Content-type: application/pdf'); header('Content-Disposition: inline; filename="' . $filename .
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)
$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');
ini_set('zlib.output_compression','0');
die($content);
Tested and works fine. If you want the file to download instead, replace
Content-Disposition: inline
with
Content-Disposition: attachment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With