I'm using the below script to display the PDF in browser.
$vPath = '/path/to/pdf/example.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $vPath . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($vPath));
header('Accept-Ranges: bytes');
#readfile($vPath);
The PDF files are on the same server where the script is.
But now I have to move the PDF to image server(Only PDF and Image files resides here)
How can I display the PDF from another server? Please help
Thanks guys.
I did a work around. The problem with using the above code is the relative path used. Instead I used IFRAME for displaying PDF using absolute path.
$vPath = 'http://path/to/pdf/example.pdf';
<iframe src="<?php echo $vPath;?>" style="width:600px; height:500px;" frameborder="0"></iframe>
Related
I would like to display PDF's from a non-public directory in an iframe. I have found a lot of descriptions about this problem using headers, but always ended up with the PDF filling the full page, meanwhile I would need it to be embedded into the page, e.g. besides a table inside a div.
E.g. this solution displays the PDF, but only the PDF, instead of embedding it into the application.
$file='../private/the.pdf';
$filename = 'Custom.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);
echo '<iframe src="Custom.pdf" width="500px"></iframe>'`
Is it, in any way, possible to access a private PDF from an iframe?
Thank you and sorry if this was already asked before, I read quite a few issues but none of them treated embedding.
Thx,
So, I need a little help here. I have a site which hosts some mp3s. When users click on the download url, it links directly to a file called downloadmp3.php, which goes 2 parameters in the url...the php file is included below, and it's basically supposed to FORCE the user to save the mp3. (not play it in the browser or anything).
That doesnt happen. Instead, it seems like the file is WRITTEN out in ascii to the browser. It seems like it's the actual mp3 file written out.
Here is my downloadmp3.php file...please, what's wrong in this code.
It works on my local LAMP (Bitnami Wampstack on windows)....that is, on my local testing environment, it sends the file to my broswer, and I can save it. When I upload it to the real server, it basically writes out the mp3 file.
Here is the culprit file, downloadmp3.php...please help
<?php
include 'ngp.php';
$file = $_GET['songurl'];
$songid = $_GET['songid'];
increasedownloadcount($songid);
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: audio/mpeg');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
ob_clean();
flush();
readfile($file);
exit;
}
?>
By the way, this site only hosts mp3s - no other audio or file format. So, this downloadmp3.php script should ideally ask the user where they want to save this file.
Thanks for your help in advance.
I think the filename should be in quotes:
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
Change the content-type value to text/plain. With this browser wont recognize it and wont play the file. Instead it will download the file at clients machine.
Seems there is too many headers. I am sure they do SOMETHING... but this code works.
This code works with MP3 files.... downloads to a file. Plays without a problem.
if(isset($_GET['file'])){
$file = $_GET['file'];
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename=".$file.'"');
readfile('path/to/your/'.$file);
exit();
}
You can access it with ajax call, or this:
<a id="dl_link" href="download.php?file=<>file-you-wish-to-download<>" target="_blank">Download this file</a>
Hopefully this is of some use
hi im currently working on a project that entails displaying od pdf files in the browser. So basically i store my pdf files under public/uploads/docs and try to display the pdf using my current code below. Unfortunately, it is always saying 'failed to load pdf' as you can see i also dumped the address i should be going to so here it is
string(59) "uploads/docs/2016-05-23-22-05-57-oth-Laravel Cheatsheet.pdf"
<?php
$pdf = 'uploads/docs/' . $list->filename;
dd($pdfurl);
header('Content-type: application/pdf');
// header('Content-Disposition: inline; filename="' . $pdf . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($pdf));
header('Accept-Ranges: bytes');
#readfile($pdf);
exit;
?>
any ideas what i am doing wrong? thanks in advance!
i just fixed my code and here is the working one :)
header('Content-type: application/pdf');
ob_clean();
flush();
readfile($pdfurl);
exit;
I wanted to display PDF files in browser using this 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);
?>
The code works well in Firefox, but not in Chrome. I haven't tested in any other browser. Is it a bug with Chrome? Is there anyway I could make it so it is displayed in Firefox and Chrome?
Just change the content disposition to attachment
header('Content-Disposition: attachment;; filename="' . $filename . '"');
This will not display the PDF in the browser, but in Chrome it seems to work.
It depends on your browser setting and not on the code. Usually by default firefox will open the pdf in browser whereas it is not in the case of chrome
I've had the same problem before, but not all computers had the issue. In my case I had some setting using Adobe or the default Chrome PDF reader. Check your settings.
Is $file just the path to the file without the file name itself? Is that case you should use instead:
header('Content-Length: ' . filesize("$file/$filename"));
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! :)
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
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. :)
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.
You can also use fpdf class available at: http://www.fpdf.org.
It gives options for both outputting to a file and displaying on browser.
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">
</a>
</span>