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,
Related
I would like to display an execl file within my browser similar to how a PDF file is viewed.Is there any way to accomplish this. I have already used an execl library, but this does not fulfil my requirements.
Here is example I would like to used in excel but its not working
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
I also used PHPExcel library for viewing execl file on browser but its not working
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'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>
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.