I am using PHP to dynamically generate a table which the user can download as a .xls file. When the using IE, after clicking the link, the "Do you want to open or save the file..." dialog pops up. If the user saves the file and opens it, there is no problem. But if the user chooses 'Open', Excel launches, but it is empty. There is no spreadsheet. Everything works fine in Chrome and FF.
Here is the code I'm using.
<?PHP
// filename for download
$filename = "filename" . date('M_j_Y') . ".xls";
//create the output
$output = //<table> code goes here.
//set the header to treat this as an excel file
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
echo $output;
exit;
?>
This is what happens when I click the link to download the spreadsheet and then choose 'Open' from the IE10 dialog.
Any advice on how to make the spreadsheet open when an IE user clicks 'Open' would be greatly appreciated. Thanks.
you can try this:
a. Right click on the file and choose Properties
b. On the General tab, click Unblock
c. Click OK
Check this link: https://support.microsoft.com/en-us/kb/3181507
Microsoft's documentation suggests that the issue may actually be a setting in Excel -- that Excel may be ignoring Dynamic Data Exchange (DDE) information from IE. Might check the setting described and see if that's it.
Related
I'm having this issue that I can't solve for the life of me.
I have a simple php script that redirects to pdfs, the code is as follows:
$file = "url/to/file";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Description: File Transfer");
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="somefile.pdf"');
header('Content-Transfer-Encoding: binary');
readfile($file);
exit();
If I try it directly in a browser tab, it works, it connects with Chrome's pdf plugin and shows it to me. If I try to put this on a link on another page, or try to open it via window.open in javascript, It shows a blank page. This only happens in Chrome and Safari, Firefox and Edge show it fine. Before anyone asks, I've tried it with and without the cache and content-description headers, which I've got from answers I found here, this is just the last version I have.
Any ideas?
Thanks in advance
EDIT
I forgot to mention that the problem persists on the blank tab even if I reload the page. I've checked the Network tab on devtools, they show identical results for bot requests and responses (the redirect and the direct one) but the redirect doesn't work. Also, no errors of any kind on either one.
Also, I've tried it without the exit(), doesn't change a thing.
As mentioned in the comments by ylva, for anyone searching for this, the answer can be found here
Open PDF from Chrome IFRAME failed with default PDF viewer
Main thing is, Chrome's PDF viewer embeds the PDF as an application, so if the link to a PDF file comes from an iframe, and this iframe has the sandbox attribute, chrome won't let it run, you either have to remove the sandbox attribute or have to install your own PDF viewer. So it had nothing to do with the PHP code itself.
Cheers
I work on a website, and up until Monday we were able to download a custom excel file sheet and display it. At this point, whenever we try to open it, it launches a blank, grayed-out workbook if Excel is not currently running, and nothing if it is. I have tried editing the content type to no avail. I have tried creating documents with older data that worked at the time and it doesn't work. Nothing has changed on the file system regarding this in a month, and it only just started. Here is the current code used to generate the file:
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $fname);
ob_clean();
ob_flush();
echo $somecontent;
exit;
Here is what happens when I try to open the created file.
The only way I have found that fixes this currently is to open the file in Notepad++, edit it, and save it, which then allows it to open properly in Excel.
Any help would be appreciated.
EDIT 1: So because the comments below didn't help much, I took a look around and found this video, and upon disabling those three options I could open the files again without having to go through Notepad++ first. I then narrowed down the field that was causing issues to "Protect files originating from the internet" even though before they worked fine.
I am trying to make a link that is clickable; when it's clicked on it forces a download (or even a Save As option would be nice) of an .xls file. This is what I have below.
When the link is clicked, there are no php log errors and it goes through this code. The file does exist as well. Are my headers wrong?
if (#file_exists("/tmp/report/{$php_session_id}.xls")) {
$filename = "/tmp/report/{$php_session_id}.xls";
$content_length = filesize($filename);
header("Pragma: public");
header("Expires: 0");
header("Content-type: application/vnd.ms-excel");
header("Content-length: {$content_length}");
header("Content-disposition: attachment; filename=\"missing_addresses.xml\"");
readfile($filename);
}
If the code is correct, is it possibly a server that is locked out of doing this?
Also, I am testing on Chrome for Mac (newest version)
UPDATE: I used AJAX which was the problem.
You can't initiate a download through Ajax; the response will simply vanish in the ether, as a normal Ajax response. (You could theoretically capture it, but you won't be able to write it to disk.)
Use
location.href = "source.php";
or
<a href='source.php' target='_blank'>
to direct the browser to the resource directly instead. It will automatically detect that it's a file to be downloaded, and initiate the download.
I am using PHP to create a program to accept data from a form and then create 20 different DOC files which can be downloaded on click. I have done all this and if you click the usrl to 20 files, it gives pop up to save this files as word files. Now I am working on printing all these files using one click from the program itself without saving them. This data comes from DB so it is dynamic. Is it possible to print these word files using one click. Also some documents can have multiple copies.
You can't print Word files directly from browser unless user has a word processor extension for browser like Zotero. An other option is to convert your doc file to PDF, HTML or image format. wvWare is a software you can do this with. see: http://wvware.sourceforge.net/
To create word files with PHPWord. see: http://phpword.codeplex.com/
With this you can save your doc(x) files and add the links to your php page.
Or you can directly download file when opening a PHP page by creating custom headers:
<?php
header('Content-Type: application/vnd.ms-word');
header('Content-Disposition: attachment;filename="myfile.docx"');
header('Cache-Control: max-age=0');
// Code that generates DOC
// output the file to the browser
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
exit;
?>
See forum thread here: https://phpword.codeplex.com/discussions/225901
One Top of your php or any html code
<?php
header("Content-Type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=Report.doc");
?>
My site is HTML/Javascript with AJAX calling server-side PHP. I want to allow the user to click an icon and create a report from MySQL data and then save this on the client's desktop without doing a page reload.
Options for creating a doc, as I can gather it, appear to be as follows. (I gather it needs to be done server-side, rather than with Javascript.) I'm not sure where the file ends up in each case. Please feel free to correct my misunderstandings :)
Method 1 - this appears only to create a .doc file. I'm not sure where the file gets put.
$fp = fopen("method1.doc", 'w+');
$str = "<B>This is the text for the word file created through php programming</B>";
fwrite($fp, $str);
fclose($fp);
Method 2 - this also appears to create a .doc file.
$word = new COM("word.application") or die ("couldnt create an instance of word");
echo "loaded , word version{$word->version}";
$word->visible = 1;
$word->Documents->Add();
$word->Selection->TypeText("Sample text.");
$word->Documents[1]->SaveAs("method2.doc");
$word->Quit();
$word->Release();
$word = null;
Method 3 - also a .doc file, I think.
header('Content-type: application/vnd.ms-word');
header("Content-Disposition: attachment;Filename=method3.doc");
echo "<html>";
echo "<body>";
echo "<b>My first document</b>";
echo "</body>";
echo "</html>";
Method 4 - PHPWord
Method 5 - PHPDocx
I've tested 1 & 2 in my home dev environment, but I can't find the files! What's the best way forward, please?
Thanks :)
BTW, I know there are relevant posts here, here and here, but none really answers the question.
If you want to have an icon, and when the icon is clicked it makes a download without page reload, then you just have to make a link to the icon that bring to a script that start a download using the appropriate headers.
Example :
header ('Pragma: no-cache');
header('Content-Disposition: attachment; filename="'.$File.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$Len);
Doing this, the download will start, but the page where the user has clicked will not be changed, neither reloaded.
If you want to generate dynamic DOCX files to be downloaded, I recommend to use OpenTBS. This library can generate a DOCX (and XLSX, PPTX, ODT, ODS, ...) using templates. It has a function that let you send the result directly as a download, without temporary files, or let you save is in the server side.
Methods 1 & 2 create document on the server side somewhere in filesystem (after that you need to transfer it to the client).
Method 3 creates document as a response to client request - depending on settings browser will either save it or open in window (or ask 'Save/Open/Cancel?').
I personally would have made java applet or flash application which will have access to your local filesystem. It can load document from server and save to local file system without page reloads.