简体   繁体   中英

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. The method is working fine and I'm getting a proper docx file. However there are no page breaks in the docx file. 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
  4. Find content

Here's what I got using 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.

<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

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

Just use this after wherever you want a page break.

More info on pagebreak here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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