I have customized CiviCase for a client who is using it to manage project grants. Things have gone pretty well overall. There are many custom fields and several related document templates in use. "Print/Merge Document" works fine when generating a pdf, but often fails when generating a docx file.

The error that is generated on screen is simply "Cannot add TextRun in TextRun.", but looking into the CiviCRM log shows a little more detail and that the error is in the 3rd-party phpoffice code (which I've never heard of before today).

    $Fatal Error Details = array:3 [
  "message" => "Cannot add TextRun in TextRun."
  "code" => null
  "exception" => BadMethodCallException {#12247
    #message: "Cannot add TextRun in TextRun."
    #code: 0
    #file: "/var/www/sitename/wordpress/wp-content/plugins/civicrm/civicrm/vendor/phpoffice/phpword/src/PhpWord/Element/AbstractContainer.php"
    #line: 266
    /var/www/sitename/wordpress/wp-content/plugins/civicrm/civicrm/CRM/Utils/PDF/Document.php:90 {
       CRM_Utils_PDF_Document::html2doc($pages, $fileName, $format = [])
    ›   $section = $phpWord->addSection($pageStyle + ['breakType' => 'nextPage']);
    ›   \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
    › }

The error only occurs when one of the custom fields of type "Rich Text Editor" is populated. All of the simpler fields seem fine. I'm not familiar with this code and so am not really sure whether this is a bug (and out of my power to fix) or some glitch in my custom fields that should be corrected.

Any thoughts?

UPDATE: Converting the rich text field type to multi-line text didn't fix the problem. Accented characters were converted to html entities and paragraph tags were added (or maybe kept) in the filed conversion. However, deleting the content and re-adding it as UTF-8 text with the accents into the multi-line field did prevent the error. I guess there is a bug in the phpoffice code with respect to html entities. Changing the field type is not a fix, but I may be able to live with it if there is no better solution.

Running CiviCRM 5.61.0 on WordPress 6.2.2 on Debian Bullseye

  • PDF generation is a bit of a dark art and it sounds like you've identified the source of the error as non-UTF-8 non-ascii characters. Your 'rich text field' in combination with wordpress would be responsible for how accented text gets encoded when submitting to CiviCRM, so that's where I'd look for a better solution.
    – Alan Dixon
    Commented Jun 21, 2023 at 21:47

1 Answer 1


If you're feeling brave - we're using version 0.18.3 (Feb. 2022 release) of PhpWord in Civi. If you know how to use Composer, you could upgrade to PhpWord 1.1 (May 2023 release).

If you don't know Composer, you can also probably delete the phpword directory in /vendor/phpoffice and unzip the new one in there.

Hopefully this is obvious, but don't do either of these on a live site! Use a test site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.