1

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.

Community
  • 1
  • 1
Nick
  • 5,995
  • 12
  • 54
  • 78
  • In your first and second attempt the file gets saved on the server, not sent to the client. If you need the client to save the file you should output the generated content and set proper response headers, like in your third example (though that won't exactly generate a "proper" Word document). – Another Code Mar 12 '12 at 07:14

2 Answers2

2

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.

Skrol29
  • 5,402
  • 1
  • 20
  • 25
1

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.

Victor
  • 3,669
  • 3
  • 37
  • 42