Tuesday, January 10, 2012

How to create a Word document with HTML & PHP

This post will explain how to create a Word document without third party software. The code will create a page with orientation landscape, view print and zoom 90% using HTML and PHP statements. 
Please ignore the page #2 after you have created a document Word.


HTML & PHP

<html
    xmlns:o='urn:schemas-microsoft-com:office:office'
    xmlns:w='urn:schemas-microsoft-com:office:word'
    xmlns='http://www.w3.org/TR/REC-html40'>
    <head>
        <title>Generate a document Word</title>
        <!--[if gte mso 9]-->
    <xml>
        <w:WordDocument>
            <w:View>Print</w:View>
            <w:Zoom>90</w:Zoom>
            <w:DoNotOptimizeForBrowser/>
        </w:WordDocument>
    </xml>
    <!-- [endif]-->
    <style>
        p.MsoFooter, li.MsoFooter, div.MsoFooter{
            margin: 0cm;
            margin-bottom: 0001pt;
            mso-pagination:widow-orphan;
            font-size: 12.0 pt;
            text-align: right;
        }


        @page Section1{
            size: 29.7cm 21cm;
            margin: 2cm 2cm 2cm 2cm;
            mso-page-orientation: landscape;
            mso-footer:f1;
        }
        div.Section1 { page:Section1;}
    </style>
</head>
<body>
    <div class="Section1">
        <h1>Hello World!</h1>
    <br clear=all style='mso-special-character:line-break;page-break-after:always' />
    <div style='mso-element:footer' id="f1">
        <p class=MsoFooter>
            Page <span style='mso-field-code:" PAGE "'></span>
        </p>
    </div>
</body>
</html>
<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=HelloWorld.doc");
?>






4 comments:

  1. Thanks for the tutorial!

    Though, there is an error for the last page. It displays the footer content twice, after the content of the page also.
    Do you have a solution for this?

    ReplyDelete
    Replies
    1. No. I do not have a solution yet.

      Delete
  2. I try to create word file using this code it's work complete but i want to set header and footer in word file so how can i set that please help me for that

    ReplyDelete
  3. Really Nice Information,Thank You Very Much For Sharing.
    Wordpress Development Company

    ReplyDelete