Generating a word file using template with php on Linux Server
As mentioned in comment you can use You can use https://github.com/PHPOffice/PHPWord It's a PHP library that can create DOCX along with some formatting.
If you are looking for a similar solution you can also visit this link
http://webcheatsheet.com/php/create_word_excel_csv_files_with_php.php
Reading/Writing a MS Word file in PHP
Reading binary Word documents would involve creating a parser according to the published file format specifications for the DOC format. I think this is no real feasible solution.
You could use the Microsoft Office XML formats for reading and writing Word files - this is compatible with the 2003 and 2007 version of Word. For reading you have to ensure that the Word documents are saved in the correct format (it's called Word 2003 XML-Document in Word 2007). For writing you just have to follow the openly available XML schema. I've never used this format for writing out Office documents from PHP, but I'm using it for reading in an Excel worksheet (naturally saved as XML-Spreadsheet 2003) and displaying its data on a web page. As the files are plainly XML data it's no problem to navigate within and figure out how to extract the data you need.
The other option - a Word 2007 only option (if the OpenXML file formats are not installed in your Word 2003) - would be to ressort to OpenXML. As databyss pointed out here the DOCX file format is just a ZIP archive with XML files included. There are a lot of resources on MSDN regarding the OpenXML file format, so you should be able to figure out how to read the data you want. Writing will be much more complicated I think - it just depends on how much time you'll invest.
Perhaps you can have a look at PHPExcel which is a library able to write to Excel 2007 files and read from Excel 2007 files using the OpenXML standard. You could get an idea of the work involved when trying to read and write OpenXML Word documents.
PHP Read and Write in MS WORD
It's worth noting that Microsoft advises against the automation of Office documents via COM objects:
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
Although you can create .docx files without COM objects however because of their XML foundations (you can use PHPDOCX for this). An added advantage with this method is that you don't need to have a local copy of Word installed (for .docx files) and you can also use it on a Linux server (in theory, although I'm not sure the PHPDOCX product supports that).
Create doc file and write image to it using PHP
Finally I solved this problem using OpenTBS
Created a template file with header, footer, images and place holders. Place holders I replaced with dynamic values. Like
$reporttitle is defined in demo_ms_word.php and created a place holder as below [onshow.reportsubtitle].
Related Topics
PHP - How to Create a Newline Character
How to Get Protected Property of Object in PHP
Forcing a Simplexml Object to a String, Regardless of Context
MySQLi Bind_Param() Expected to Be a Reference, Value Given
Where Does PHP'S Error Log Reside in Xampp
File_Get_Contents() Breaks Up Utf-8 Characters
Show Ajax Upload Status on Progress Element
How to Write into a File in PHP
Is Closing the MySQL Connection Important
PHP Warning: PHP Startup: How to Load Dynamic Library
Turning Multidimensional Array into One-Dimensional Array
MySQLi::Query(): Couldn't Fetch MySQLi
Strict Standards: MySQLi_Next_Result() Error With MySQLi_Multi_Query