繁体   English   中英

OpenTbs 将 html 标签转换为 MS Word 标签

[英]OpenTbs convert html tags to MS Word tags

我正在使用 OpenTbs, http: //www.tinybutstrong.com/plugins/opentbs/tbs_plugin_opentbs.html。

我有一个 template.docx 并且能够用内容替换字段,但如果内容有 html 代码,它会显示在模板创建的文档中。

First list <br /> Second Line

我曾尝试使用:

$TBS->LoadTemplate('document.docx', OPENTBS_ALREADY_XML); 

认为这将允许我用 ms office 标签替换我的 html 标签,但它只是在文档中显示了 MS Office 标签:

First Line<w:br/> Second Line

如何将 HTML 标记转换为 MS Office XML 等效项。

既然您有 HTML 到 DOCX 的转换功能,那么您可以使用自定义 PHP 函数和参数“onformat”在 OpenTBS 中实现它。

以下函数仅转换换行符:

function f_html2docx($FieldName, &$CurrVal) {
  $CurrVal= str_replace('<br />', '<w:br/>', $CurrVal);
} 

在 DOCX 模板中使用:

[b.thetext;onformat=f_html2docx]

关于将 HTML 转换为 DOCX :

将格式化文本转换为另一种格式化文本通常是一场噩梦。 这就是为什么存储纯数据而不是格式化数据是明智的。

将 HTML 转换为 DOCX 是一场真正的噩梦,因为格式的结构不同。

例如,在 HTML 标签中可以嵌套,如下所示:

<i> hello <b> this is important </b> to know </i>

在 DOCX 中,它将显示为交叉,如下所示:

  <w:r>
    <w:rPr><w:b/></w:rPr>
    <w:t>hello</w:t>
  </w:r>

  <w:r>
    <w:rPr><w:b/><w:i/></w:rPr>
    <w:t>this is important</w:t>
  </w:r>

  <w:r>
    <w:rPr><w:i/></w:rPr>
    <w:t>to know</w:t>
  </w:r>

除了换行符之外,我暂时没有转换标签的解决方案。 对不起。 而且我认为编写一个代码会非常困难。

感谢 Skrol 对我所有 openTBS 问题的投入,只是注意到你是它的创造者,这是一个很棒的课程,经过一天的学习 MS Word 格式,你上面说的都是真的,我有一个脑电波,我现在可以生成您在上面指定的格式,并且可以使用粗斜体和下划线,这是我所需要的,我希望这为您提供了改进的基础。

我基本上注意到,在你放置的例子中,你只需要一个样式数组,当你找到一个结束标签时,你从样式数组中删除。 每次您找到一个标签时,您都需要关闭<w:r>并创建一个新标签,我已经对其进行了测试,并且效果很好。

class printClass {
    private static $currentStyles = array();    

    public function __construct() {}

    public function format($string) {
            if($string !=""){
            return preg_replace_callback("#<b>|<u>|<i>|</b>|</u>|</i>#",
                                        'printClass::replaceTags',
                                        $string);
        }else{
            return false;
        }
    }


    private static function applyStyles() {

        if(count(self::$currentStyles) > 0 ) {

            foreach(self::$currentStyles as $value) {

                if($value == "b") {
                    $styles .= "<w:b/>";
                }   

                if($value == "u") {
                    $styles .= "<w:u w:val=\"single\"/>";
                }   

                if($value == "i") {
                    $styles .= "<w:i/>";
                }
            }

            return "<w:rPr>" . $styles . "</w:rPr>";
        }else{
            return false;
        }
    }



    private static function replaceTags($matches) {

        if($matches[0] == "<b>") {
            array_push(self::$currentStyles, "b");
        }   

        if($matches[0] == "<u>") {
            array_push(self::$currentStyles, "u");
        }   

        if($matches[0] == "<i>") {
            array_push(self::$currentStyles, "i");
        }

        if($matches[0] == "</b>") {
            self::$currentStyles = array_diff(self::$currentStyles, array("b"));
        }   

        if($matches[0] == "</u>") {
            self::$currentStyles = array_diff(self::$currentStyles, array("u"));
        }   

        if($matches[0] == "</i>") {
            self::$currentStyles = array_diff(self::$currentStyles, array("i"));
        }

        return "</w:t></w:r><w:r>" . self::applyStyles() . "<w:t xml:space=\"preserve\">";
    }
}
public function f_html2docx($currVal) {

    // handling <i> tag

    $el = 'i';
    $tag_open  = '<' . $el . '>';
    $tag_close = '</' . $el . '>';
    $nb = substr_count($currVal, $tag_open);

    if ( ($nb > 0) && ($nb == substr_count($currVal, $tag_open)) ) {
        $currVal= str_replace($tag_open,  '</w:t></w:r><w:r><w:rPr><w:i/></w:rPr><w:t>', $currVal);
        $currVal= str_replace($tag_close, '</w:t></w:r><w:r><w:t>', $currVal);
    }

    // handling <b> tag

    $el = 'b';
    $tag_open  = '<' . $el . '>';
    $tag_close = '</' . $el . '>';
    $nb = substr_count($currVal, $tag_open);

    if ( ($nb > 0) && ($nb == substr_count($currVal, $tag_open)) ) {
        $currVal= str_replace($tag_open,  '</w:t></w:r><w:r><w:rPr><w:b/></w:rPr><w:t>', $currVal);
        $currVal= str_replace($tag_close, '</w:t></w:r><w:r><w:t>', $currVal);
    }

    // handling <u> tag

    $el = 'u';
    $tag_open  = '<' . $el . '>';
    $tag_close = '</' . $el . '>';
    $nb = substr_count($currVal, $tag_open);

    if ( ($nb > 0) && ($nb == substr_count($currVal, $tag_open)) ) {
        $currVal= str_replace($tag_open,  '</w:t></w:r><w:r><w:rPr><w:u w:val="single"/></w:rPr><w:t>', $currVal);
        $currVal= str_replace($tag_close, '</w:t></w:r><w:r><w:t>', $currVal);
    }

    // handling <br> tag

    $el = 'br';
    $currVal= str_replace('<br />', '<w:br/>', $currVal);

    return $currVal;
}

public function f_handleUnsupportedTags($fieldValue){
    $fieldValue = strip_tags($fieldValue, '<b><i><u><br>');

    $fieldValue = str_replace('&nbsp;',' ',$fieldValue);
    $fieldValue = str_replace('<br>','<br />',$fieldValue);

    return $fieldValue;
}

现在将此函数调用为:

$fieldVal = $this->f_html2docx($this->f_handleUnsupportedTags($fieldVal));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM