CakePHP + TinyButStrong

Anyone tried using TinyButStrong together with CakePHP? I have no prior knowledge of TinyButStrong but seems to be a good way to generate Word documents from templates. But I am not sure how to integrate this with a CakePHP application.

Thank you for any ideas / suggestions.

Best regards, Tony.

Answers


I presume you mean TinyButStrong with the OpenTBS plug-in which can merge DOCX (and other Ms Office and OpenOffice documents) using templates.

Here is a way to add an export action in a CakePHP Controller which is destined to generate a Docx to be downloaded.

The following code is available for CakePHP version 1.3, it is not tested with version 2.0.

Steps :

1) Add the TBS and OpenTBS classes in the vendor directory, under a subdirectory:

vendors/tbs/tbs_class.php vendors/tbs/tbs_plugin_opentbs.php

2) Create a CakePHP helper that will simplify the preparation of TBS + OpenTBS:

app/views/helpers/tbs.php

<?php

class TbsHelper extends AppHelper {

    function getOpenTbs() {
        App::import('Vendor', 'tbs/tbs_class');
        App::import('Vendor', 'tbs/tbs_plugin_opentbs');

        $tbs  = new clsTinyButStrong; // new instance of TBS
        $tbs->Plugin(TBS_INSTALL, OPENTBS_PLUGIN); // load OpenTBS plugin   
        return $tbs;
    }

}

3) Now add a new "export" action in the controller that should generate the Docx:

app/controllers/example_controller.php

<?php

class ExamplesController extends AppController {

    var $name = 'Examples';

    function export() {

        // Stop Cake from displaying action's execution time, this can corrupt the exported file
        // Re-ativate in order to see bugs
        Configure::write('debug',0);

        // Make the Tbs helper available in the view
        $this->helpers[] = 'Tbs';

        // Set available data in the view
        $this->set('records', $this->Example->find('all'));

    }

}

4) The last thing is to create the corresponding view. Don't forget to place your DOCX template in the same folder as the view.

app/views/examples/export.ctp (below) app/views/examples/export_template1.docx (to build with Ms Office)

<?php

ob_end_clean(); // Just in case, to be sure

// Get a new instance of TBS with the OpenTBS plug-in
$otbs = $tbs->getOpenTbs(); 

// Load the DOCX template which is supposed to be placed in the same folder
$otbs->LoadTemplate(dirname(__FILE__).'/export_template1.docx');

// Merge data in the template
$otbs->MergeBlock('r', $records);

// End the merge and export
$file_name = 'export.docx';
$otbs->Show(OPENTBS_DOWNLOAD, $file_name);

exit; // Just in case, to be sure

TinyButStrong gives facilities to merge PHP global variables, but it is recommended to not use such feature within CakePHP. Instead, you should use MergeBlock() and MergeField() with the data set by the Controller for the View.

If you met bugs, don't forget to disable the line

Configure::write('debug', 0);

and this will show you the CakePHP errors. Otherwise CakePHP will hide all errors including PHP errors.

Don't forget that OpenTBS has also a debug mode. See the manual if needed.

You can also make this a lib (to be used anywhere in your application).


Need Your Help

Css,Html,Xml Style Sheet?

asp.net visual-studio-2008

I am new to Asp.net and i saw many tutorials talking about css and Html and Xml Style sheet so please can anyone tell me what is the difference between those 3 Languages and what they are used for ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.