简体   繁体   English

从PHP生成DOC时需要有关分页符的帮助

[英]Need Help regarding Page Breaks while generating DOC from PHP

I'm trying to generate a doc file wrapping it through HTML codes in PHP using headers method. 我正在尝试生成一个doc文件,使用headers方法在PHP中通过HTML代码包装它。 The method is working fine and I'm getting a proper docx file. 该方法工作正常,我得到一个正确的docx文件。 However there are no page breaks in the docx file. 但是docx文件中没有分页符。 No matter how long the text is it goes in a single page. 无论文本有多长,它都在一页中。 Here is the sample code.. 这是示例代码..

<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=demo.doc");
echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset= Windows-1251\">";
echo "<body>";
echo "<b>Random Text</b>";
echo "&lt;/body>";
echo "&lt;/html>";
?>

before posting this question I also tried google and came up that by adding below code many peoples solved the similar issue. 在发布这个问题之前我也试过谷歌,并通过添加下面的代码,许多人解决了类似的问题。

echo "&lt;br style='mso-special-character:line-break; pageBreakBefore:auto'>";

However this solution is not working in my case. 但是这个解决方案在我的情况下不起作用。

  1. Open word 打开单词
  2. Create a document with a page break 使用分页符创建文档
  3. Open docx in your favorite zip file program 在您喜欢的zip文件程序中打开docx
  4. Find content 查找内容

Here's what I got using LibreOffice Writer: 这是我使用LibreOffice Writer得到的:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
    <w:body>
        <w:p>
            <w:pPr>
                <w:pStyle w:val="style0"/>
            </w:pPr>
            <w:r>
                <w:rPr></w:rPr>
                <w:t>Before page break</w:t>
            </w:r>
        </w:p>
        <w:p>
            <w:pPr>
                <w:pStyle w:val="style0"/>
                <w:pageBreakBefore/>
            </w:pPr>
            <w:r>
                <w:rPr></w:rPr>
                <w:t>After</w:t>
            </w:r>
        </w:p>
        <w:sectPr>
            <w:type w:val="nextPage"/>
            <w:pgSz w:h="15840" w:w="12240"/>
            <w:pgMar w:bottom="1134" w:footer="0" w:gutter="0" w:header="0" w:left="1134" w:right="1134" w:top="1134"/>
            <w:pgNumType w:fmt="decimal"/>
            <w:formProt w:val="false"/>
            <w:textDirection w:val="lrTb"/>
        </w:sectPr>
    </w:body>
</w:document>

You just add a style to an HTML element wherever page break required. 您只需在需要分页符的地方为HTML元素添加样式。

<h2 style="page-break-before: always;">Next Page</h2>

This header will be displayed in a new page. 此标题将显示在新页面中。

I know this is a pretty old question but this is what I am using and it works perfectly fine. 我知道这是一个非常古老的问题,但这是我正在使用的,它完全正常。

Docx XML uses this to break the pages Docx XML使用它来打破页面

<w:br w:type="page"/>

Just use this after wherever you want a page break. 只需在您想要分页的地方使用它。

More info on pagebreak here . 在分页符更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM