PHPOffice / PHPWord

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

Problems with block in template processor #1561

Open Hydro8 opened 5 years ago

Hydro8 commented 5 years ago

This is:

Expected Behavior

Be able to delete or clone block in a long text.

Current Behavior

I'm able to delete block (for example) in a small word file. If file is too long, deleteBlock won't work.

Failure Information

I tried with a brand new document but I have the same problem. For exemple I use a simple template with this text :

PHP Word
PHPWord is a pure PHP library for reading and writing Word, ODT, and RTF files. This file is the source file of read/write capabilites of PHP Word. The text in red should be changed when writing.
${delete}
PHPWord can apply font formats such as bold, italics, color, underline, strikethrough, subscript, superscript, or highlighted. You may also notice that there’s one text break (empty paragraph) before this one that can be created also by PHPWord.
${/delete}
PHPWord can also format paragraph such as this justified, 12pt before and 12pt after with 1.5 lines spacing paragraph. This formatting can be applied inline or using predefined style as we use to do in Word.

It works, but if I add one paragraph, it doesn't work :

PHP Word
PHPWord is a pure PHP library for reading and writing Word, ODT, and RTF files. This file is the source file of read/write capabilites of PHP Word. The text in red should be changed when writing.
${delete}
PHPWord can apply font formats such as bold, italics, color, underline, strikethrough, subscript, superscript, or highlighted. You may also notice that there’s one text break (empty paragraph) before this one that can be created also by PHPWord.
${/delete}
PHPWord can also format paragraph such as this justified, 12pt before and 12pt after with 1.5 lines spacing paragraph. This formatting can be applied inline or using predefined style as we use to do in Word.
PHPWord can also format paragraph such as this justified, 12pt before and 12pt after with 1.5 lines spacing paragraph. This formatting can be applied inline or using predefined style as we use to do in Word.

I can make this work by modify templateProcessor.php with this lines : preg_match( '/\$\{' . $blockname . '}.*\$\{\/' . $blockname . '}/is', $this->tempDocumentMainPart, $matches ); if (isset($matches[0])) { $this->tempDocumentMainPart = str_replace( $matches[0], $replacement, $this->tempDocumentMainPart ); } But in this case I have a white line so not very cool.

How to Reproduce

Please provide a code sample that reproduces the issue.

        // Template processor instance creation
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('template/Trame_EDDC.docx');
        // Will clone everything between ${tag} and ${/tag}, the number of times. By default, 1.
        $templateProcessor->deleteBlock('delete');
        $templateProcessor->saveAs('template/Trame2.docx');
        return response()->download(public_path('template/Trame2.docx'));

Here a link to download word files :

https://wetransfer.com/downloads/b6731f6817ce7a8f0991546b02d4596120190128110140/5babac

Context

troosan commented 5 years ago

did you try with version 0.16?

egosselin-dev commented 5 years ago

Hello, i'm getting the same issue

haidargit commented 5 years ago

Thanks @Hydro8 🥇 your custom code works. Really Nice. I changed my inner function code just like yours. your code works more flexible for .docx template and it's comfy for me...⭐️⭐️⭐️

Well, the (MsWord) template that I use for implement replaceBlock method
is Ms Word 2019 for Mac. :-)

here's my custom replaceBlock function code here.txt

haidargit commented 5 years ago

Hello, i'm getting the same issue

  • PHP version : 7.1.27
  • PHPWord version : 0.16

you can customise your function just like I did. 👍 @Hydro8 custom code works well.. even for the newest version of MsWord. (I am using Ms Word with license 2019 for Mac) here.txt