How to use the examples

Anoto Live™ forms functionality can be extended through the use of PHP event handlers. Whenever a new form application is used for the first time, a default handler is generated in the forms var/opt/dpp-appserver/apps/APP_ID/custom/ folder along with a custom.xml file which links to it.  To add your own handlers, edit these two files as per the instructions below.


<?xml version="1.0" encoding="UTF-8"?> <CustomApplicationConfiguration>         <EventHandlerClass>MyHandlers.php</EventHandlerClass> </CustomApplicationConfiguration>


<?php // File: default generated MyHandlers.php class MyHandlers {   function addEventHandlers (RequestContext $ctx) {     // Connect event handlers here   }   // Add event handler methods here } ?>


For each event you want to listen to, add code inside the addEventHandlers method which connects each specific event with your event handler method. The easiest approach is to put all your handler methods inside the forms MyHandlers class:

<?php // File: MyHandler.php class MyHandlers {    function addEventHandlers (RequestContext $ctx)   {         $ctx->addEventHandler (RendererEvent::PDF, $this, "onGeneratedPDF");         $ctx->addEventHandler (RendererEvent::XML, $tsv, "onGeneratedXML");         $ctx->addEventHandler (ActionEvent::FIELDS_PROCESSED, $concat, "onFieldsProcessed");   }    function onGeneratedPDF (RendererEvent $event)   {         $retval = 0; $lines = array();         exec  ("/usr/bin/convert $event->file /tmp/my.png", $lines, $retval);         if ($retval != 0)             throw new Exception ("pdf to png conversion failed");   }    function onGeneratedXML (RendererEvent $event) { /* ... */ }    function onFieldsProcessed (ActionEvent $event) { /* ... */ } } } ?>


Although the above method requires only one file to maintain, it may get cumbersome in cases where your event handling code grows.  An alternative approach to structuring your custom code is to use several classes and to reference them as external files. In the following example, we have one specific class for each event type:

<?php //File: MyHandlers.php require_once(dirname(__FILE__)."/GeneratePNGHandler.php"); require_once(dirname(__FILE__)."/GenerateTSVHandler.php"); require_once(dirname(__FILE__)."/ConcatFieldsHandler.php"); require_once ("/opt/dpp-appserver/src/required.php");  class MyHandlers {   function addEventHandlers (RequestContext $ctx)   {     g_Log (__METHOD__);     $png = new GeneratePNGHandler();     $tsv = new GenerateTSVHandler();     $concat = new ConcatFieldsHandler();      $ctx->addEventHandler (RendererEvent::PDF, $png, "onGeneratedPDF");     $ctx->addEventHandler (RendererEvent::XML, $tsv, "onGeneratedXML");     $ctx->addEventHandler (ActionEvent::FIELDS_PROCESSED, $concat, "onFieldsProcessed");   } } ?>


....and here is one of the referenced files (GeneratePNGHandler.php) :

<?php // File: GeneratePNGHandler.php class GeneratePNGHandler {   function onGeneratedPDF (RendererEvent $event)      {     $retval = 0; $lines = array();     exec  ("/usr/bin/convert $event->file /tmp/my.png", $lines, $retval);     if ($retval != 0)       throw new Exception ("pdf to png conversion failed");   } } ?> 

By separating your handlers in this fashion, common code can be re-used and referenced multiple times across different forms.

Was this article useful? Thanks for the feedback There was a problem submitting your feedback. Please try again later.