user2729379
user2729379

Reputation: 51

How to create a Word document header/footer in php

I have add header/footer are in document in php. but i have footer content twice in last page. I have user this code:-

    <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");
?>

there is an error for the last page. It displays the footer content twice, after the content of the page also.

Upvotes: 5

Views: 12835

Answers (2)

sjreese
sjreese

Reputation: 1

Yes, the error was in adding the "Page" text in the paragraph tag - Here is the code that works;

     <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 class="Section1">Hello World!</h1>
      <p class="MsoNormal">test is a test </p>
      <br clear=all style='mso-special-character:line-break;page-break-after:always' 
      />
      <div class="MsoFooter" style='mso-element:footer' id="f1">
      <p class=MsoFooter align=right style='margin-bottom:0in;margin-bottom:.0001pt; 
      text-align:right;line-height:normal'> 
      <span style=<span style='mso-field-code:" PAGE "'></span></p> 
      </div>
      </div>
      </body>
      </html>

      <?PHP
      header("Content-type: application/vnd.ms-word");
      header("Content-Disposition: attachment;Filename=HelloWorld.doc");
      ?>

Upvotes: 0

Wissam El-Kik
Wissam El-Kik

Reputation: 2515

You need to insert the HTTP Header at the top of the page. The HTTP Headers should be always declared before anything is printed on the page.

<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=HelloWorld.doc");
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<title>Hello World</title>
<style>
<!--
 /* Style Definitions */
 p.MsoNormal
    {margin:0cm;
    margin-bottom:.0001pt;
    font-size:12.0pt;
    font-family:"Times New Roman";}
-->
</style>
</head>
<body>
<p class=MsoNormal>Hello World</p>
</body>
</html>

Also, you can have a look at the PHPWord library. It'll make your life easier: https://github.com/PHPOffice/PHPWord

Upvotes: 5

Related Questions