I would like to display a PDF file as a result of browsing to the following address:
http://www.mydomain.com/myfile
To that end I am using a 301 Redirect on .htaccess which redirects myfile to myfile.php. This file then contains
<?php
$filename = 'files/somefile.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="somefile.pdf"');
header('Content-Length: ' . filesize($filename));
#readfile($filename);
?>
The main idea is that the PDF is displayed in the browser without prompting the user for download, but that when forcing the download the output filename becomes somefile.pdf. Everything is working smoothly in IE, Firefox, Chrome, Safari and Opera. However, in Android nothing happens. The PDF is not displayed and it doesn't prompt for download. How can I get this to work with Android? Is there anything I need to change in the script?
Android native browser doesn't have PDF support. You will need to embed the your url with google doc. Try this one..hope it will help..
url="http://docs.google.com/gview?embedded=true&url=http://118.102.182.50/victor/testpdf.aspx?s=9";
webView.loadUrl(url);
Related
I am working on a generate PDF button that it's a POST method and this POST opens to you a new window opens a PDF. (Until here all nice). But when I try to download instead the name report.pdf appears report.php.
Do you know what can it happening?
Here I left to you the code what I output FPDF:
header("Content-type:application/pdf");
$filename = 'Report -'.date("d-m-Y").'.pdf';
$pdf->Output($filename,'I');
And this is what happens when I click to download on the PDF
It's not completely clear to me, what you mean by "when I click to download on the PDF". Maybe it works with a Content-Disposition-Header:
$filename = 'Report -'.date("d-m-Y").'.pdf';
header("Content-type:application/pdf");
header('Content-Disposition: attachment; filename="'.$filename.'"');
$pdf->Output($filename,'I');
I've made a small program to search and display an PDF-file, some years ago. The PDF-file is shown inline, so the user can verify that it is the correct file. Usually he then uses the download button of the adobe plugin to save the file on his PC.
Since end of may this makes problems with Microsoft Edge and Google Chrome. The file is displayed correctly and when the user clicks the download button the save dialog opens, but when he presses the save button the file is not downloaded (error message "network error"). Strange thing is, that the print button next to the download button works.
On Firefox the whole thing still works. Each browser is updated automatically.
This is the code I use to display the pdf file:
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . $deliveryPdf->getFilename() . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
echo $deliveryPdf->getData();
Some notes, I recognized while analyzing the problem:
On Edge and Chrome the save dialog does not use the filename given in the response headers, while firefox uses the correct name
When using the "download" or "print" button in the adobe view, there is no additional request to the webserver (on none of the browsers). The only request on the webserver appears when initially opening the pdf
The print button works fine on all browsers. This raises the question where the difference between download and print is, when they both seem to use the cached file.
I use POST requests to transfer data that is needed to create the downloadable document on the fly.
When the form is submitted, the $_POST array is passed to a function that returns the path of a created FDF file.
The code:
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.$file.'"');
passthru("pdftk file.pdf fill_form ".$fdf_file." output - ");
exit;
Work on all browser (Safari, Firefox, Opera, Chrome, Internet Explorer) but not in Android stock browser.
In Android browser, the download taking place but the content of the file being empty or some HTML garbage, or the browser downloading the file but ignoring my file name and trying to save the file under the name of the script that generated it.
Any help is much appreciated.
Thanks!
UPDATE!!!
the solution: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/
Try sending the binary encoding header:
header("Content-Transfer-Encoding: binary");
If still not, you may have to send the file under a different file type.
the solution:
Android and the HTTP download file headers
http://digiblog.de/2011/04/19/android-and-the-download-file-headers/
I have a simple PHP page for downloading files that contains this code.
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $name . '"');
readfile("$file");
This downloads the current bit of music I am listening to in a simple flash player.
For some reason in the latest Safari 4.0
When the download code is executed it interrupts the loading of the song to the flash player.
Basically it makes the flash player act as if it has reached the end of the file and loaded it all.
Kinda hacky, but you could embed a hidden iframe on the page and make the download link use that (href="download.php?filename=something" target="iframe_name"). Like I said, a bit lame, but it (theoretically) would do the trick since you're not redirecting the page technically.
when i click a button the browser force the image to download
the code work it download the image with the right size but it's all black
im using this code :
// get attachment location
$attachment_location = "files/image.jpg";
if (file_exists($attachment_location)) {
// attachment exists
// send open/save jpg dialog to user
header('Cache-Control: public'); // needed for i.e.
header('Content-Type: application/jpg');
header('Content-Disposition: attachment; filename="image.jpg"');
readfile($attachment_location);
die(); // stop execution of further script because we are only outputting the jpg
}
else {
die('Error: File not found.');
}
I tried your code on my test server. (PHP5.3, Apache2.2, Win7)
Works fine in both Firefox and IE8.
Are you sure the problem is not with your image, or with your own browser?
Which version of PHP are you using? What HTTP server? Which OS?
Which browser did you test this on?
Is it possible you have other data still in the buffer? what happens if you use echo file_get_contents() instead of readfile? What happens if you call ob_clean(); flush(); before calling readfile?
Also do you have error_reporting truned on? Try turning it off before calling readfile or using any of the buffer operations i mentioned above.