Convert HTML to MS Word Document using PHP

Convert-HTML-to-MS-Word-Document-Using-PHP
Convert-HTML-to-MS-Word-Document-Using-PHP

You can get simple and understandable steps to convert HTML to MS Word documents using PHP. Before we move into the conversion that can be done using PHP, let’s have a brief look at PHP.

What is this PHP?

PHP means Hypertext Preprocessor, which is a programming language for web development and using PHP you can create dynamic web page content and many more.
It can be used across various platforms like ( Windows, Mac OS, CentOS Linux, Unix ). It is flexible to work and effective with a maximum of servers like Apache. PHP can be used to encrypt the data, to enhance data security. The access of users can also be controlled using PHP. PHP can perform various database functionalities like Add, Delete, Alter, Update, etc., Cookies can be sent and received to web servers using PHP. When it comes to the filing system of web servers, PHP can be used to open, read, write, edit, delete, close operations can be performed. PHP has a wide range of usability because of its adaptability with the server with its language.

Before Conversion process HTML file

Hope you would have already installed PHP in your system. If not, click on the below links to install PHP.

Apache Web Server  – Check out our article, How to Install Apache on CentOS 7.
PHP  – Check out our article, How to Install PHP on CentOS.
MySQL  – Check out our article, How to Install MySQL on CentOS 7.

Here we go:

For converting HTML to a Word file, we need to create two basic parts of coding. The first one is an Index file and export file.

In the index file, the user can enter the title and content of the word file in HTML format and click on the Export to word button. Then an export file will export the HTML to a word doc file, then you can download and save it as .doc in your local drive.

1) Index File:

Here we are going to create two main fields for data entry (Header and Body) and one action field ( Submit or Export to Word). PHP HTTP, in this you need to format HTML or PHP using word-friendly CSS and add header information to PHP scripts and you can use the simple PHP-HTML with inline CSS.

Now we can get started with the coding part. We have to make one form to enter the title and the description part to export HTML to a word file. You can see in the below picture is what we are going to create exactly using the index file.

Convert HTML to MS Word Document using PHP
Convert HTML to MS Word Document using PHP

For making HTML form, here we are using form tag with method as post and action as export.php
<form method=”post” action=”export_file.php”>

After this we have to make text box for entering title. For this we have make, Input type as Text, Name as title. In this text box user can enter title for the word file
<input type=”text” name=”title” id=”title” class=”form-control” />

Then for entering a description, we have to create textarea HTML with Name as description, rows as 10, then close the text area tag.
<textarea name=”description_text” class=”form-control” rows=”10″></textarea>

Last we have to create one submit button to PHP script. For this, Input type as submit, Name as create word, Value as export to word.
<input type=”submit” name=”create_word_file” class=”btn btn-info” value=”Export to Word” />

index.php Code :

2) Export File:

Once the user clicks on the Export to word button. The PHP script will be submitted to the HTTP post method through the export.php script and this script will execute and export HTML text to a word file.

If we need to check whether any action has been received from index.php. For that give $post as creating a word in check condition. (create_word_file’ is the term used on index.php script). This will check for action if no action is received then it won’t execute any block of code.
if(isset($_POST[“create_word_file”]))

If action received, then we have to check whether any text is given as input or not. For this we are using empty function within if block. This will reducing PHP error.
if(empty($_POST[“title”]) || empty($_POST[“description_text”]))

If anyone field was blank then give a message. Both Fields Are Required
echo ‘<script>alert(“Both Fields Are Required”)</script>’;

Once the user clicks the ok button on the dialog box, then the page has to be redirected to the index.php script. For this, we have to write javascript between open and closed tag, Window location as index.php. This will redirect the page to form the field.

If both text fields are present then else part will start executing. Html script from server to user’s drive, where user can save in content type. Here we require a word. So enter the application as ms-word.

header(“Content-type: application/vnd.ms-word”);

Content-Disposition will recommend a file name to the browser to display in the dialog box. The word file will be downloaded to a local drive.

header(“Content-Disposition: attachment;Filename=PHPADVICES-DEMO-“.rand().”.doc”); Pragma will prevent the client from cache and response. header(“Pragma: no-cache”); Expires 0 will prevent cache in the browser. header(“Expires: 0”); After this, using post function the content in the Text box of index file will get loaded and generate as a word file.

Export Code :

Here you go…!! We have learned how to convert HTML file to Word format using PHP.
You can now start a conversion on HTML file to Word !!
Today, we learned about “What is PHP ?”, “How to create PHP file for Conversion ?” , “What is Index file?”, “What is export file?” and finally “How to convert PHP to Word?”

Hope it helped you in an easy way to write PHP files with more understanding. Please let us know in the comment section if you have any concerns.

You can also share with your friends, who need to learn PHP. Will meet you in my upcoming articles.