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

When using the Template Processor, the "Titles" injected will appear as simple text (no title's style) in the generated document ('cause "Heading" title style name is different on non-english docx) #1803

Open
damienfa opened this issue Jan 12, 2020 · 5 comments

Comments

@damienfa
Copy link

Hello,

I try to include inside of a Word template a simple text with titles (text and titles generated dynamically by PHP and integrated in a "nice" Word file).

Everything went well, I've used the TemplateProcessor and its setComplexBlock.
However, in the generated document, the "titles" are not really appearing as Titles but as simple text. I see that the XML generated contains the style "Heading1" but simple text appears inside Microsoft Word ...

Here some sample code :

$pw = new PhpWord();
$mainSection = $pw->addSection();

// I fill my section with titles and content
$mainSection->addTitle('First Chapter', 1);
$textrun = $mainSection->addTextRun('pStyle');
$textrun->addText('Lorem ipsum....');
$mainSection->addTitle('Second Chapter', 2);
$textrun = $mainSection->addTextRun('pStyle');
$textrun->addText('Second Lorem ipsum....');

// Set my section inside the template
$templateProcessor = new TemplateProcessor($myTemplatePath);
$container = new Container($mainSection); // (explained below)
templateProcessor->setComplexBlock("my-content-here", $container);
templateProcessor->save();

🙏🏼 Have you ever meet that type of problem ? Do I miss something ?


Note :

You will notice that I use a "Container". It's because PhpWord don't let me to give my object $mainSection as second argument of setComplexBlock(. I will have a error "Attempted to load class "Section" from namespace "PhpOffice\PhpWord\Writer\Word2007\Element"." (I guess it is normal, there is no Section.php class in the Writer Elements of PhpWord).

Then, I have created a Container (see code below) : it is a simple Class that extend the PhpWord\Element\Section and forwards the section's elements.

class Container extends \PhpOffice\PhpWord\Element\Section
{
    public function __construct($section)
    {
        parent::__construct($section->getSectionId());
        $this->elements = $section->getElements();
    }
}
@damienfa
Copy link
Author

damienfa commented Oct 6, 2020

🙌🏼  I have found why it doesn't works !

When I use the TemplateProcessor, I provide a .docx file which is built from my Microsoft Word or LibreOffice application.
On my computer, those applications are installed with the French locale. It seems to be a detail, but it is important here. 🇫🇷
On the French version, the style "Heading 1" is names "Titre 1". It's not just on the user-interface, it's also inside the XML's generated !

Therefore, when I use $section->addTitle("My new title", 1); , and then inject this $section through the TemplateProcessor, the XML generated contains <w:pStyle w:val="Heading1"/> which is useless because my docx has defined "Titre1" ... This is why when I open the generated docx, I don't see any title's style.
Same problem with every non-English versions (tested with Spanish and Greek versions of Microsoft word). When I switch to the English version of Microsoft Word, the problem doesn't occur anymore.

The solution could be to let PhpWord define a custom "Heading" default name when it builds a title.
Ex: $phpWord->setHeadingStyleDefaultName("Titre"); .
Then, use this "custom heading style name" everywhere "Heading" is hard-coded today. Like here :

@damienfa damienfa changed the title Using setComplexBlock (Template Processor), "titles" appear as simple text in the generated document When using the Template Processor, the "Titles" injected will appear as simple text (no title's style) in the generated document ('cause "Heading" title style name is different on non-english docx) Oct 6, 2020
@atlza
Copy link

atlza commented Jan 13, 2021

Same case here, with french also.
@damienfa did you find any workaround ?
Regards.

@atlza
Copy link

atlza commented Jan 13, 2021

I tried to replace in html.php file, heading3 by "Title 3" :

'h3' => array('Heading', null, $element, $styles, null, 'Heading3', null),

it works as the outpule file does contain "Titre 3" in tag but does not solve the display problem and outputed docx file style does not applies style to the text.
That's a pity else we could have make the tag name change according to the document locale.

@damienfa
Copy link
Author

Hello @atlza ,

No, I didn't find any workaround, I've just forked the library and used the fix that I propose above;
I would have made a Pull Request but I was waiting to discuss about that solution. Unfortunately, noboby has replied at the moment...

I've raised this issue one year ago now. I ask to myself if the library is still maintained ... it's strange : 120 contributors but no new released since more than 1 year and 805 opened issues 😳

@atlza
Copy link

atlza commented Jan 15, 2021

I do have the same feeling, the lib seems unmaintened.
Il will try your fix has mine does not work, but it is a partial solution, has if you don't know your readers locale, it may fails for some of them. Very strange things from Word.

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

No branches or pull requests

2 participants