1

我正在尝试提出一个解决方案,允许用户上传启用邮件合并的 Word DOCX 模板文件。理想情况下,系统将读取 DOCX 文件,提取 XML,找到邮件合并字段并将它们保存到数据库中以供将来映射。我可能会使用诸如 Zend LiveDocX 或 PHPDOCX 之类的 SOAP 服务或完全使用其他东西——但现在我需要弄清楚如何识别 DOCX 文件中的字段。为此,我从这篇文章开始: http: //dfmaxwell.wordpress.com/2012/02/24/using-php-to-process-a-word-document-mail-merge/

我已经根据我的需要对其进行了一些调整(这可能是一个问题,尽管我在原始代码中也遇到了同样的错误。)具体来说,我现在没有使用它来执行邮件合并,我只是想要来识别字段。这是我所拥有的:

    $newFile = '/var/www/mysite.com/public_html/template.docx';

    $zip = new ZipArchive();
    if( $zip->open( $newFile, ZIPARCHIVE::CHECKCONS ) !== TRUE ) { echo 'failed to open template'; exit; }
    $file = 'word/document.xml';
    $data = $zip->getFromName( $file );
    $zip->close();

    $doc = new DOMDocument();
    $doc->loadXML( $data );
    $wts = $doc->getElementsByTagNameNS('http://schemas.openxmlformats.org/wordprocessingml/2006/main', 'fldChar');

    $mergefields = array();

    function getMailMerge(&$wts, $index) {
        $loop = true;
        $counter = $index;
        $startfield = false;
        while ($loop) {
            if ($wts->item($counter)->attributes->item(0)->nodeName == 'w:fldCharType') {
                $nodeName = '';
                $nodeValue = '';
                switch ($wts->item($counter)->attributes->item(0)->nodeValue) {
                    case 'begin':
                        if ($startfield) {
                            $counter = getMailMerge($wts, $counter);
                        }
                        $startfield = true;
                        if ($wts->item($counter)->parentNode->nextSibling) {
                            $nodeName = $wts->item($counter)->parentNode->nextSibling->childNodes->item(1)->nodeName;
                            $nodeValue = $wts->item($counter)->parentNode->nextSibling->childNodes->item(1)->nodeValue;
                        }
                        else {
                            // No sibling
                            // check next node
                            $nodeName = $wts->item($counter + 1)->parentNode->previousSibling->childNodes->item(1)->nodeName;
                            $nodeValue = $wts->item($counter + 1)->parentNode->previousSibling->childNodes->item(1)->nodeValue;
                        }
                        if (substr($nodeValue, 0, 11) == ' MERGEFIELD') {
                            $mergefields[] = strtolower(str_replace('"', '', trim(substr($nodeValue, 12))));
                        }
                        $counter++;
                    break;
                case 'separate':
                    $counter++;
                    break;
                case 'end':
                    if ($startfield) {
                        $startfield = false;
                    }
                    $loop = false;
                }
            }
        }
        return $counter;
    }

    for ($x = 0; $x < $wts->length; $x++) {
        if ($wts->item($x)->attributes->item(0)->nodeName == 'w:fldCharType' && $wts->item($x)->attributes->item(0)->nodeValue == 'begin') {
            $newcount = getMailMerge($wts, $x);
            $x = $newcount;
        }
    }

我用 ZipArchive() 打开 DOCX 文件没有问题,如果我使用 print_r($doc->saveHTML()); 我看到 XML 数据很好。问题是,当我执行我的代码时,我得到了致命错误:在指向此的非对象上调用成员函数 item():

$nodeName = $wts->item($counter)->parentNode->nextSibling->childNodes->item(1)->nodeName;

谷歌在试图找出这个错误时让我失望了,谁能指出我正确的方向?提前致谢!

4

1 回答 1

0

找到了一个解决方案——它并不像我希望的那样优雅,但是就这样。

使用 xml_parser_create_ns 我可以在 DOCX 文件中搜索我需要的键,特别是“HTTP://SCHEMAS.OPENXMLFORMATS.ORG/WORDPROCESSINGML/2006/MAIN:INSTRTEXT”,它标识所有标记为“MERGEFIELD”的字段。然后我可以将结果转储到一个数组中并使用它们来更新数据库。以机智:

    // Word file to be opened
    $newFile = '/var/www/mysite.com/public_html/template.docx';

    // Extract the document.xml file from the DOCX archive
    $zip = new ZipArchive();
    if( $zip->open( $newFile, ZIPARCHIVE::CHECKCONS ) !== TRUE ) { echo 'failed to open template'; exit; }
    $file = 'word/document.xml';
    $data = $zip->getFromName( $file );
    $zip->close();

    // Create the XML parser and create an array of the results
    $parser = xml_parser_create_ns();
    xml_parse_into_struct($parser, $data, $vals, $index);
    xml_parser_free($parser);

    // Cycle the index array looking for the important key and save those items to another array
    foreach ($index as $key => $indexitem) {
        if ($key == 'HTTP://SCHEMAS.OPENXMLFORMATS.ORG/WORDPROCESSINGML/2006/MAIN:INSTRTEXT') {
            $found = $indexitem;
            break;
        }
    }

    // Cycle *that* array looking for "MERGEFIELD" and grab the field name to yet another array
    // Make sure to check for duplicates since fields may be re-used
    if ($found) {
        $mergefields = array();
        foreach ($found as $field) {
            if (!in_array(strtolower(trim(substr($vals[$field]['value'], 12))), $mergefields)) {
                $mergefields[] = strtolower(trim(substr($vals[$field]['value'], 12)));
            }
        }
    }

    // View the fruits of your labor
    print_r($mergefields);
于 2012-10-24T18:40:34.877 回答