Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue templateProcessor setValue html display #1615

Open
minhnhsg opened this issue Apr 15, 2019 · 2 comments
Open

Issue templateProcessor setValue html display #1615

minhnhsg opened this issue Apr 15, 2019 · 2 comments

Comments

@minhnhsg
Copy link

Hi everyone,
I'm using PHPWord v0.16. I have a issue like this:

  • Using templateProcessor and set setValue with HTML display (HTML code, with superscript, subscript, ...). I have searched for a solution for it many times, but no.
    Anyone can help me with this issue?
    Thank for your help, have a nice day!
@yellow1912
Copy link

For simple HTML, you could use setComplexValue, something like this

$text = new TextRun();
Html::addHtml($text, 'your html here');
$templateProcessor->setComplexValue('replace', $text);

It doesn't work very well with complex, nested elements such as ordered list, I haven't found a good way to do it.

@ddruganov
Copy link

i know this is a very old post but found a reasonably good solution that works:

private function prepareHtml(string $value): AbstractElement
    {
        $content = html_entity_decode($value);

        $dom = new DOMDocument();
        try {
            $dom->loadXML($content);
        } catch (Throwable) {
            $content = "<p>$content</p>";
            $dom->loadXml($content);
        }
        $dom->documentElement->setAttribute('style', 'font-family: Tahoma; font-size: 12pt');
        $content = $dom->saveXML($dom->documentElement);

        $table = new Table(['cellMargin' => 0]);
        $table->addRow();
        $cell = $table->addCell();
        Html::addHtml($cell, $content);

        foreach ($cell->getElements() as $element) {
            if (method_exists($element, 'getParagraphStyle')) {
                $element->getParagraphStyle()->setIndent(0);
            }
        }

        return $table;
    }

@yellow1912 your solution might be valid but in my case throws a BadMethodCallException: "Cannot add TextRun in TextRun."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants