Click here to Skip to main content
15,942,934 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Our customer wants a web-page that allows to enter image (photograph) stored in MySQL database table in MS Word document by just clicking a button. Following is the code in CodeIgniter:

Model

<pre lang="PHP"><?php

Class user_model extends CI_Model {

    function getImage($EmpId) {
       $this -> db -> select('Employee_Id', 'Employee_Photo');
       $this -> db -> from('Employees');
       $this -> db -> where('Employee_Id', $EmpId);
       $this -> db -> limit(1);

       $query = $this -> db -> get();

       if($query -> num_rows() == 1)  {
           return $query->result();
       }   else   {
                  return false;
                  }
       }
  }
?>




Controller

PHP
$FullPath_with_File = $data['full_path'];    // MS Word file with path

$result = $this->user_model->getImage($session_data['Emp_Id']);

foreach($result as $row)
{
    $data['Emp_Photo'] = $row->Employee_Photo;
}
file_put_contents($FullPath_with_File, $data['Emp_Photo'], FILE_APPEND);



Everything works fine. No-error. However, when I open the MS Word file it warns that file is corrupted, Do you want to recover? If asnwered 'yes', it asks select yes only if you trust. After answering 'yes', nothing is shown.

I want the image to be displayed in the word document. I spent entire week in searching internet, articles and even internals of MS Word. There is not even a single hint how image is recognised by MS Word.

Please help me.

What I have tried:

I spent entire week in searching internet, articles and even internals of MS Word. There is not even a single hint how image is recognized by MS Word.
Posted
Updated 19-Jan-17 9:18am

1 solution

I have bad news for you. MS-Word files are not flat files ans simply appending a picture at the end will never work.
PHP
file_put_contents($FullPath_with_File, $data['Emp_Photo'], FILE_APPEND);

It is a little more complicated.
First thing to do is launch ms-word, open a document and see how to insert a picture inside. You will see that word wants to know where is the picture in document, its size and a few other settings.

If you are on server, you have to find a library that allow to directly manipulate a doc.
If you are on client side, you can also use a library that launch word and send commands.

I don't use such library myself, but I think one is named INTEROP.
Google "php ms-word"
 
Share this answer
 
Comments
Richard Deeming 19-Jan-17 15:41pm    
Office interop isn't supported from "unattended" applications:
Considerations for server-side Automation of Office[^]

I suspect the OpenXML SDK[^] is the only viable option.
Patrice T 19-Jan-17 16:15pm    
"Office interop isn't supported from "unattended" applications:"
I didn't say that it was.
Anyway, more details for the OP is always appreciated.
Nitin_Rajurkar 20-Jan-17 6:42am    
Thank you. In fact, I suspected same thing, that it will never be simple. I want exact steps to complete this.

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