如何在php中创建Word文档的页眉/页脚

5
我已经为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");
?>

最后一页出现了错误。它会在页面内容后面再次显示页脚内容。

2个回答

5

您需要在页面顶部插入HTTP头。在打印内容之前,应始终声明HTTP头。

<?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>

此外,您可以查看PHPWord库。它将使您的生活更加轻松: https://github.com/PHPOffice/PHPWord


0

是的,错误在于将“Page”文本添加到段落标签中 - 这是可行的代码;

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

那么你从未找到过包含“Page”或其他任何单词的方法吗? - ebelendez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接