OpenTbs convert html tags to MS Word tags

2020-03-08 06:17发布

问题:

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 the content has html code it is displayed in the document created by the template.

First list <br /> Second Line

I have tried to use:

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

Thinking this would allow me to replace my html tags with ms office tags, but it just showed the MS Office tags in the document instead:

First Line<w:br/> Second Line

How do i convert the HTML tags into the MS Office XML equivalent.

回答1:

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.



回答2:

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\">";
    }
}