Forum


Replies: 3   Views: 35
Phpdocx_link is adding/removing breaks

Posted by ralfjahr  · 06-04-2021 - 10:05

Hello,

I haven an issue when using phpdocx_link elements in html code which I am adding to my document.

You can see the problem here: https://cloud.quintet.io/index.php/s/bzBo6H8WMFX3FgW

When I use a phpdocx_link element it ignores breaks before the element, and adds a new line after the element. 

The code looks like this:

if($condition) {
    $string .= '<b><phpdocx_link data-text="' . $contact->getName() . '" data-url="#' . $contact->getHash() . '" /></b>';
} else {
    $string .= "<b>" . $contact->getName() . "</b>";
}


How can I change this behavior?

Thanks!

Posted by admin  · 06-04-2021 - 11:02

Hello,

We have run the following code and it works correctly:

$html = '<span>Content</span> <b><phpdocx_link data-text="My Link" data-url="https://www.phpdocx.com" /></b>';
$docx->embedHTML($html, array('useHTMLExtended' => true));

$html = '<span>Content</span> <phpdocx_link data-text="My Link" data-url="https://www.phpdocx.com" />';
$docx->embedHTML($html, array('useHTMLExtended' => true));

We think the problem doesn't come from the phpdocx_link tag but the default behaviour of tables in MS Word, if a content exceeds the cell margin as seems the case (adding a link or not and the blank spaces added to the contents may vary the content size), an automatic line break is done.
We recommend you to test the previous script and your code out of table contents to check if this is the case. And also test using an a tag instead of phpdocx_link to check if you get the same output.

If you send to contact[at]phpdocx.com your DOCX we'll check it to know the exact source of that behaviour.

Regards.

Posted by ralfjahr  · 12-04-2021 - 13:25

Hello,

thanks for your response. Unfortunately we could not make it work.

The final HTML-code for the table that will be embedded is this one:

<table width="100%" style="table-layout: fixed;">
<style>
table {
font-size: 10pt;
border-collapse: collapse;
border-spacing: 0px;
}

tr.border_top_bottom td {
border-bottom: 1pt solid black;
border-top: 1pt solid black;
}

tr.border_top_only td {
border-top: 1pt solid black;
}

tr.border_bottom_only td {
border-bottom: 1pt solid black;
}

tr {
page-break-inside: avoid;
}
</style>

<tr width="503.15pt" class="border_top_bottom">
<td width="75pt" valign="top">
<b>10:30 AM</b>
<br/>
(15 minutes)
</td>
<td width="252pt" valign="top">
<b><phpdocx_link data-text="Maurice Nicolas" data-url="#7d6a55a24099ef9068ad4bbbc35aa0d1" /></b> - BioNTech AG<br />
Business Development<br />
P: +1169005827387<br />
C: +1 231-607-8121<br />
demo_qmbc9b@troutaccess.com                    <br/>
<b><phpdocx_link data-text="Hans Hansen" data-url="#1c6bd954cd94500481cd13cb21fda279" /></b> - ABC Capital<br />
PNC Relationship Manager<br />
hans.hansen@gmail.com                                        </td>
<td width="176pt" valign="top">

Germany

</td>
</tr>

</table>

The strange thing is that when we use anchor tags it works perfectly fine, however we cannot use them as we need to link to another position in the document.

Thanks!

Posted by admin  · 12-04-2021 - 14:14

Hello,

Thanks for posting the code that illustrates your issue.

The problem is how the WordFragments work internally, that in certain cases don't add a run-of-text to apply that break. Instead of adding new tags to wrap the contents to use phpdocx_link with line breaks, for your case the best and easiest approach is using a tags (that allows using internal anchors too) wrapping the contents in a paragraph:

<td width="252pt" valign="top">
<p>
<b><a href="#7d6a55a24099ef9068ad4bbbc35aa0d1">Maurice Nicolas</a></b> - BioNTech AG<br />
Business Development<br />
P: +1169005827387<br />
C: +1 231-607-8121<br />
demo_qmbc9b@troutaccess.com <br/>
<b><a href="#1c6bd954cd94500481cd13cb21fda279">Hans Hansen</a></b> - ABC Capital<br />
PNC Relationship Manager<br />
hans.hansen@gmail.com </p></td>

As you need to apply the anchors to link internal contents instead of external ones, you can set parseAnchors as true when importing HTML:

$docx->embedHTML($html, array('parseAnchors' => true));

Using the previous code your internal links will work with the br tags as needed. You can read about this option on the API page (https://www.phpdocx.com/api-documentation/word-content/embed-html-Word-document-with-PHP) and the introduction doc about HTML to DOCX (https://www.phpdocx.com/documentation/introduction/html-to-word-PHP).

Regards.