1
I am wanting to change the way the user views the system report I am developing, currently by clicking the button to view the report, is given the option for the user to download or open directly in the PDF viewer that is installed on the machine, but I’ve seen it on many web systems, in which it displays a screen identical to the pdf viewer, where it allows the same already see the report, without downloading itself and printing thus facilitating his life. I’ve tried several ways the last attempt the code looked like this:
Executioner
public class ExecutorRelatorio implements Work {
private String caminhoRelatorio;
private Map<String, Object> parametros;
private String nomeArquivoSaida;
InputStream arquivo;
public ExecutorRelatorio(String caminhoRelatorio, Map<String, Object> parametros, String nomeArquivoSaida) {
this.caminhoRelatorio = caminhoRelatorio;
this.parametros = parametros;
this.nomeArquivoSaida = nomeArquivoSaida;
this.parametros.put(JRParameter.REPORT_LOCALE, new Locale("pt", "BR"));
}
@Override
public void execute(Connection connection) throws SQLException {
try {
JasperReport jasperReport = JasperCompileManager
.compileReport(getClass().getResourceAsStream(this.caminhoRelatorio));
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametros, connection);
byte[] b = JasperExportManager.exportReportToPdf(jasperPrint);
arquivo = new ByteArrayInputStream(b);
arquivo.read();
} catch (Exception e) {
throw new SQLException("Erro ao executar relatório " + this.caminhoRelatorio, e);
}
}
public InputStream getArquivo() {
return arquivo;
}
public String getNomeArquivoSaida() {
return nomeArquivoSaida;
}
}
Reportariofichaemergenciabean
@Named
@ManagedBean
@SessionScoped
public class RelatorioFichaEmergenciaBean implements Serializable {
private static final long serialVersionUID = 1L;
private Long registro;
@Inject
private EntityManager manager;
@Inject
@ManagedProperty(value = "#{pdfMB}")
private PdfMB pdfMB;
public void emitir(ActionEvent event) {
UIParameter parameter = (UIParameter) event.getComponent().findComponent("registroId");
registro = Long.parseLong(parameter.getValue().toString());
Map<String, Object> parametros = new HashMap<>();
parametros.put("registro", registro);
ExecutorRelatorio executor = new ExecutorRelatorio("/relatorios/ficha_emergencia.jrxml", parametros,
"Ficha de Emergência.pdf");
pdfMB.gerar(executor.getArquivo(), executor.getNomeArquivoSaida());
Session session = manager.unwrap(Session.class);
session.doWork(executor);
}
public PdfMB getPdfMB() {
return pdfMB;
}
public void setPdfMB(PdfMB pdfMB) {
this.pdfMB = pdfMB;
}
}
Pdfmb
/**
*
* @author Weverton Reis
* Fórum: GUJ
*
*/
@ManagedBean
@SessionScoped
public class PdfMB implements Serializable {
private static final long serialVersionUID = 1L;
private StreamedContent pdf;
/**
* Esse método deixa o arquivo pronto para ser exibido na tela.
*
* @param arquivo O arquivo pronto para ser apresentado.
* @param nomeArquivo Nome do arquivo.
*/
public void gerar(InputStream arquivo, String nomeArquivo) {
pdf = new DefaultStreamedContent(arquivo, "application/pdf", nomeArquivo);
}
public StreamedContent getPdf() {
try {
if(pdf != null){
pdf.getStream().reset();
}
} catch (Exception e) {
//logger.error(e.getMessage(), e);
e.printStackTrace();
}
return pdf;
}
public void setPdf(StreamedContent pdf) {
this.pdf = pdf;
}
}
Page xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:pe="http://primefaces.org/ui/extensions">
<p:toolbar style="margin-top: 20px">
<p:toolbarGroup>
<p:button value="Novo" icon="ui-icon-circle-plus"
outcome="/atendimento/Atendimento" />
<p:commandButton value="Salvar" icon="ui-icon-disk" update="@form"
action="#{cadastroAtendimentoBean.salvar}" />
<p:button value="Pesquisar" icon="ui-icon-circle-zoomin"
outcome="/atendimento/PesquisaAtendimento" />
<p:commandButton value="Imprimir Fichas" icon="ui-icon-print"
oncomplete="PF('fichasPaciente').show()" process="@this"
disabled="#{cadastroAtendimentoBean.atendimento.cancelado}">
</p:commandButton>
<p:commandButton value="Imprimir Laudo de AIH" update="@form"
icon="ui-icon-print"
disabled="#{cadastroAtendimentoBean.atendimento.naoInternado}"
actionListener="#{relatorioLaudoAih.emitir}" ajax="false">
<f:param id="registroIdLaudo" name="registroId"
value="#{cadastroAtendimentoBean.atendimento.registro}" />
</p:commandButton>
</p:toolbarGroup>
<p:toolbarGroup align="right">
<p:commandButton value="Realizar Atendimento"
action="#{atenderAtendimentoBean.mudarParaEmAtendimento}"
disabled="#{cadastroAtendimentoBean.atendimento.naoAguardandoAtendimento}"
process="@form" update="@form" />
<p:commandButton value="Observação" icon="ui-icon-check"
action="#{observacaoAtendimentoBean.mudarParaObservacao}"
disabled="#{cadastroAtendimentoBean.atendimento.aguardandoAtendimento}"
process="@form" update="@form" />
<p:commandButton value="Internamento" icon="ui-icon-locked"
oncomplete="PF('subTelaInternamento').show()"
disabled="#{cadastroAtendimentoBean.atendimento.aguardandoAtendimento}"
process="@this" />
<p:button value="Alta" outcome="/saida/Saida" icon="ui-icon-home"
disabled="#{cadastroAtendimentoBean.atendimento.aguardandoAtendimento}">
<f:param id="saida" name="atendimento"
value="#{cadastroAtendimentoBean.atendimento.registro}" />
</p:button>
<p:button value="Transferir" icon="ui-icon-alert"
outcome="/transferencia/Transferencia"
disabled="#{cadastroAtendimentoBean.atendimento.aguardandoAtendimento}">
<f:param id="transferencia" name="atendimento"
value="#{cadastroAtendimentoBean.atendimento.registro}" />
</p:button>
<p:separator />
<p:commandButton value="Cancelar" icon="ui-icon-cancel"
process="@form" update="@form"
action="#{cancelarAtendimentoBean.mudarParaCancelado}" />
</p:toolbarGroup>
</p:toolbar>
<p:dialog header="Fichas do Paciente" widgetVar="fichasPaciente"
id="fichasPaciente" modal="true" draggable="false" resizable="false">
<p:commandButton value="Ficha de Atendimento" icon="ui-icon-print"
actionListener="#{relatorioFichaEmergenciaBean.emitir}"
update="@form, pgRelatorio" oncomplete="PF('viewRelatorio').show()">
<f:param id="registroId" name="registroId"
value="#{cadastroAtendimentoBean.atendimento.registro}" />
</p:commandButton>
<p:commandButton value="Ficha do Paciente" icon="ui-icon-print"
actionListener="#{relatorioFichaEmissaoAih.emitir}" update="@form"
ajax="false">
<f:param id="registroIdFicha" name="registroId"
value="#{cadastroAtendimentoBean.atendimento.registro}" />
</p:commandButton>
</p:dialog>
<p:dialog header="Tela de Internamento" widgetVar="subTelaInternamento"
id="subTelaInternamento" modal="true" draggable="false"
resizable="false" closable="true">
<p:panelGrid columns="2" style="width: 100%; margin-top: 20px"
columnClasses="rotulo, campo, rotulo, campo">
<p:outputLabel value="Tipo da Clínica" for="tipoClinica" />
<p:selectOneMenu id="tipoClinica"
value="#{cadastroAtendimentoBean.atendimento.clinica}">
<f:selectItem itemLabel="Selecione ..." itemDisabled="true" />
<f:selectItems value="#{cadastroTipoClinicaBean.listaClinicas}"
var="clinica" itemValue="#{clinica}"
itemLabel="#{clinica.descricao}" />
</p:selectOneMenu>
<p:outputLabel value="Tipo do Internamento" for="tipoInternamento" />
<p:selectOneMenu id="tipoInternamento"
value="#{cadastroAtendimentoBean.atendimento.tipoAtendimento}">
<f:selectItem itemLabel="Selecione ..." itemDisabled="true" />
<f:selectItem itemLabel="Urgência/Emergência" itemValue="02" />
<f:selectItem itemLabel="Eletivo" itemValue="01" />
</p:selectOneMenu>
<p:outputLabel value="Médico Responsável pelo internamento:"
for="medicoResponsavelExec" />
<p:autoComplete id="medicoResponsavelExec" size="40" dropdown="true"
value="#{cadastroAtendimentoBean.atendimento.profissionalMedicoExecutor}"
completeMethod="#{cadastroAtendimentoBean.completarProfissional}"
var="profissional" itemLabel="#{profissional.nome}"
itemValue="#{profissional}" forceSelection="true" />
</p:panelGrid>
<p:commandButton value="Internar Paciente"
action="#{internamentoAtendimentoBean.mudarParaInternamento}"
process="@form" update="@form"
onclick="PF('subTelaInternamento').close();" />
</p:dialog>
<p:dialog header="Teste" widgetVar="viewRelatorio" width="900px"
height="100%" id="viewRelatorio">
<h:panelGrid id="pgRelatorio" columns="1"
style="width: 100%; margin-top: 20px">
<pe:documentViewer id="view" style="width:100%; height:100%;"
value="#{pdfMB.pdf}" />
</h:panelGrid>
</p:dialog>
</ui:composition>
This way, no error appears in the console, the dialog is displayed but the PDF of the report does not appear, it is only the empty component. There’s something wrong I know, but I couldn’t identify where I’m going wrong.
This project uses JSF with CDI, this will be the problem.
– Fábio Luciano