Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my problem is I want to enable the user to download the text area content as word file
-the code is work if I stop at saving to server code (the content are exactly as written in the text area)
-but when I added the download code the download's file will contain the programed page format
please help me

// Create a new PHPWord Object
02	$PHPWord = new PHPWord();
03	 
04	// Every element you want to append to the word document is placed in a section. So you need a section:
05	$section = $PHPWord->createSection();
06	 
07	// After creating a section, you can append elements:
08	$section->addText($_POST['editor1']);
09	 
10	// At least write the document to webspace:
11	$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
12	//$objWriter->save('helloWorld.docx');
13	$temp_file_uri = tempnam('', 'xyz');
14	$objWriter->save($temp_file_uri);
15	//download code
16	header('Content-Description: File Transfer');
17	header('Content-Type: application/octet-stream');
18	header('Content-Disposition: attachment; filename=helloWorld.docx');
19	header('Content-Transfer-Encoding: binary');
20	header('Expires: 0');
21	header('Content-Length: ' . filesize($temp_file_uri));
22	readfile($temp_file_uri);
23	unlink($temp_file_uri); // deletes the temporary file
24	exit;
25	 
26	}?> 

I mean by page format:
XML
<!DOCTYPE HTML>
<!--
    Verti 2.0 by HTML5 UP
    html5up.net | @n33co
    Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
    <head>

        <!-- <script type="text/javascript" src="ckeditor/ckeditor.js"> </script> -->
    </head>
    <body class="left-sidebar">


......................... etc

Update: I found what the problem was that was only because I don't have office 2007 sorry but the file was open as word file even if I wasn't have office so I thought that will not be the problem maker :( sorry for bothering you

but still there is problems before opening the file I receive warning message "said that the content are corrupted and if I'm not ensure from the source I shoudn't open it!" also there is no breaks between lines (as if I wrote all words in one paragraph!
Posted
Updated 28-Sep-13 8:10am
v2

First - does your code produce a working XML based docx file that you can open with word? If that doesn't work, then don't worry about your header information. Once you've successfully created a docx file, then you can use the header info. Suggestion: use one of the example files included w/PHPWord and, if you get that to work, then add the header block.

The following snippet works for me (at least as of last week):
// Save File
	$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
	$filename = "Perfomance_Appraisal.docx";
	$objWriter->save($filename);


// The following offers file to user on client side: deletes temp version of file
	header('Content-Description: File Transfer');
	header('Content-Type: application/octet-stream');
	header('Content-Disposition: attachment; filename='.$filename);
	header('Content-Transfer-Encoding: binary');
	header('Expires: 0');
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	header('Pragma: public');
	header('Content-Length: ' . filesize($filename));
	flush();
	readfile($filename);
	unlink($filename); // deletes the temporary file
 
Share this answer
 
thank you very much for your replay
-in case I removed the download code it will be saved as docx and will have the same content which I wrote in the textarea
-but with the download code it will be downloaded as docx but the content is not as written in the textarea
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900