Example #1
1
 /**
  * Loads template, replaces tokens with provided data and saves new file.
  * Token should have the following format in document: ${token}. In array you need to provide only (@see token)
  * @param string $templateName file name
  * @param string $newFileName file name
  * @param array $data list of tokens that should be replaced with the appropriate value, in format: (token => value)
  */
 public function saveFromTemplate($templateName, $newFileName, $data = [])
 {
     $document = new TemplateProcessor($templateName);
     foreach ($data as $key => $value) {
         $document->setValue($key, $value);
     }
     $document->saveAs($newFileName);
 }
 /**
  * add body rows
  *
  * @param Tinebase_Record_RecordSet $records
  */
 public function processIteration($_records)
 {
     $record = $_records->getFirstRecord();
     $converter = Tinebase_Convert_Factory::factory($record);
     $resolved = $converter->fromTine20Model($record);
     foreach ($this->_config->properties->prop as $prop) {
         $property = (string) $prop;
         $this->_docTemplate->setValue($property, isset($resolved[$property]) ? htmlspecialchars($resolved[$property]) : '');
     }
 }
 public function crearReciboCajaMenorAction(Request $peticion)
 {
     $formulario = $this->createForm(new ReciboCajaMenorType());
     $formulario->handleRequest($peticion);
     if ($formulario->isValid()) {
         $kernel = $this->get('kernel');
         //Se carga la plantilla
         $path = $kernel->locateResource('@FacturaBundle/Template/caja_menor_template.docx');
         $plantilla = new TemplateProcessor($path);
         $datos = $formulario->getData();
         //Se carga el archivo el valor del consecutivo
         $path = $kernel->locateResource('@FacturaBundle/Resources/config/consecutivos.yml');
         $consecutivo = null;
         $value = null;
         try {
             $value = Yaml::parse(file_get_contents($path));
             $consecutivo = $value['consecutivo_caja_menor'];
         } catch (ParseException $e) {
             printf("Problemas con el string: %s", $e->getMessage());
         }
         //Se aumenta el valor del consecutivo almacenado
         $value['consecutivo_caja_menor'] += 1;
         $yaml = Yaml::dump($value);
         file_put_contents($path, $yaml);
         // Se setea el consecutivo
         $plantilla->setValue('consecutivo', $consecutivo);
         //Se setean los datos que estaban en el formulario
         foreach ($datos as $clave => $dato) {
             if ($clave != 'fecha') {
                 $plantilla->setValue($clave, $dato);
             } else {
                 $plantilla->setValue($clave, $dato->format('d/m/Y'));
             }
         }
         //Se almacena la suma en letras
         $conversor = $this->get('numeroLetras');
         $numeroEnLetras = $conversor->numtoletras($datos['valor']);
         $plantilla->setValue('valor_letras', $numeroEnLetras);
         $plantilla->saveAs('descargas/temp.docx');
         //Se manda a guardar al cliente
         $file = 'descargas/temp.docx';
         $response = new BinaryFileResponse($file);
         $response->prepare($peticion);
         $response->send();
     }
     return $this->render('FacturaBundle:Default:creacionReciboCajaMenor.html.twig', array('formulario' => $formulario->createView()));
 }
Example #4
0
 /**
  * generate doc
  * @var array $params
  */
 public function generateDoc($params)
 {
     Yii::$app->user->identity = \app\models\User::findIdentityByAccessToken($params['template']['key']);
     header("Content-Description: File Transfer");
     header('Content-Transfer-Encoding: binary');
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
     header('Expires: 0');
     switch ($params['template']['format']) {
         case 'PDF':
             $file = Yii::$app->user->id . '_temp.pdf';
             $writeFormat = 'PDF';
             PhpWordSettings::setPdfRendererPath(dirname(__DIR__) . '/../../../vendor/tecnickcom/tcpdf');
             PhpWordSettings::setPdfRendererName('TCPDF');
             header('Content-Type: application/pdf');
             break;
         case 'Word2013':
             $file = Yii::$app->user->id . '_temp.docx';
             $writeFormat = 'Word2013';
             header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
             break;
         default:
             $file = Yii::$app->user->id . '_temp.doc';
             $writeFormat = 'Word2007';
             header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
             break;
     }
     header('Content-Disposition: attachment; filename="' . $file . '"');
     $document = new TemplateProcessor(dirname(__DIR__) . '/../../../files/' . $this->id . '/' . $this->template_file);
     /**
      * process the fields, that have been send through the rest interface
      */
     foreach ($params['template']['fields'] as $field) {
         foreach ($field as $key => $value) {
             $document->setValue($key, UTF8encoding::fixUTF8($value));
         }
     }
     /**
      * process the tables, that have been send through the rest interface
      */
     foreach ($params['template']['tables'] as $tables) {
         foreach ($tables as $name => $rows) {
             //first we create a clone for the master row
             $document->cloneRow($name, count($rows));
             //our walking variable for the table
             $ii = 1;
             foreach ($rows as $row) {
                 foreach ($row as $cell) {
                     $document->setValue(key($cell) . '#' . $ii, current($cell));
                 }
                 $ii++;
             }
         }
     }
     // save as a random file in temp file
     $temp_file = tempnam(sys_get_temp_dir(), $file);
     $document->saveAs($temp_file);
     switch ($params['template']['format']) {
         case 'PDF':
             $phpWord = IOFactory::load($temp_file);
             $xmlWriter = IOFactory::createWriter($phpWord, $writeFormat);
             $xmlWriter->save("php://output");
             break;
         case 'Word2007':
             $phpWord = IOFactory::load($temp_file);
             $xmlWriter = IOFactory::createWriter($phpWord, $writeFormat);
             $xmlWriter->save("php://output");
             break;
         default:
             readfile($temp_file);
             break;
     }
     unlink($temp_file);
     $LogEvent = new TemplateEvent();
     $LogEvent->aTemplateCreated(Yii::$app->user->identity->username, $this->id);
     \Yii::$app->end();
 }
 /**
  * @covers ::cloneBlock
  * @covers ::deleteBlock
  * @covers ::saveAs
  * @test
  */
 public function testCloneDeleteBlock()
 {
     $templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-delete-block.docx');
     $this->assertEquals(array('DELETEME', '/DELETEME', 'CLONEME', '/CLONEME'), $templateProcessor->getVariables());
     $docName = 'clone-delete-block-result.docx';
     $templateProcessor->cloneBlock('CLONEME', 3);
     $templateProcessor->deleteBlock('DELETEME');
     $templateProcessor->saveAs($docName);
     $docFound = file_exists($docName);
     unlink($docName);
     $this->assertTrue($docFound);
 }