Forum


Replies: 5   Views: 3195
Toc: not working links to chapters
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 TieBreakTech  · 05-06-2018 - 09:22

Hi,

when I export a document from html to MS Word, the links in the table of contents do not work.

The issue I find with MS Word (2016) on Windows is that the link in the TOC contains the path of the .docx file I create and download from my web service.

If I open the file using Open Office on Linux, it works.

The link is like:

file:///c:\users\username\downloads\filename.docx#correct_link_to_the_chapter

I think that my problem is that I need to remove "file:///c:\users\username\downloads\filename.docx" from the link.

Does anybody has some suggestions on how to do it?

Thanks!

Edoardo

Posted by admin  · 05-06-2018 - 11:17

Hello,

Headings are added to TOCs, using addHeading, embedHtml (h1 to h6 tags) a template or any other source, if the heading levels are included in the TOC. And then you need to update the TOC content.

Please check the following information about TOCs to understand how they work:

https://www.phpdocx.com/documentation/cookbook/tables-of-contents

About your question, if you extract the DOCX (it's a ZIP file), you won't file that path in the files. That information is added by some versions of MS Word by default, if you copy the file to other computer it won't appear: (https://answers.microsoft.com/en-us/mac/forum/macoffice2011-macword/hyperlinks-in-my-table-of-contents-point-to-the/ea32c870-05e6-4798-9edb-d87de6711e17?db=5)

Regards.

Posted by TieBreakTech  · 07-06-2018 - 09:45

Hi,

we are using embedHTML to convert an html text into a docx document and our goal is to create a table of content as well. We started by adding several anchors such as <a href="#abc">abc</a> and then the relative chapter with their header, like <h4 id="abc">abc</h4>

If we open the docx file with openoffice the summary works fine and we can jump from the summary to the chapters.
If we open it in microsoft word instead we found that it prepend a relative path to the anchor path and 
the jump operation does not work. The table of content appears to not be created as well.

The solution suggested by Microsoft Support does not seem to have any effect.

Thanks

Posted by admin  · 07-06-2018 - 10:56

Hello,

When you add headings to a DOCX, when you update the TOC the links are generated automatically (as long as the heading leves are in the range of levels of the TOC). You can't add links manually to a TOC using <a> tags.

We have done some quick tests and everything is working as expected with MS Word 2007, MS Word 2010, MS Word 2013, MS Word 2016, LibreOffice 5, Libreoffice 6 and OpenOffice. And we don't see any anchor path.

Please reply this topic (or send to contact[at]phpdocx.com) with the most simple sample that illustrates your issue and we'll check it. We need to run it, so please send the most simple code without doing external connections such as databases or web services.

Regards.

Posted by admin  · 07-06-2018 - 11:44

Hello,

Thanks for the code. The problem is that you are adding blank spaces in the anchor (and some DOCX readers may don't support them), and the element to generate the bookmark must be added after the element.

This is a simple sample:

$html = "
    <html>
        <body>
            <br>UnisefPolicy 1
            <h1>Sommario</h1>
            <br>
            <h4><a href='#1InformazioniGenerali'>1. Informazioni Generali</a></h4>
            <h4><a href='#2SistemiInformativieSicurezzadeiDati'>2. Sistemi Informativi e Sicurezza dei Dati</a></h4>
            <br>Other text<br>
            <h3>1. Informazioni Generali</h3>
            <div id='1InformazioniGenerali'></div>
            <br>Other text<br>
            <h3>2. Sistemi Informativi e Sicurezza dei Dati</h3>
            <div id='2SistemiInformativieSicurezzadeiDati'></div>
        </body>
    </html>";

$options = array('parseAnchors'=> true,'baseUrl'=>'//','downloadImages' => true);

About showing the file path, it's the default behaviour of Word. We recommend you to use Table of contents to accomplish what you need.

Regards.