Categories > OpenTBS with DOCX >

Can't get blocks to work with docx

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Damien
Date: 2013-11-06
Time: 17:07

Can't get blocks to work with docx

Hello,

I'm new to OpenTBS and TBS.  (Your project looks like a great alternative to some of the other products out there, BTW) I've read a lot of your documentation over the last couple of days but still can't seem to get a basic MergeBlock to work.  (MergeField works fine.)
In my docx document I simply have:
[myblock;block=begin]

[myblock.name]

[myblock;block=end]


Using OpenTBS version 1.8.1, I've tried:
[myblock.name;block=tbs:page]

Ultimately, I would like to create a page for every record in my array.

Here's the relevant php:
class ApplicationPrinter
{

    function __construct($tbs, $template)
    {   
        $this->template = __DIR__.$template;
        $this->tbs = $tbs;
    }
   
    public function make()
    {
        $data = array(
            array( 'name' => 'John Doe'),
            array( 'name' => 'Sally Bob')
        );
       
        $this->tbs->LoadTemplate($this->template);
        $this->tbs->MergeBlock('myblock', $data );
    }

    public function printSheet()
    {
        $this->tbs->show(OPENTBS_DOWNLOAD, 'application.docx');
    }

}

So right now, I'm just getting my docx template back to me without the data rendered in the page.  

I'm using MAMP on my local mac to test out my web application.  Don't know if those config settings would affect anything, but as I said above MergeField works fine.  I'm also using the OpenTBSBundle for Symfony2 ~ https://github.com/mbence/OpenTBSBundle/blob/master/README.md ~ but I looked at its code and it appears to be straight-up the exact same implementation that I would use if I were installing it myself.

Any suggestions?  I'm hoping this is just the result of some stupid oversight on my part.
Thanks.
By: Damien
Date: 2013-11-06
Time: 19:02

Re: Can't get blocks to work with docx

I found the problem!  It has to do with cutting and pasting in word.  When you do that, sometimes it screws up the underlying xml document.
I used the debugging tool to figure this out.

$this->tbs->LoadTemplate($this->template);
$this->tbs->PlugIn(OPENTBS_DEBUG_XML_SHOW);
$this->tbs->MergeBlock('myblock', $data );

That shows the underlying xml.  I found this:

<w:t>
     [
    </w:t>
   </w:r>
   <w:r>
    <w:rPr>
     <w:rStyle w:val="opt-name"/>
     <w:rFonts w:eastAsia="Times New Roman" w:cs="Times New Roman"/>
    </w:rPr>
    <w:t>
     myblock
    </w:t>
   </w:r>
   <w:r>
    <w:rPr>
     <w:rFonts w:eastAsia="Times New Roman" w:cs="Times New Roman"/>
    </w:rPr>
    <w:t>
     .name;
    </w:t>
   </w:r>
   <w:r>
    <w:rPr>
     <w:rStyle w:val="opt-name"/>
     <w:rFonts w:eastAsia="Times New Roman" w:cs="Times New Roman"/>
    </w:rPr>
    <w:t>
     block
    </w:t>
   </w:r>
   <w:r>
    <w:rPr>
     <w:rFonts w:eastAsia="Times New Roman" w:cs="Times New Roman"/>
    </w:rPr>
    <w:t xml:space="preserve">
     =tbs:page]
    </w:t>
   </w:r>

You see - the [myblock.name;block=tbs:page] is split up over several different xml tags.  If I go back to my word template and (instead of copying and pasting) simply type "[myblock.name;block=tbs:page]", I get this:

<w:r>
    <w:rPr>
     <w:rFonts w:eastAsia="Times New Roman" w:cs="Times New Roman"/>
    </w:rPr>
    <w:t>
     [myblock.name;block=tbs:page]
    </w:t>
   </w:r>

And consequently, MergeBlock works as it should...