package siprp.ficha; import java.awt.Color; import java.io.*; import java.util.*; import com.lowagie.text.*; import com.lowagie.text.pdf.*; import com.evolute.utils.*; import siprp.*; public class ExamePDF { private final Font FONT_BOLD = FontFactory.getFont( "Arial", 8, Font.BOLD ); private final Font FONT_NORMAL = FontFactory.getFont( "Arial", 8, Font.NORMAL ); private final Font FONT_ZAPFDINGBATS = new Font( Font.ZAPFDINGBATS , 12, Font.NORMAL, new Color( 0, 0, 0 ) ); private BaseFont BASE_WINGDINGS; private Font FONT_WINGDINGS; // static // { // try // { // //BASE_WINGDINGS = BaseFont.createFont("C:\\WINNT\\Fonts\\WINGDING.TTF", BaseFont.CP1252, BaseFont.NOT_EMBEDDED); // BASE_WINGDINGS = BaseFont.createFont("WingDings", BaseFont.CP1252, BaseFont.EMBEDDED); // InputStream stream = BaseFont.getResourceStream( "WING", ClassLoader loader) // BASE_WINGDINGS = BaseFont.createFont("WingDings", BaseFont.CP1252, BaseFont.EMBEDDED, // true, byte[] ttfAfm, null ); // FONT_WINGDINGS = new Font(BASE_WINGDINGS, 12); // } // catch( Exception ex ) // { // FONT_WINGDINGS = FONT_NORMAL; // System.out.println( "NO FONT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ); // } // } public ExamePDF() { FontFactory.registerDirectories(); String possibleNames[] = new String[]{ "Wingdings", "WingDings", "wingdings", "WINGDINGS" }; for( int n = 0; n < 4; n++ ) { FONT_WINGDINGS = FontFactory.getFont( possibleNames[ n ] ); if( FONT_WINGDINGS.getFamilyname().toLowerCase().equals( "wingdings" ) ) { break; } } } public static void main( String[] args ) { try { Hashtable ht = new Hashtable(); // designacao_social // estabelecimentos.nome // estabelecimentos.localidade // servico_saude_tipo_interno // servico_saude_tipo_interempresas // servico_saude_tipo_externo // servico_saude_tipo_sns // servico_saude_designacao // servico_higiene_tipo_interno // servico_higiene_tipo_interempresas // servico_higiene_tipo_externo // servico_higiene_outro // servico_higiene_designacao // trabalhadores.nome // sexo // data_nascimento // nacionalidade // numero_mecanografico // data_admissao // categoria // local_trabalho // funcao_proposta // data_admissao_funcao // observacoes // exames.data // tipo_admissao // tipo_periodico // tipo_ocasional // tipo_apos_doenca // tipo_apos_acidente // tipo_pedido_trabalhador // tipo_pedido_empresa // tipo_mudanca_funcao // tipo_trabalho // tipo_outro // resultado_apto // resultado_apto_condicionalmente // resultado_inapto_temp // resultado_inapto_def // outra_funcao_1 // outra_funcao_2 // outra_funcao_3 // outra_funcao_4 // proximo_exame // outras_recomendacoes // medicos.nome // numero_cedula ht.put( "trabalhadores.nome", "Trabalhador do comércio" ); ht.put( "servico_saude_designacao", "Designacao servico saude" ); ht.put( "servico_saude_tipo_externo", Boolean.TRUE ); ht.put( "tipo_periodico", Boolean.TRUE ); ht.put( "resultado_inapto_temp", Boolean.TRUE ); FileOutputStream fos = new FileOutputStream( System.getProperty( "user.home" ) + "\\report.pdf" ); fos.write( new ExamePDF().createPDF( ht ) ); fos.close(); System.out.println( "File saved." ); Process proc = Runtime.getRuntime().exec( "cmd.exe /c \"" + System.getProperty( "user.home" ) + "\\report.pdf\"" ); proc.waitFor(); new File( System.getProperty( "user.home" ) + "\\report.pdf" ).delete(); System.out.println( "Done." ); } catch( Exception e ) { e.printStackTrace(); } } public void print( byte []pdf ) throws Exception { long time = System.currentTimeMillis(); FileOutputStream fos = new FileOutputStream( System.getProperty( "user.home" ) + System.getProperty( "file.separator" ) + "report_ficha" + time + ".pdf" ); fos.write( pdf ); fos.close(); System.out.println( "File saved." ); Process proc; if( System.getProperty( "os.name" ).startsWith( "Windows" ) ) { //System.out.println( "cmd.exe /c \"" + System.getProperty( "user.home" ) + System.getProperty( "file.separator" ) + "report_ficha.pdf\"" ); proc = Runtime.getRuntime().exec( "cmd.exe /c \"" + System.getProperty( "user.home" ) + System.getProperty( "file.separator" ) + "report_ficha" + time + ".pdf\"" ); proc.waitFor(); if( !new File( System.getProperty( "user.home" ) + System.getProperty( "file.separator" ) + "report_ficha" + time + ".pdf" ).delete() ) { System.err.println( "File: report_ficha" + time + ".pdf - NOT DELETED" ); } } else { //System.out.println( "/usr/bin/open \"" + System.getProperty( "user.home" ) + System.getProperty( "file.separator" ) + "report_ficha.pdf\"" ); try{ proc = Runtime.getRuntime().exec( new String[]{"/usr/bin/open", "" + System.getProperty( "user.home" ) + System.getProperty( "file.separator" ) + "report_ficha" + time + ".pdf" } ); } catch( Exception ex ) { ex.printStackTrace(); } // proc = Runtime.getRuntime().exec( "/usr/bin/open \"/Users/Shared/teste.pdf.pdf\"" ); } } public byte[] createPDF( Hashtable values ) { Document document = new Document(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); document.setPageSize( PageSize.A4 ); try { PdfWriter pdfw = PdfWriter.getInstance( document, bos ); document.addTitle( "Ficha de Aptid\u00e3o" ); String acronym = (String) Singleton.getInstance( SingletonConstants.COMPANY_ACRONYM ); document.addAuthor( acronym != null ? acronym: "n/a" ); document.addCreator( "Evolute" ); document.open(); Paragraph conteudo = new Paragraph(); conteudo.add( new Chunk( "\n\nFICHA DE APTID\u00c3O\n", FontFactory.getFont( "Arial", 10, Font.BOLD ) ) ); conteudo.add( new Chunk( "(Portaria n\u00ba1031/2002, de 10 de Agosto)", FontFactory.getFont( "Arial", 7, Font.BOLD ) ) ); conteudo.setAlignment( Element.ALIGN_CENTER ); document.add( conteudo ); Table table = new Table( 1 ); table.setBorderWidth( 1 ); table.setPadding( 5 ); table.setOffset( 0 ); table.setWidth( 100 ); table.addCell( new Phrase( "Empresa/Entidade", FONT_BOLD ) ); StringBuffer texto = new StringBuffer(); Phrase ph = new Phrase( 12f ); Cell cell = new Cell(); texto.append( "DESIGNA\u00c7\u00c3O SOCIAL: " + values.get( "designacao_social" ) + "\n" ); texto.append( "ESTABELECIMENTO: " + values.get( "estabelecimentos.nome" ) + " LOCALIDADE: " + values.get( "estabelecimentos.localidade" ) + "\n" ); texto.append( "SERVI\u00c7O DE SA\u00DaDE: Tipo " ); ph.add( new Chunk( texto.toString(), FONT_NORMAL ) ); texto = new StringBuffer( "Interno" ); ph.add( doCheckedPhrase( texto.toString(), values.get( "servico_saude_tipo_interno" ) != null ) ); texto = new StringBuffer( "Interempresas" ); ph.add( doCheckedPhrase( texto.toString(), values.get( "servico_saude_tipo_interempresas" ) != null, true ) ); texto = new StringBuffer( "Externo" ); ph.add( doCheckedPhrase( texto.toString(), values.get( "servico_saude_tipo_externo" ) != null, true ) ); texto = new StringBuffer( "Servi\u00e7o Nacional de Sa\u00fade\n" ); ph.add( doCheckedPhrase( texto.toString(), values.get( "servico_saude_tipo_sns" ) != null, true ) ); texto = new StringBuffer(); texto.append( "DESIGNA\u00c7\u00c3O: " + values.get( "servico_saude_designacao" ) + "\n" ); texto.append( "SERVI\u00c7O DE HIGIENE E SEGURAN\u00c7A: Tipo " ); ph.add( new Chunk( texto.toString(), FONT_NORMAL ) ); texto = new StringBuffer( "Interno" ); ph.add( doCheckedPhrase( texto.toString(), values.get( "servico_higiene_tipo_interno" ) != null ) ); texto = new StringBuffer( "Interempresas" ); ph.add( doCheckedPhrase( texto.toString(), values.get( "servico_higiene_tipo_interempresas" ) != null, true ) ); texto = new StringBuffer( "Externo" ); ph.add( doCheckedPhrase( texto.toString(), values.get( "servico_higiene_tipo_externo" ) != null, true ) ); texto = new StringBuffer( "Outro\n" ); ph.add( doCheckedPhrase( texto.toString(), values.get( "servico_higiene_outro" ) != null, true ) ); texto = new StringBuffer(); texto.append( "DESIGNA\u00c7\u00c3O: " + values.get( "servico_higiene_designacao" ) + "" ); ph.add( new Chunk( texto.toString(), FONT_NORMAL ) ); cell.addElement( ph ); table.addCell( cell ); document.add( table ); table = new Table( 1 ); table.setBorderWidth( 1 ); table.setPadding( 5 ); table.setOffset( 0 ); table.setWidth( 100 ); table.addCell( new Phrase( "Trabalhador", FONT_BOLD ) ); texto = new StringBuffer(); texto.append( "NOME: " + values.get( "trabalhadores.nome" ) + "\n" ); texto.append( "SEXO: " + values.get( "sexo" ) + " DATA DE NASCIMENTO: " + values.get( "data_nascimento" ) + " NACIONALIDADE: " + values.get( "nacionalidade" ) + "\n" ); texto.append( "N\u00daMERO MECANOGR\u00c1FICO/OUTRO: " + values.get( "numero_mecanografico" ) + " DATA DE ADMISS\u00c3O: " + values.get( "data_admissao" ) + "\n" ); texto.append( "CATEGORIA PROFISSIONAL: " + values.get( "categoria" ) + " LOCAL DE TRABALHO: " + values.get( "local_trabalho" ) + "\n" ); texto.append( "FUN\u00c7\u00c3O PROPOSTA: " + values.get( "funcao_proposta" ) + " DATA DE ADMISS\u00c3O NA FUN\u00c7\u00c3O: " + values.get( "data_admissao_funcao" ) + "" ); table.addCell( new Phrase( 12f, texto.toString(), FONT_NORMAL ) ); document.add( table ); table = new Table( 1 ); table.setBorderWidth( 1 ); table.setOffset( 0 ); table.setPadding( 5 ); table.setWidth( 100 ); table.addCell( new Phrase( "Observa\u00e7\u00f5es", FONT_BOLD ) ); texto = new StringBuffer(); texto.append( values.get( "observacoes" ) ); table.addCell( new Phrase( 12f, texto.toString(), FONT_NORMAL ) ); document.add( table ); table = new Table( 2 ); table.setBorderWidth( 1 ); table.setOffset( 0 ); table.setPadding( 5 ); table.setWidth( 100 ); cell = new Cell( new Phrase( "Exame M\u00e9dico", FONT_BOLD ) ); cell.setColspan( 2 ); table.addCell( cell ); texto = new StringBuffer(); ph = new Phrase( 12f ); texto.append( "DATA DO EXAME: " + values.get( "exames.data" ) + "\n" ); texto.append( "TIPO\n" ); ph.add( new Chunk( texto.toString(), FONT_NORMAL ) ); ph.add( doCheckedPhrase( "ADMISS\u00c3O\n", values.get( "tipo_admissao" ) != null ) ); ph.add( doCheckedPhrase( "PERI\u00d3DICO\n", values.get( "tipo_periodico" ) != null ) ); ph.add( doCheckedPhrase( "OCASIONAL\n", values.get( "tipo_ocasional" ) != null ) ); ph.add( doCheckedPhrase( "AP\u00d3S DOEN\u00c7A\n", values.get( "tipo_apos_doenca" ) != null, true ) ); ph.add( doCheckedPhrase( "AP\u00d3S ACIDENTE\n", values.get( "tipo_apos_acidente" ) != null, true ) ); ph.add( doCheckedPhrase( "A PEDIDO DO TRABALHADOR\n", values.get( "tipo_pedido_trabalhador" ) != null, true ) ); ph.add( doCheckedPhrase( "A PEDIDO DO SERVI\u00c7O\n", values.get( "tipo_pedido_empresa" ) != null, true ) ); ph.add( doCheckedPhrase( "POR MUDAN\u00c7A DE FUN\u00c7\u00c3O\n", values.get( "tipo_mudanca_funcao" ) != null, true ) ); ph.add( doCheckedPhrase( "POR ALTERA\u00c7\u00c3O DAS CONDI\u00c7\u00d5ES DE TRABALHO\n", values.get( "tipo_trabalho" ) != null, true ) ); ph.add( doCheckedPhrase( "OUTRO\n", values.get( "tipo_outro" ) != null, true ) ); texto = new StringBuffer(); texto.append( " ESPECIFIQUE: " + ( ( values.get( "tipo_outro" ) == null ) ? "" : values.get( "tipo_outro" ) ) + "" ); ph.add( new Chunk( texto.toString(), FONT_NORMAL ) ); table.addCell( ph ); ph = new Phrase( 12f ); ph.add( new Chunk( "RESULTADO\n", FONT_NORMAL ) ); ph.add( doCheckedPhrase( "APTO\n", values.get( "resultado_apto" ) != null ) ); ph.add( doCheckedPhrase( "APTO CONDICIONALMENTE\n\n", values.get( "resultado_apto_condicionalmente" ) != null ) ); ph.add( doCheckedPhrase( "INAPTO TEMPORARIAMENTE\n", values.get( "resultado_inapto_temp" ) != null ) ); ph.add( doCheckedPhrase( "INAPTO DEFINITIVAMENTE\n\n", values.get( "resultado_inapto_def" ) != null ) ); texto = new StringBuffer(); texto.append( "OUTRAS FUN\u00c7\u00d5ES QUE PODE DESEMPENHAR\n" ); texto.append( " 1 " + ( ( values.get( "outra_funcao_1" ) == null ) ? "" : values.get( "outra_funcao_1" ) ) + "\n" ); texto.append( " 2 " + ( ( values.get( "outra_funcao_2" ) == null ) ? "" : values.get( "outra_funcao_2" ) ) + "\n" ); texto.append( " 3 " + ( ( values.get( "outra_funcao_3" ) == null ) ? "" : values.get( "outra_funcao_3" ) ) + "\n" ); texto.append( " 4 " + ( ( values.get( "outra_funcao_4" ) == null ) ? "" : values.get( "outra_funcao_4" ) ) + "" ); ph.add( new Chunk( texto.toString(), FONT_NORMAL ) ); table.addCell( ph ); document.add( table ); table = new Table( 1 ); table.setBorderWidth( 1 ); table.setOffset( 0 ); table.setPadding( 5 ); table.setWidth( 100 ); table.addCell( new Phrase( "Outras Recomenda\u00e7\u00f5es", FONT_BOLD ) ); texto = new StringBuffer(); if( ( (Boolean) Singleton.getInstance( SingletonConstants.FICHA_MARCA_EXAMES ) ).booleanValue() ) { System.out.println( "ficha marca exames" ); texto.append( "PR\u00d3XIMO EXAME: " + values.get( "proximo_exame" ) + "\n" ); } texto.append( "" + values.get( "outras_recomendacoes" ) + "" ); table.addCell( new Phrase( 12f, texto.toString(), FONT_NORMAL ) ); document.add( table ); table = new Table( 1 ); table.setBorderWidth( 1 ); table.setOffset( 0 ); table.setPadding( 5 ); table.setWidth( 100 ); texto = new StringBuffer(); texto.append( "M\u00c9DICO DO TRABALHO: " + values.get( "medicos.nome" ) + " C.P. " + values.get( "numero_cedula" ) + "\n" ); texto.append( "ASSINATURA _____________________________________________________________________________________\n\n" ); texto.append( "TOMEI CONHECIMENTO ___________________________________________________________ DATA:____/____/________\n" ); texto.append( " O RESPONS\u00c1VEL DOS RECURSOS HUMANOS" ); table.addCell( new Phrase( 18f, texto.toString(), FONT_NORMAL ) ); document.add( table ); } catch( Exception e ) { e.printStackTrace(); return null; } document.close(); return bos.toByteArray(); } private Phrase doCheckedPhrase( String text, boolean checked, boolean indent ) { Phrase p = new Phrase( 12f ); //p.add( new Chunk( ( indent ? " " : "" ) + ( char )( checked? 110: 111) + " ", FONT_ZAPFDINGBATS ) ); //System.out.println( "FAMILY: " + FONT_WINGDINGS.getFamilyname() ); p.add( new Chunk( ( indent ? " " : "" ) + ( char )( checked? 0xfe: 0xa8), FONT_WINGDINGS ) ); // p.add( new Chunk( ( indent ? " " : "" ) + ( char )( checked? 'X': '_'), FONT_WINGDINGS ) ); p.add( new Chunk( text, FONT_NORMAL ) ); return p; } private Phrase doCheckedPhrase( String text, boolean checked ) { return doCheckedPhrase( text, checked, false ); } }