Forum


Replies: 3   Views: 692
Gdocs is not showing headings at all
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by net7  · 25-11-2020 - 10:02

Hi!
I need to have a table of contents in my doc and I need to see it only via Google Docs, so I'm trying to display headings as explained in the documentation. I'm using both "H1,H2,H3" and "phpdocx_heading" tags, but all of them are displayed as "Normal text" in GDocs. I'm using "replaceVariableByHTML" with "useHTMLExtended" option.

Here is my code:
 

$html = <<<EOF

 <h2>Surface Preparation H2</h2>
 
 <phpdocx_heading data-text="Custom heading Surface Preparation 2" data-level="2" />

 <phpdocx_heading data-text="Custom heading Surface Preparation 1" data-level="1" />

 <phpdocx_heading data-text="Custom heading Surface Preparation 3" data-level="3" />

EOF;


Here the result:

https://drive.google.com/file/d/1naFeBkRuv0wmKqrw9E2voLTuDwpwLOVk/view?usp=sharing

You can see the (not-)headings at page 3.

Can you help me? Am I doing something wrong?

 

 

Posted by admin  · 25-11-2020 - 11:04

Hello,

The heading tags are being added correctly to the DOCX using w:outlineLvl tags:

<w:outlineLvl w:val="2"/>

as you can check if you open the DOCX with MS Word or LibreOffice and update the TOC. The problem is that Google Docs has many restrictions and it doesn't read the OOXML standard (used by DOCX documents) correctly: charts, complex lists, absolute positions, complex table styles, outline level tags and others.

To set a heading tag compatible with Google Docs (and also MS Word and LibreOffice), you need to use a heading content as you are using now (using h1, h2 tags... with HTML Standard or the phpdocx_heading tag with HTML Extended) and also apply a preset custom style supported by Google Docs (Heading1 with exact name heading 1, Heading2 with the exact name heading 2...). As you are using a template (and also it contains Heading1, Heading2... tags), you can use the existing styles when importing HTML:

$html = '
  <p>Content</p>
  <h1 style="color: #b70000">A heading1 content</h1>
  <h2 style="color: #b70000">A heading2 content</h2>
';
$docx->embedHTML($html, array('wordStyles' => array('<h1>' => 'Heading1', '<h2>' => 'Heading2')));

Google Docs, and other DOCX readers, has some limitations when reading DOCX contents, although phpdocx includes option to get the maximum compatibility. Instead of supporting w:outlineLvl tag as OOXML standard details, Google Docs uses fixed paragraph style names (if you generate other style name with other name but the same properties it won't work as TOC content).

Regards.

Posted by net7  · 25-11-2020 - 15:18

Thank you, you really saved my week!
Just another doubt: do you even know why the table of contents is never displayed already "expanded"? I've every time to click on the reload button.

I've tried with:

'<phpdocx_tablecontents data-autoUpdate="false" />'

...and also with:

'<phpdocx_tablecontents data-autoUpdate="true" />'

Is this also a Google Docs restrictions problem?

Posted by admin  · 25-11-2020 - 15:39

Hello,

Setting autoUpdate to true enables an internal option in the DOCX to request the DOCX reader to update the TOC when the document is opened (OOXML standard). MS Word shows a dialog to update the TOC if the user accepts it, but this option is only supported by MS Word; other programs such as LibreOffice or Google Docs don't support it, so it needs to be updated manually (LibreOffice also supports using a macro to update it).

Using the conversion plugin based on LibreOffice and adding a macro (docs folder in the package), you can update TOC contents in DOCX files.

Regards.