Documentation

Examples

Sending document for conversion

$transformer = new \Bitrix\Transformer\DocumentTransformer();
// $file - absolute file path, file link, ID from b_file. ID from b_file must be passed as integer
// When such file is available - it will be found.
// $formats - array of file processing result. For documents, its usually .pdf and .jgp
// $module - name or array of module names to be connected before callback
// $callback - full class name (or array of classes) which implement the interface \Bitrix\Transformer\InterfaceCallback
// $params - arbitrary set of parameters to be sent to callback input. 
// Can contain queue name by the key "queue" . When not available, server will select queue by default
$result = $transformer->transform($file, $formats, $module, $callback, $params);
if($result->isSuccess())
{
    // everithing is fine
}
else
{
    $errors = $result->getErrors();
}

Get status of the last command by file

When you have a file (in the same format as received on input \Bitrix\Transformer\FileTransformer::transform()) and you want to get status of the recent executed command - proceed as follows:

// $file - absolute file path, file link, ID from b_file. ID from b_file must be passed as integer
$transformInfo = FileTransformer::getTransformationInfoByFile($file);

The result returns either false when such file wasn't sent for conversion, or the following array:

array(
    'status' => $command->getStatus(), // command status
    'time' => $command->getTime(), // conversion time
    'id' => $command->getId(), // command ID from the table b_transformer_command
    'params' => $command->getParams(), // command parameters
);

Callback example

Full example can be viewed in source methods. They are \Bitrix\DocumentGenerator\Integration\TransformerManager and \Bitrix\Disk\Integration\TransformerManager.

The interface \Bitrix\Transformer\InterfaceCallback has only one method - call. Input parameters:

  • $status (int) - command status
  • $command - command name
  • $params - parameters, received on command's input
  • $result - array of executed command result, where key - format and value - result. When result contains files, they will be stored in the separate array files, where key - format, value - path to file

Method must return true on success and a string with error text otherwise (this error will be written in the conversion server).

Simple example below:

class TransformerCallback implements \Bitrix\Transformer\InterfaceCallback
{
    public static function call($status, $command, $params, $result = array())
    {
        // Specify fileId - field identifier in $params of command input.
        // Now you need to reset command data cache using field ID
        if(isset($params['fileId']) && $params['fileId'] > 0)
        {
            FileTransformer::clearInfoCache($params['fileId']);
        }
        
        if($status === 1000)
        {
            // do something with error
            return true;
        }
        
        $pdfFileResult = $result['files']['pdf'];
        if($pdfFileResult)
        {
            $file = \CFile::MakeFileArray($pdfFileResult, 'application/pdf');
            $bFileId = \CFile::SaveFile($file);
            // save $bFileId
        }
        
        // send some php event
        
        return true;
    }
}


© «Bitrix24», 2001-2024
Up