OpenTbs convert html tags to MS Word tags

后端 未结 3 2018
忘了有多久
忘了有多久 2021-01-07 13:55

I am using OpenTbs, http://www.tinybutstrong.com/plugins/opentbs/tbs_plugin_opentbs.html.

I have a template.docx and am able to replace fields with content but if th

相关标签:
3条回答
  • 2021-01-07 14:04

    Since you have a conversion function for HTML to DOCX, then you can implement it in OpenTBS using a custom PHP function and parameter "onformat".

    The following function only convert line breaks:

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

    Use in the DOCX template :

    [b.thetext;onformat=f_html2docx]
    

    About converting HTML to DOCX :

    Converting a formated text into another formated text is quite often a nightmare. That's why is it wise to store the pure data instead if formated data.

    Converting HTML to DOCX is a real nightmare because the formating is not structured the same way.

    For example, in HTML tags may me nested, like this:

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

    In DOCX it will be presented as crossing, like this :

      <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>
    

    I have no solution for converting tags other than line-breaks for now. Sorry for that. And I think it would be quite difficult to code one.

    0 讨论(0)
  • 2021-01-07 14:06

    Thanks Skrol for your input on all my openTBS issues, just noticed that you are the creator of it, its a great class and what you said above was true after a day of plowing through learning the MS Word Format i had a brain wave and I am now able to produce the format that you specified above and can have bold italic and underline which is all i require, I hope this gives you a foundation to improve upon.

    I basically noticed that in the example you put you just need an array of the styles which when you find a closing tag you remove from the style array. Each time you find a tag you need to close the <w:r> and create a new one, I have tested it and it works wonderfully.

    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\">";
        }
    }
    
    0 讨论(0)
  • 2021-01-07 14:10
    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;
    }
    

    Now call this function as:

    $fieldVal = $this->f_html2docx($this->f_handleUnsupportedTags($fieldVal));
    
    0 讨论(0)
提交回复
热议问题