PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.29k stars 2.7k forks source link

Template to docx add unwanted new lines #1057

Open shadoWalker89 opened 7 years ago

shadoWalker89 commented 7 years ago

Hi,

I'm using the template processor to populate a file with data and then use the saveAs('file_saved_using_template.docx'). Until here everything is working as expected.

After that i need to open the saved file with IOFactory::load('file_saved_using_template.docx') And do some additional stuff, when i'm done, i download the file using

$phpWord->save('My report.docx', 'Word2007', true);

The problem is that i find a lot of new lines in the downloaded file My report.docx, that doesn't exist in file_saved_using_template.docx

The problem is that in the file_saved_using_template.docx` i find bookmarks like this

<w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/> which causes the new lines in the final downloaded file.

I looked around and i found out that these bookmarks are added to maintain the cursor position. In the file_saved_using_template.docxthery are invisible and doesn't have any effect. In theMy report.docx` they cause new lines

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/45020141-template-to-docx-add-unwanted-new-lines?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github).
troosan commented 7 years ago

Looks like a bug to me. As far as I can see every run inside a paragraph is being read as as many paragraphs, which explains all the unnecessary new lines.

        <w:p w:rsidR="00A00266" w:rsidRDefault="00967524" w:rsidP="00967524">
            <w:pPr>
                <w:jc w:val="center"/>
                <w:rPr>
                    <w:b/>
                    <w:sz w:val="72"/>
                    <w:szCs w:val="72"/>
                </w:rPr>
            </w:pPr>
            <w:r w:rsidRPr="00967524">
                <w:rPr>
                    <w:b/>
                    <w:sz w:val="72"/>
                    <w:szCs w:val="72"/>
                </w:rPr>
                <w:t>T</w:t>
            </w:r>
            <w:r w:rsidR="00340061" w:rsidRPr="00967524">
                <w:rPr>
                    <w:b/>
                    <w:sz w:val="72"/>
                    <w:szCs w:val="72"/>
                </w:rPr>
                <w:t>est</w:t>
            </w:r>
        </w:p>

is being read as

        <w:p>
            <w:pPr>
                <w:jc w:val="center"/>
            </w:pPr>
        </w:p>
        <w:p>
            <w:pPr>
                <w:jc w:val="center"/>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:sz w:val="72"/>
                    <w:szCs w:val="72"/>
                    <w:b/>
                </w:rPr>
                <w:t xml:space="preserve">T</w:t>
            </w:r>
        </w:p>
        <w:p>
            <w:pPr>
                <w:jc w:val="center"/>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:sz w:val="72"/>
                    <w:szCs w:val="72"/>
                    <w:b/>
                </w:rPr>
                <w:t xml:space="preserve">est</w:t>
            </w:r>
        </w:p>

which is obviously not correct