PA
Problema ao gerar PDF
Prezados(as),
Estou enfrentando um problema ao gerar um arquivo PDF. Ao adicionar uma célula, diferente de uma imagem, não é possível coloca uma margin, ficando fixo em uma parte do arquivo.
Exemplo:
// $w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link=''
$pdf->Cell(6,.47,'IDENTIFICACAO DO ESTUDANTE','',1,'R');
// $file, $x=null, $y=null, $w=0, $h=0, $type='', $link=''
$pdf->Image('appimagesnew_user.png', 6.5, 1.5, 1.7, 1.9,'png');
Segue o código abaixo:
Estou enfrentando um problema ao gerar um arquivo PDF. Ao adicionar uma célula, diferente de uma imagem, não é possível coloca uma margin, ficando fixo em uma parte do arquivo.
Exemplo:
// $w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link=''
$pdf->Cell(6,.47,'IDENTIFICACAO DO ESTUDANTE','',1,'R');
// $file, $x=null, $y=null, $w=0, $h=0, $type='', $link=''
$pdf->Image('appimagesnew_user.png', 6.5, 1.5, 1.7, 1.9,'png');
Segue o código abaixo:
- <?php
- //define('fpdf','font/');
- require('rpdf.php');
- include('phpqrcode/qrlib.php');
- class GerarEtiqueta extends TPage
- {
- private $form; // form
- /**
- * Class constructor
- */
- function __construct() {
- parent::__construct();
- // creates the form and a inner table
- $this->form = new TForm('form_gerar_pdf');
- $table = new TTable;
- $this->form->add($table);
- $matricula = new TEntry('matricula');
- $nome = new TEntry('nome');
- $data_admissao_i = new TDate('data_admissao_i');
- $data_admissao_f = new TDate('data_admissao_f');
- $data_demissao_i = new TDate('data_demissao_i');
- $data_demissao_f = new TDate('data_demissao_f');
- $nome->setSize('100%');
- $data_admissao_i->setSize('60%');
- $data_admissao_f->setSize('60%');
- $data_demissao_i->setSize('60%');
- $data_demissao_f->setSize('60%');
- // creates an action button
- $file = new TFile('file');
- $save_button=new TButton('generate');
- $save_button->setAction(new TAction(array($this, 'onPDF')), 'Gerar');
- $save_button->setImage('ico_save.png');
- $file->setSize('100%');
- //$file->setAllowedExtensions( ['txt','csv'] );
- // add a row for the form action
- $table->addRowSet(new TLabel('Matrícula'), $matricula);
- $table->addRowSet(new TLabel('Nome'), $nome);
- $table->addRowSet(new TLabel('Data Admissão Inicial'), $data_admissao_i, new TLabel('Data Admissão Final'), $data_admissao_f);
- $table->addRowSet(new TLabel('Data Demissão Inicial'), $data_demissao_i, new TLabel('Data Demissão Final'), $data_demissao_f);
- //$table->addRowSet($file);
- $table->addRowSet($save_button);
- // define wich are the form fields
- $this->form->setFields([ $matricula ] );
- $this->form->setFields([ $nome ] );
- $this->form->setFields([ $data_admissao_i ] );
- $this->form->setFields([ $data_admissao_f ] );
- $this->form->setFields([ $data_demissao_i ] );
- $this->form->setFields([ $data_demissao_f ] );
- //$this->form->setFields( [ new TLabel('TFile') ], [ $file ] );
- $this->form->setFields(array($save_button));
- // wrap the page content using vertical box
- $vbox = new TVBox;
- $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $vbox->add($this->form);
- parent::add($vbox);
- }
- /**
- * method onGenerate()
- * Executed whenever the user clicks at the generate button
- */
- function onPDF($param) {
- try {
- TTransaction::open('permission');
- $matricula = $param['matricula'];
- $nome = $param['nome'];
- $data_admissao_i = $param['data_admissao_i'];
- $data_admissao_f = $param['data_admissao_f'];
- $data_demissao_i = $param['data_demissao_i'];
- $data_demissao_f = $param['data_demissao_f'];
- if($data_admissao_i == ''){
- $date_a_i ='1500-01-01 00:00:00';
- } else {
- $date_a_i = new DateTime($data_admissao_i);
- $date_a_i = $date_a_i->format('Y-m-d H:i:s');
- }
- if($data_admissao_f == ''){
- $date_a_f = date('Y-m-d H:i:s');
- } else {
- $date_a_f = new DateTime($data_admissao_f);
- $date_a_f = $date_a_f->format('Y-m-d H:i:s');
- }
- if($data_demissao_i == '') {
- $date_d_i ='1500-01-01 00:00:00';
- } else {
- $date_d_i = new DateTime($data_demissao_i);
- $date_d_i = $date_d_i->format('Y-m-d H:i:s');
- }
- if($data_demissao_f == '') {
- $date_d_f = date('Y-m-d H:i:s');
- } else {
- $date_d_f = new DateTime($data_demissao_f);
- $date_d_f = $date_d_f->format('Y-m-d H:i:s');
- }
- $criteria = new TCriteria;
- $criteria->add(new TFilter('nome', 'like', "%{$nome}%"));
- $criteria->add(new TFilter('data_admissao','BETWEEN',$date_a_i, $date_a_f));
- $criteria->add(new TFilter('data_demissao','BETWEEN', $date_d_i,$date_d_f));
- $criteria->setProperties(array('order' => 'data_demissao', 'direction', 'asc'));
- $objetos = Funcionario::getObjects($criteria);
- if($objetos != null){
- //Gera o PDF
- $pdf = new RPDF('L','cm',array(8.5,5.4));
- foreach ($objetos as $tupla) {
- $pdf->AddPage();
- $pdf->SetFont('Arial','',10);
- $this->onGenerate($tupla, $pdf);
- $date = date('d-m-YHi');
- $file = 'app/output/envelope'.$date.'.pdf';
- }
- if (!file_exists($file) OR is_writable($file)) {
- $pdf->output($file);
- parent::openFile($file);
- }
- else {
- throw new Exception(_t('Permission denied') . ': ' . $file);
- }
- new TMessage('info', 'Relatório gerado Por favor, habilite os popups no navegador.');
- }
- else {
- new TMessage('error', 'Favor inserir o documento para geração dos dados.');
- }
- TTransaction::close();
- }
- catch (Exception $e) { // in case of exception
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- }
- }
- function onGenerate($tupla,$pdf)
- {
- $cartao='';
- $registro = $tupla->matricula;
- $nome = $tupla->nome;
- $dataAdmissao = new DateTime($tupla->data_admissao);
- $dataQuitacao = new DateTime($tupla->data_demissao);
- $dataAdmissao = $dataAdmissao->format('d/m/Y');
- $dataQuitacao = $dataQuitacao->format('d/m/Y');
- $cartao .= 'NOME:'.$registro."\n";
- $cartao .= 'MATRICULA:'.$nome."\n";
- $cartao .= 'DATAADMISSAO:'.$dataAdmissao."\n";
- $cartao .= 'DATAQUITACAO:'.$dataQuitacao."\n";
- QRcode::png($cartao, 'imgQRCode\\'.$registro.'.png', QR_ECLEVEL_L , 1);
- // $w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link=''
- $pdf->Cell(6,.47,'IDENTIFICACAO DO ESTUDANTE','',1,'R');
- $pdf->Cell(2,.47,$nome,'',1,'R');
- $pdf->Cell(2,.47,$registro,'',1,'L');
- $pdf->Cell(2,.47,$dataAdmissao,'',1,'L');
- $pdf->Cell(2,.47,$dataQuitacao,'',1,'L');
- // $file, $x=null, $y=null, $w=0, $h=0, $type='', $link=''
- $pdf->Image('app\images\new_user.png', 6.5, 1.5, 1.7, 1.9,'png');
- $pdf->Image('app\images\escola_amelia.png', 0, 0, 1.6, 5.4,'png');
- $pdf->Image('app\images\prefeitura.png', 2, 3.9, 1.7, 1,'png');
- $pdf->Image('imgQRCode\\'.$registro.'.png', 7, 3.7, 1.3, 1.3,'png');
- }
- }
Na função Image você define "X" e "Y" diretamente na função. Na função "Cell" não funciona dessa forma, você precisa definir o posicionamento com "setXY" antes de chamá-la.