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;
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,
PDF file shown symbols on live server while fetched from database, but displayed correctly on local sever. I don’t know exactly what I am doing wrong.
Below is my code
<?php
if(ISSET($_REQUEST['file'])){
$file = $_REQUEST['file'];
// Add header to load pdf file
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' .$file. '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
// #readfile($file);
#readfile("files/".$file);
}
?>
Here is a sample displayed symbol
I am trying to download a file. I am using IE11. I have tried several methods to do this. Currently I am trying to use the header with Content-Disposition method. I have tried to do this a few different ways according to other answers people have given. And it does download. But instead of downloading the file I point it to, it downloads the file it is written in. So if I tell it to download example.txt in my test.php file. It will only download test.php.
These are the methods I have tried:
This one is written in test.html:
<?php
$filename = "example.txt"
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
?>
I've also tried making it a button:
BUTTON
Where download.php is:
<?php
$file = $_GET['file'];
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename="'.$file.'"');
?>
I tried this:
<?php
header('Content-Type: application/download');
header('Content-Disposition: attachment; filename="example.txt"');
header("Content-Length: " . filesize("example.txt"));
$fp = fopen("example.txt", "r");
fpassthru($fp);
fclose($fp);
?>
And this:
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($File));
header("Connection: close");
There are many more slight variations and mix and matching that I have tried. All have the same problem that the .html or .php file downloads rather than example.txt. Does anyone know why this would happen? Is something not supported in IE11? I do not think it is a syntax error simply because most of these I copied from other answers online. I have tried with example.txt existing and not existing, in this folder and other folders.
EDIT: So it turns out that these all work, I was just using them wrong. I had been trying to make isolated files to run this code so I could test it without interference from the rest of the functions on my website, but this left the php files without the resources they needed to actually run properly. When I put the code into the actual files on the website it worked perfectly. smh
Try this :
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($File));
header('Content-Disposition: attachment; filename=' . basename($File));
readfile($File);
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 know this question has been asked, but the solutions offered are not working for me. I am able to download PDFs, but Adobe Reader is telling me it's either corrupt or decoded improperly. Does anyone have an alternative solution, or possible fix?
$path = "http://www.laerdalmail.com/discoversimulation/downloads/needs_assessment_form.pdf";
header('Content-Type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=needs_assessment_form.pdf");
readfile($path);
exit;
readfile should be given a filesystem path, not a URL. something like this:
$path = '/websitefolder/discoversimulation/downloads/needs_assessment_form.pdff';
// or
$parh = 'c:\something\needs_assessment_form.pdff';
Try adding in content length too:
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($path));