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

addHtml() through error when there is list inside the content #2077

Open
kalibano opened this issue May 9, 2021 · 5 comments
Open

addHtml() through error when there is list inside the content #2077

kalibano opened this issue May 9, 2021 · 5 comments

Comments

@kalibano
Copy link

kalibano commented May 9, 2021

I am storing html data in database from ckeditor , but when i try to wrtite in file , addHtml causing an error ,
here is the code :
$wordTable3 = new \PhpOffice\PhpWord\Element\Table();
$wordTable3->addRow();
$cell3 = $wordTable3->addCell();
Html::addHtml($cell3,$session_content);

    $templateProcessor->setComplexBlock('session_content', $wordTable3);

here is the issue :
"Call to a member function addNumberingStyle() on null", exception: "Error",…}
exception: "Error"
file: "E:\xampp\htdocs\mis\vendor\phpoffice\phpword\src\PhpWord\Shared\Html.php"
line: 521
message: "Call to a member function addNumberingStyle() on null"
trace: [{function: "parseList", class: "PhpOffice\PhpWord\Shared\Html", type:

@kalibano kalibano changed the title addHtml() error addHtml() through error when there is list inside the content May 9, 2021
@Adesin-fr
Copy link

Same issue for me.
Did you manage to solve it ?

@kalibano
Copy link
Author

` WordSettings::setOutputEscapingEnabled(false);
$phpWord = new PhpWord('Word2007');
// Adding an empty Section to the document...
$section = $phpWord->addSection();

 Html::addHtml($section, $val);

 // convert the html to "word2017" xml
 $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY, './', WordSettings::hasCompatibility());
 $containerWriter = new Container($xmlWriter, $section);
 $containerWriter->write();
 $htmlAsXml = $xmlWriter->getData();

$templateProcessor->setValue('lesson_learned', $htmlAsXml );
`
I tried to add html to templateProcessor this way. But this is generating a corrupt doc file whenever special character involves. Couldn't able to find solution for this.

@NataliaAnche
Copy link

Same issue for me.
If there are lists in the text (ul, ol), then error Call to a member function addNumberingStyle() on null

@NEETLee
Copy link

NEETLee commented Mar 1, 2023

对我来说同样的问题。如果文本中有列表(ul,ol),则错误调用成员函数addNumberingStyle() on null

我通过跟踪源代码发现如果你的HTML片段中存在ol或ul那么添加HTML片段到word时所提供的wraper必须从phpword对象生成。你必须像这样使用才能正确添加包含ol或ul的HTML片段。

    private function html2xml($html): string
    {
        $phpword = new PhpWord();
        $section = $phpword->addSection();
        Html::addHtml($section, $html);
        $xmlWriter = new XMLWriter();
        (new Container($xmlWriter, $section, false))->write();
        return $xmlWriter->getData();
    }

@NEETLee
Copy link

NEETLee commented Mar 1, 2023

` WordSettings::setOutputEscapingEnabled(false); $phpWord = new PhpWord('Word2007'); // Adding an empty Section to the document... $section = $phpWord->addSection();

 Html::addHtml($section, $val);

 // convert the html to "word2017" xml
 $xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY, './', WordSettings::hasCompatibility());
 $containerWriter = new Container($xmlWriter, $section);
 $containerWriter->write();
 $htmlAsXml = $xmlWriter->getData();

$templateProcessor->setValue('lesson_learned', $htmlAsXml ); ` I tried to add html to templateProcessor this way. But this is generating a corrupt doc file whenever special character involves. Couldn't able to find solution for this.

我也有类似的使用需求,目前为止你已经实现把html转换为xml,接下来你应该使用replaceXmlBlock方法而不是setValue方法

 $processor->replaceXmlBlock($key, $xml);

replaceXmlBlock方法会直接用$xml替换掉$key所在的w:p。目前我的代码运行良好

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

4 participants