PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

Variable number of lists in TemplateProcessor

Open rkorebrits opened this issue 7 years ago • 3 comments

We are using the TemplateProcessor for writing content to documents and use https://github.com/rkorebrits/HTMLtoOpenXML to transform HTML to OOXML, which all works fine overall.

The issue that we are having currently though is with lists. As the content we are populating comes from a CMS, there can be a number of lists in the content and there is an option to use <ul> or <ol>.

Over the last few weeks, I've done quite extensive research on how the numbering works in Word documents and from what I gather, it is not possible to set the type of list from the document.xml. numbering.xml has all of that information in it:

  1. All the types of lists
  2. Every single list in document.xml has its own node, referring to a list type (e.g. <w:num w:numId="1"><w:abstractNumId w:val="0"/></w:num>).

I've got the feeling that it is not really possible in the PHPWord TemplateProcessor, to be able to dictate what type of list is added, by adding XML to the document.xml only, but I'm hoping there might be some trick that I know of?

Also, is it possible at all to write to numbering.xml from the TemplateProcessor? What I otherwise could do is prepare the numbering.xml file, to have 2 styles in it (unordered and ordered) but without a list of actual implementations, and when parsing the HTML, for each list, write a node to numbering.xml to say if its unordered/bullet or ordered/number.

Context

  • PHP version: 7.0/2
  • PHPWord version: custom branch (with cloneBlockString and insertImage), up-to-date with dev-master

rkorebrits avatar May 31 '18 16:05 rkorebrits

Hi @rkorebrits,

Did you get solution for this problem?

since I have same issue.

Best, Agung

dodeagung avatar Aug 26 '19 19:08 dodeagung

I know this is an old issue but I just spent SO MUCH time figure out a solution for this. I'm pretty sure my hair turned a little grey. I did find a solution. It's not perfect, but it seems to work.

I'm using the template processor as well and I had a specific requirement that lists should be styled with a specific setup: First level should be dash, second open bullet.

What I did was I added a block in the DOCX template and set up the desired styles for the levels. It looks like this:

image

This will ensure that the proper numbering.xml file is added in the DOCX template.

What you then can do is simply remove this block before saving the new file:

$templateProcessor->cloneBlock('list_block', 0, true, true);

The template processor's deleteBlock() didnt do the trick for me, but the above workaround (clone) did.

Now, all the lists added from <ul><li> etc is correctly styled.

ThomasBS avatar Feb 15 '24 20:02 ThomasBS

Hello I am using TemplateProcessor + HTMLtoOpenXML\Parser; I am looking for a way to prevent Word from continuing the numbering of the lists That is, if I have multiple ->setValue(${mivar}, $htmlParsed) containing list it respects the numbering of the parsed html if I have 3 variables with html content and ordered lists, the numbering in Word interprets them as if they were just 1

Expected behavior

  1.Value 1
  2.Value 2
  3.Value 3

  1.Value 1
  2.Value 2
  3.Value 3

Obtained behavior

  * 1.Value 1
  * 2.Value 2
  * 3.Value 3
  *
  * 4.Value 1
  * 5.Value 2
  * 6.Value 3

Thank you

CISPDEVELOP avatar Apr 04 '24 19:04 CISPDEVELOP