I have a PHP file that includes a link to a .pdf file that opens in a new tab - this essentially simply opens the page which downloads the PDF to the user's downloads folder immediately - the new tab doesn't remain open in the browser. This is all working well.
I'm now trying to change it so that the php page will load and do the download of the pdf in the one page, without the user having to click another link to download the pdf file.
Here's the structure with the 2 page setup - the viewReport.php page has a link like this:
$downloadLink = 'downloadFile.php?fileName='.$fileName.'&path='.$url;
$displayLink = 'This Report was successfully created. Click here to view the PDF.';
The downloadFile.php page has the following:
$fileName = $_GET['fileName'];
header('Content-type: application/force-download');
header('Content-Disposition:filename="'.$fileName.'"');
echo $fm->getContainerData($_GET['path']);
which works to immediately download the PDF. I've now changed the viewReport.php page to include this:
$downloadLink = 'downloadFile.php?fileName='.$fileName.'&path='.$url;
header('Content-type: application/force-download');
header('Content-Disposition:filename="'.$fileName.'"');
echo $fm->getContainerData($url);
When this page loads it does download a PDF file that it cannot be opened and is about 1/3 the size of the one that is generated with the 2 page version, so obviously not a valid PDF file. Also the viewReport.php page appears to stop loading once it processes this line hat downloads the pdf file:
echo $fm->getContainerData($url);
and then appears to go back to the previous page.
This is my first time trying to do this in a single page so not sure if this can even be done this way?
You won't be able to do both actions of rendering a new page for the user to view while simultaneously triggering an automatic file download with a single request.
The problem is that both of those actions require specific headers being sent in order to deliver their payloads correclty. Obviously you're seeing the headers you need to set to trigger the file download, but the delivery of a web page also requires specific headers which are normally set for you in the background. And the headers for delivering a web page for viewing vs. a file download are not compatible with each other - you can only send one or the other in a single response.
With all that said, there are probably other workarounds possible using javascript and ajax. But ultimately it all comes down to sending separate requests in order to receive two distinct sets of headers required for the two scenarios.
Related
I'm outputting a PDF via PHP with the following code. $file is an object that contains data pertaining to the file being displayed.
header('Content-type: application/pdf');
header('Content-disposition: inline; filename="'.$file->name.'"');
#readfile($file->ServerPath());
My issue is that when I go to download the file in Chrome it will occasionally try to save the page instead of the PDF.
For example, say the URL that is displaying the file is mywebsite.com/file?file_id=1234. Most of the time it will try to save the file correctly as "file_name.pdf". However, sometimes chrome will try to save the file as "file" with no extension. This seems to happen randomly.
If it makes any difference the page displaying the file is being opened in a new tab. The issue happens regardless of whether I redirect via PHP or Javascript.
I really need to resolve this issue, as these PDFs will be accessible by users.
Thanks in advance.
I have a PHP application that generates a CSV file and redirect the user to a static page linking to the file, just the example below :
https://www.example.com/public_html/static/temp/myfile.csv
Problem is, Chrome is opening the file instead of saving it. I need Chrome to save this file, as it would do with any other file like a zip or mp3, for instance.
Here is what I tried :
header('location:https://www.example.com/public_html/static/temp/myfile.csv');
header('Content-Disposition: attachment; filename=myfile.csv');
But no luck, Chrome keeps showing the myfile.csv contents instead of downloading it.
Any ideas ?
Thanks
Your argumentation in the comments has one never-ending misunderstanding: the Location header instructs any client to perform a new request to the given URI. With that the current request is over. Headers from the current request (i.e. Content-Disposition) aren't magically carried over to the next request.
In other words: your "static page linking to the file, just the example below" must send your wanted header.
Ultimately I'm sure it's not a Chrome problem either, but affects all internet browsers, as they easily detect the CSV data as text, hence being able to render/display that data instead of only being able to save it to a file.
With html5 you can set the "download" attr in an element.
Download it!
Source : http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download
After struggling with this issue for some days, the only real solution i got is to ZIP the file and then redirecting to the ZIP file instead of the CSV. By doing this, Chrome will download the ZIP file instead of opening it :
header('location:https://www.example.com/public_html/static/temp/myfile.csv.zip');
I am redirecting to an image with a Location header from PHP, and in firefox when you view the image and right click to save it prompts to save with the name of the PHP redirect script, not the name of the image. This behaviour is not present in the other browsers.
Here is the code of the file:
<?php
header("Location: foo.jpg");
Is there anyway to get firefox to use the correct name when a user opens the save dialog?
jewlhuq's suggestion to bypass php altogether works.
<?php print("<script>window.location='image.jpg';</script>"); ?>
Using php to read the file's contents and dump those to the browser with the proper headers including the following
header('Content-Disposition: inline; filename="desired-filename.jpg"');
also works.
Which is better depends on your application. I used the first for the problem listed above, in another application I needed to serve an image with a different file name than the one it is actually saved with, for that I needed the latter.
I have this download script running that allows people to click on a button and download a file without any new html pages opening. It all works well. Here is the script.
<?php
header('Content-disposition: attachment; filename=brochure-company-details.pdf');
header('Content-type: pdf');
readfile('brochure-company-details.pdf');
?>
However, I really want to be able to run the statcounter script at the same time, so as to document who is downloading the file. The Statcounter script works well enough when an html page opens and runs through its function. But I down't want a new page to open. I just want the download to start. The user experience will be that they only have the file download and without them knowing the statcounter will record the event.
Can you help?
What you want is not possible. (executing javascript on pdf download) Statcounter is an script and is executed by the browser by being included on the html. If you download a pdf file, you are not executing any js.
However, statcounter can see what links are pressed and therefore you can find which files were downloaded; as long your files are downloaded by using regular links on an html that has statcounter included. You don't need to do anything at all, they would be counted by default.
The idea Dachi gave, is to add an sql insertion to that php code of yours.
<?php
$dbh = new PDO("sqlite:/path/to/database.sdb");
//Put the insertion code here.
//Insert things like the IP, the login name, the time, etc.
header('Content-disposition: attachment; filename=brochure-company-details.pdf');
header('Content-type: pdf');
readfile('brochure-company-details.pdf');
That works too, and is a good choice if you are allowing downloads by not using regular links or not from an html that has statcounter.
In a PHP project I need to Create a PDF file and redirect to another page when user clicks a Submit button.
I have managed to create the pdf file using DOMPDF. PDF creation is done in a seperate file ('PDFRecipt.php').
I have called that page when a user clicks a button on the main page. This is how call PDF page
header('location:PDFRecipt.php');
but the problem is when I try to redirect after calling PDF page by
header('location:Other.php');
It does not create the PDF (only redirects). I tried changing
header('location:PDFRecipt.php');
to
include_once('PDFRecipt.php');
then it does not create the PDF correctly (Corrupted PDF File)
How to create the PDF file & redirect to other page?
EDIT:
Code in PDFRecipt.php
$html='SOME HTML';
include("../../dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream('FileName.pdf');
//header('location:Other.php);
Answer to question
For this to work you would need to move the second header call into the PDFRecipt file. At the moment with both of them in the one file your second call to header is overriding the first.
Remember that headers are sent when the output is sent to the users browser, which is why you often see people calling exit() right after a header('Location: http://example.org');.
So any subsequent calls to set the same header, in this case Location, will override the first until the headers are sent.
It is also worth pointing out that you should be using full web URLs in Location:
HTTP/1.1 requires an absolute URI as argument to ยป Location: including
the scheme, hostname and absolute path, but some clients accept
relative URIs. You can usually use $_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a
relative one yourself
according to the header page in the manual: http://php.net/manual/en/function.header.php
Update from comments
So you are using the stream() method to send the client the PDF - you cannot combine this with a Location: header. This is because DOMPDF has already flushed content to screen. I had assumed that your PDFRecipt.php file was storing the PDF to disk somewhere.
See DOMPDF source code for more details: http://code.google.com/p/dompdf/source/browse/trunk/dompdf/lib/class.pdf.php#3061