PHP not displaying PDF file in Google Chrome - php

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"));

Related

Displaying private PDF in iframe

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,

PHP - Forcing an MP3 file download

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

PDF file won't display

So, I've followed a lot of posts here on StackOverflow still can't work out why my code doesn't work.
I want to be able to open a .pdf file outside the web root, so I've tried this code:
<?php
$file = '/user/Desktop/exemplo.pdf';
echo $file.'<br>';
$filename = 'test.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
#readfile($file);
?>
Problem is I always get the same error:
pdf this file cannot be correctly displayed
I've decided to try several browsers: firefox, chrome, opera and finally internet explorer which suprinsingly said something the others didn't:
file does not start with '%pdf-'Local\EWH-696-6
So does someone know what I need to configure in my code to open pdf's? Or to have them start with the code above (hardcoding it didn't fix it).
EDIT: tried removing the echo and still had the same error.

How to display PDF from another server

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>

content-type causes chrome to call function twice

Most peculiar problem with following code. It returns a pdf report to the browser.
function cart_aspdf() {
trace('cart_aspdf_in');
$file = 'order_WS000250.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $file . '"');
$file = APPPATH.'pdfcache/'.$file;
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
trace('cart_aspdf_readfile');
#readfile($file);
trace('cart_aspdf_out');
}
The trace output in opera,firefox,ie,safari is as you would expect:
cart_aspdf_in
cart_aspdf_readfile
cart_aspdf_out
BUT the trace for chrome shows the following which seems to indicate that the function is being called at least twice if not three times. Why should this be so?
cart_aspdf_in
cart_aspdf_readfile
cart_aspdf_out
cart_aspdf_in
cart_aspdf_readfile
cart_aspdf_in
cart_aspdf_readfile
cart_aspdf_out
The problem does not occur if I omit the content-type line but then chrome shows the raw pdf data which is no use
I ran into the same problem.
header('Content-Disposition: inline;');
For whatever reason, when the content-disposition is inline it calls the page twice.
This was giving me problems trying to use the referrer because the second call does not pass referrer data.
using
header('Content-Disposition: attachment;');
only runs once, but will not display inside the browsers PDF viewer. It will instead download the file.
I think this needs to be posted on chrome's bugtracker. This is quite annoying and for streaming files a bandwidth waste.

Categories