-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Tabs not working in titles (Edit: actually not really working at all anywhere) #862
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
The same happens for Libra Office, except none of the tabs work (titles or paragraphs). The RTF output passes the raw tab character through. I'm wondering if the instance it is working for DOCX is a pure fluke, and I've just got the whole syntax wrong? |
Looking into the DOCX source, it looks like tab characters are carried through to that too, so they are in there. |
Looking at the source of the DOCX document after saving it with a manual tab added, MS Word seems to add a tab as an XML element rather than a tab character. So this text run will generate a tab between "One" and "Two": $textrun = $section->createTextRun('entryFirst');
$textrun->addText('One');
$textrun->addText("<w:r><w:tab/></w:r>");
$textrun->addText('Two');
$textrun->addTextBreak(); I'm not sure how to get a text run unto a heading though, without generating an invalid DOCX file. |
Spent hours trying to understand the code, but have not yet been able to wrap my head around it. What I suspect needs to happen to fix this, is text and title elements (and presumably text runs) that contain |
Last one for tonight. This awful, nasty, dirty hack fixes the XML for tab characters for DOCX ONLY: protected function tabXml($string)
{
return str_replace("\t", '</w:t></w:r><w:r><w:tab/></w:r><w:r><w:t>', $string);
}
...
$section->addTitle($this->tabXml(htmlspecialchars("One\tTwo"))), $depth); It works, for all the wrong reasons (one of which is that This clip shows a Header3 (the "Class 1..." line) with a tab inserted before "Minor Puppy" using the above method, and the text tabbing out to the tab stop set up in the ruler of the header 3 paragraph style. |
I'm assuming ODT and RTF both have their own way of encoding tabs, which is why using |
ODT is a simpler "hack" as the text-run tags don't need to be closed and reopened: protected function tabOdtXml($string)
{
return str_replace("\t", '<text:tab/>', $string);
} But again, this is a hack, because the XML elements are the domain of the document output and not the construction of the document source. |
RTF uses |
Is this not fixed yet or is there a work around? I still can not use tabs in title. Thanks |
Where are you putting this code to fix the issue? Because no matter what i do this does nothing. |
This hack is in a site that has been running for three years, so I expect the core library has changed since then (or maybe it hasn't). I use this method in my controller (note I generate docx, odt and rtf): protected function tabXml($string, $extension = '.html')
{
$string = htmlspecialchars($string);
if ($extension == '.rtf') {
return str_replace("\t", '\tab ', $string);
} elseif ($extension == '.odt') {
return str_replace("\t", '<text:tab/>', $string);
} elseif ($extension == '.docx') {
return str_replace("\t", '</w:t></w:r><w:r><w:tab/></w:r><w:r><w:t>', $string);
} else {
return $string;
}
} Then I use it to wrap any text that may contain a tab character (titles, paragraphs etc,.):
Like I say, this should not work, as the Hope that helps. |
Sorry to post in here again, but does anyone have a workaround for this in 0.16. I can't get the hack to work on this version as there is quite a lot of changes. UPDATE: UPDATE 2:
The above works fine, tabs work fine. Now the TOC is competely broken and the docx errors when i open it but everything is there other than the TOC. Even taking the tab out still brakes the TOC. So this is a whole different issue maybe? |
I done a thing and managed to get it working in 0.16. Hacky but it works. \vendor\phpoffice\phpword\src\PhpWord\Writer\Word2007\Element\Title.php
to this
Just to point out I copied the exact markup from an actual word document. @judgej i managed to figure out how to use TextRuns in title.
Tabs work this way but unfortunatly it breaks to TOC. I've added an issue for it. |
Setting a tab in a paragraph works fine:
The First/Second/Third words get correctly positioned at the tab stops defined in the paragraph style.
When creating a title, this does not seem to work:
The resulting DOCX document puts a space into the position where the tab should be.
However, the tab stops are correctly added to the ruler in the title, so manually replacing the space with a tab inside MS Word takes the title text to the correct tab positions. The tabs are getting in there, are supported by MS Word, but the title text is not getting its
"\t"
tab characters handled as text tabs in the generated document.Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: