Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Erro ao exportar para PDF Estou precisando exportar uma página para PDF. Na tela ela aparece e quando mando exportar ela da um warning e não exporta nada. Já tenho algo parecido em outra parte do código (com menos conteúdo) e a exportação funciona bem. Alguma dica? ...
TB
Erro ao exportar para PDF  
Estou precisando exportar uma página para PDF.
Na tela ela aparece e quando mando exportar ela da um warning e não exporta nada.
Já tenho algo parecido em outra parte do código (com menos conteúdo) e a exportação funciona bem.
Alguma dica?

Curso completo Meu Negócio Pronto
Use para si, ou transforme em um negócio: Inclui aulas e códigos-fontes
Gestor de conteúdo (SITE) + Loja Virtual (E-Commerce) + Emissor de Notas para infoprodutos


Meu negócio pronto Quero me inscrever agora!

Comentários (5)


HL

Tales, compartilha o código que faz essa chamada.
AC

Veja aqui esse exemplo:

www.adianti.com.br/framework_files/tutor/index.php?class=DocumentHtm
TB

O código é esse

<php>
  1. <?php
  2. /**
  3.  * ImprimeQuestoesFormView Form
  4.  * @author  <your name here>
  5.  */
  6. class ImprimeQuestoesFormView extends TPage
  7. {
  8.     /**
  9.      * Form constructor
  10.      * @param $param Request
  11.      */
  12.      
  13.     protected $skey "code
  14.     protected $ciphering = "cipe";
  15.     protected $encryption_iv = 'keyenc';
  16.     use Adianti\Base\AdiantiStandardFormTrait; // Standard form methods    
  17.          
  18.     public function __construct( $param )
  19.     {
  20.         parent::__construct();
  21.        
  22.         $this->form = new BootstrapFormBuilder('form_ImprimeQuestoes_View');
  23.         
  24.         TTransaction::open('ppconnectpolo'); 
  25.         $prova_feita = new ProvasFeitas($param['prova_id']);
  26.         $questoes_imprimir = ImprimeQuestoes::where('pg_id','=',$prova_feita->provas_geradas_id)->load();
  27.         TTransaction::close();             
  28.         
  29.         $this->form->setFormTitle('Imprimir');
  30.         $this->form->setColumnClasses(2, ['col-sm-3', 'col-sm-9']);
  31.         $this->form->addHeaderActionLink( _t('Print'), new TAction([$this, 'onPrint'], ['key'=>$param['prova_id'], 'static' => '1']), 'far:file-pdf red');
  32.         
  33.         // vertical box container
  34.         $container = new TVBox;
  35.         $container->style = 'width: 100%';
  36.         $container->add($this->form);
  37.         parent::add($container);
  38.     }
  39.     
  40.     /**
  41.      * Show data
  42.      */
  43.     public function onEdit( $param )
  44.     {
  45.        try
  46.         {
  47.             TTransaction::open('ppconnectpolo');
  48.             
  49.             $prova_feita = new ProvasFeitas($param['prova_id']);
  50.             $questoes_imprimir = ImprimeQuestoes::where('pg_id','=',$prova_feita->provas_geradas_id)->load();
  51.             $n_questao=1;
  52.             foreach($questoes_imprimir as $imprime){
  53.                 $object = new Questoes($imprime->questao_id);
  54.                 $questao_pg = new QuestoesDasProvasGeradas($imprime->questapg_id);
  55.                 $idv='Id'.$n_questao;
  56.                 $n_questao++;               
  57.                 $label_id = new THidden($idv);
  58.                 $this->form->addFields([$label_id]);
  59.                 
  60.                 $table = new TTable;
  61.                 $enunciado = new TLabel('enunciado');
  62.                 $enunciado = ($n_questao-1) . ') ' . $this->decode($object->texto);                
  63.                 $enunciado = $table->addRow()->addCell('<br><hr/><br>'.$enunciado);
  64.                 
  65.                 $this->form->add($table);
  66.                 
  67.                 $table_midia = new TTable;
  68.                 if($object->imagem!=null || $object->audio!=null ||  $object->video!=null){
  69.                     $row = $table_midia->addRow();
  70.                     if($object->imagem!=null) { 
  71.                         $img = new TImage($object->imagem);
  72.                         $img->width= '320px';
  73.                         $cell = $row->addCell($img);
  74.                     }
  75.                     if($object->audio!=null) {
  76.                         $cell = $row->addCell('<td>Resposta com Audio</td>');
  77.                     }
  78.                     if($object->video!=null) {
  79.                         $cell = $row->addCell('<td>Resposta com Vídeo</td>' );                
  80.                     }
  81.                 }    
  82.                 $table->addRow()->addCell($table_midia);
  83.                 
  84.                 $table_alternativas = new TTable;                
  85.                 if($object->questoes_tipos_id == 2 ){ // multipla escolha
  86.                     $items_alternativas = [];
  87.                     if(!empty($questao_pg->a_alternativas_id)){
  88.                         $alt_a = QuestoesAlternativas::where('id','=',$questao_pg->a_alternativas_id)->load();
  89.                         if($alt_a!=null){
  90.                             $items_alternativas['A'] = $this->decode($alt_a[0]->texto);
  91.                         }  
  92.                     }
  93.                     if(!empty($questao_pg->b_alternativas_id)){
  94.                         $alt_b = QuestoesAlternativas::where('id','=',$questao_pg->b_alternativas_id)->load();
  95.                         if($alt_b!=null){
  96.                             $items_alternativas['B'] = $this->decode($alt_b[0]->texto);
  97.                         }  
  98.                     }
  99.                     if(!empty($questao_pg->c_alternativas_id)){
  100.                         $alt_c = QuestoesAlternativas::where('id','=',$questao_pg->c_alternativas_id)->load();
  101.                         if($alt_c!=null){
  102.                             $items_alternativas['C'] = $this->decode($alt_c[0]->texto);
  103.                         }  
  104.                     }
  105.                     if(!empty($questao_pg->d_alternativas_id)){
  106.                         $alt_d = QuestoesAlternativas::where('id','=',$questao_pg->d_alternativas_id)->load();
  107.                         if($alt_d!=null){
  108.                             $items_alternativas['D'] = $this->decode($alt_d[0]->texto);
  109.                         }  
  110.                     }
  111.                     if(!empty($questao_pg->e_alternativas_id)){
  112.                         $alt_e = QuestoesAlternativas::where('id','=',$questao_pg->e_alternativas_id)->load();
  113.                         if($alt_e!=null){
  114.                             $items_alternativas['E'] = $this->decode($alt_e[0]->texto);
  115.                         }  
  116.                     }
  117.                     $letra = ['A', 'B', 'C', 'D', 'E'];
  118.                     $pletra=0;
  119.                      
  120.                     foreach($items_alternativas as $alternativa){
  121.                         $row1 = $table_alternativas->addRow();
  122.                         $linha = new TLabel($letra[$pletra]); 
  123.                         if ($letra[$pletra]==$questao_pg->resposta_letra) { // assinalada
  124.                              $linha->setValue('<b>X ) '. $alternativa. '</b>');
  125.                         } else {
  126.                             $linha->setValue($letra[$pletra] .') '. $alternativa);
  127.                         }
  128.                         $pletra++;
  129.                         $row1->addCell($linha);                        
  130.                     }                                  
  131.                     $linha_alternativas = $table->addRow();
  132.                     $linha_alternativas->addCell($table_alternativas);                    
  133.                 }
  134.                             
  135.                 $table_vf = new TTable;
  136.                 if($object->questoes_tipos_id == 4 ){ // VF
  137.                    $linha = new TLabel( $object->vf == 'V' ? 'Verdadeiro' : 'Falso' );
  138.                    $linha->setFontColor('#FF0000');
  139.                    $row = $table_vf->addRow();
  140.                    $row->addCell($linha);                   
  141.                 }
  142.                 $linha_vf = $table->addRow();
  143.                 $linha_vf->addCell($table_vf);
  144.                 
  145.                 $table_texto = new TTable;
  146.                 if($object->questoes_tipos_id == 1 ||  $object->questoes_tipos_id == 3){ // dissertativa ou discursiva 
  147.                    $linha = new TLabel('<b>'.$questao_pg->resposta_texto.'</b>');
  148.                    $row = $table_texto->addRow();
  149.                    $row->addCell($linha);         
  150.                 }
  151.                 $linha_texto = $table->addRow();
  152.                 $linha_texto->addCell($table_texto);
  153.                 
  154.                 
  155.             }
  156.             TTransaction::close();
  157.         }
  158.         catch (Exception $e)
  159.         {
  160.             new TMessage('error', $e->getMessage());
  161.         }      
  162.     }
  163.     
  164.     /**
  165.      * Print view
  166.      */
  167.     public function onPrint($param)
  168.     {
  169.         try
  170.         {
  171.             $this->onEdit($param);
  172.             
  173.             // string with HTML contents
  174.             $html = clone $this->form;
  175.             $contents = file_get_contents('app/resources/styles-print.html') . $html->getContents();
  176.             
  177.             // converts the HTML template into PDF
  178.             $dompdf = new \Dompdf\Dompdf();
  179.             $dompdf->loadHtml($contents);
  180.             $dompdf->setPaper('A4', 'portrait');
  181.             $dompdf->render();
  182.             
  183.             $file = 'app/output/ImprimeQuestoes-export.pdf';
  184.             
  185.             // write and open file
  186.             file_put_contents($file$dompdf->output());
  187.             
  188.             $window = TWindow::create('Export', 0.8, 0.8);
  189.             $object = new TElement('object');
  190.             $object->data  = $file.'?rndval='.uniqid();
  191.             $object->type  = 'application/pdf';
  192.             $object->style = "width100%; height:calc(100% - 10px)";
  193.             $window->add($object);
  194.             $window->show();
  195.         }
  196.         catch (Exception $e)
  197.         {
  198.             new TMessage('error', $e->getMessage());
  199.         }
  200.     }
  201.     
  202.     public  function encode($value){ 
  203.         if(!$value){return false;}
  204.         $encryption = openssl_encrypt($value$this->ciphering$this->skey, 0, $this->encryption_iv); 
  205.         return $encryption
  206.     }
  207.     
  208.     public function decode($value){
  209.         if(!$value){return false;}
  210.         $text = openssl_decrypt ($value$this->ciphering$this->skey, 0, $this->encryption_iv);        
  211.         return trim($text);
  212.     }
  213. }
  214. </php></your>
HL

Talvez o problema esteja ocorrendo porque no método construtor esteja sendo adicionado um BootstrapFormBuilder e dentro do formulário não tenha sido adicionado nenhum "campo", como um TEntry ou outros.

Verifica adicionar um:

$entry = new TEntry('entry ').

$this->form->addFields([$entry]);

TB

Resolvido... o problema estava na chamada do método que impressão que estava indo sem valor para o KEY.
Criei uma variável de sessão quando abre o form e mandei essa variável para o onPrint.