Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with block in template processor #1561

Open
1 of 3 tasks
Hydro8 opened this issue Jan 28, 2019 · 4 comments
Open
1 of 3 tasks

Problems with block in template processor #1561

Hydro8 opened this issue Jan 28, 2019 · 4 comments

Comments

@Hydro8
Copy link

Hydro8 commented Jan 28, 2019

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

  • PHP version: 7.1.19
  • PHPWord version: 0.14
@troosan
Copy link
Contributor

troosan commented Jan 30, 2019

did you try with version 0.16?

@egosselin-dev
Copy link

Hello, i'm getting the same issue

  • PHP version : 7.1.27
  • PHPWord version : 0.16

@haidargit
Copy link

haidargit commented Jun 9, 2019

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

  • My PHPWord version : 0.16
  • My PHP version : 7.3.1

@haidargit
Copy link

haidargit commented Jun 9, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants