Search code examples
phpxmlms-worddomdocument

Why is my word field limited to 257 character?


I have a program using a list of properties to generate a Word document:

        // Open the docx (is a zip).
        $zip = new ZipArchive();
        if ($zip->open($fileName, ZIPARCHIVE::CHECKCONS) !== TRUE) {
            return false; // failed opening. The template must be docx (xml in zip archive).
        }

        // Get the filename of the document into the archive (depend by the format).
        $file = substr($templateFileName, -4) == '.odt' ? 'custom.xml' : 'docProps/custom.xml';

        // Load the data.
        $data = $zip->getFromName($file);

        // The data is an XML document, so we use DOMDocument to work on it.
        $XML_doc = new DOMDocument();
        $XML_doc->loadXML($data);

        // Replace the properties.
        $properties = $XML_doc->getElementsByTagName("property");
        foreach ($properties as $property)
        {
            if ( isset($replaceData[$property->getAttribute("name")]) )
            {
                $property->firstChild->nodeValue = $replaceData[$property->getAttribute("name")];
            }
        }

Problem: one of these properties is a list of users, with mail and company. But when you had more than four users, the fifth one is cut in the middle and the next ones are not in the document.

After trying several things, I noticed that the length of the String used to create the list was always 257 characters. Do you guys have an idea of why it always stops after 257 chars ? I am clueless.


Solution

  • This is a built-in limitation for Word's DocumentProperties. The information needs to be trimmed or broken up into multiple pieces of information in order to use document properties.

    There may be alternatives. Word has other ways to store information in a document, but we need to know what happens with the information to provide guidance. Covering that would belong in a new question that would need to address:

    What is being done with this information, inside the document? Or, to put it another way, how is the information being used, once it's in the document?