This project is read-only.

The Office Open XML file cannot be opened because there are problems with the contents.

Dec 2, 2012 at 3:48 PM
Edited Dec 2, 2012 at 3:56 PM

This is my 1st time using PHPWORD and I'm not pretty good about it .. at the same time, I'm just a newbie in PHP and MYSQL.

So I will explain what I'm trying to do :
I'm trying to use data from database phpmyadmin at Microsoft Word with my own Microsoft Word template that called "TamatBelajar.docx". After that, the file will be saved as "TamatBelajar007.docx".

I don't know if my code was right or not .. because I getting this error message "The Office Open XML file cannot be opened  because there are problems with the contents." when I try to open "TamatBelajar007.docx".

This code I take from someone and I change some code based on my database :

If someone out there can help me, please help me because I need to get it done in less than 6 days.

I'm so damn stress.. Thank You for reading . I'll waitting for your reply.

 

<?php
session_start();

require_once('PHPWord.php');

	$PHPWord = new PHPWord();
	$document = $PHPWord->loadTemplate('TamatBelajar.docx');

	$host="localhost";
	$username="root";
	$password="";
	$database="finalproject";
	$osr = mysql_connect($host, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR); 
	mysql_select_db("$database") or die (mysql_error());
	
	$query_RecordsetFail = "SELECT * FROM admin,student where admin.adminfullname and student.Bil and student.student_id and student.fullname 
	and student.ic_number and student.course and student.type_of_letter ;	";
	$RecordsetFail = mysql_query($query_RecordsetFail, $osr) or die(mysql_error());
	$row_RecordsetFail = mysql_fetch_assoc($RecordsetFail);
	
	//set value
	$document->setValue('NamaPelajar', $course);
	$document->setValue('NRIC', '8');
	$document->setValue('Course', 'abc123');
	$document->setValue('ID', 'wan');

	$new_file = 'TamatBelajar007.docx';
	$document->save($new_file);
		
		  	header('Content-Description: File Transfer');
			header('Content-type: application/force-download');
			header('Content-Disposition: attachment; filename='.basename($new_file));
			header('Content-Transfer-Encoding: binary');
			header('Content-Length: '.filesize($new_file));
			readfile($new_file);

?>
Dec 2, 2012 at 4:06 PM

Update : If you can see at //set value ... I only try to change the 1st value as "$course" because I want to try it's working or not with call only 1 data from database ..but looks like there's must have a problem inside this code which I don't know where it is.

Jul 29, 2013 at 7:53 AM
I found it occurs when the template having images.
Aug 2, 2013 at 11:15 AM
Whats is the value of $course?

In somewhere you have to use iconv() function to convert charset of text you want to write to UTF since PHPWord accept only UTF8 as charset and also you have to change setValue function as below
public function setValue($search, $replace) {
    if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
        $search = '${'.$search.'}';
    }

      preg_match_all('/\$[^\$]+?}/', $this->_documentXML, $matches);

    for ($i=0;$i<count($matches[0]);$i++){

    $matches_new[$i] = preg_replace('/(<[^<]+?>)/','', $matches[0][$i]);

    $this->_documentXML = str_replace($matches[0][$i], $matches_new[$i], $this->_documentXML);
    }        

    /*if(!is_array($replace)) {
        $replace = utf8_encode($replace);
    }*/

    $this->_documentXML = str_replace($search, $replace, $this->_documentXML);
}