2
I am working on a JSF page and I need to download a .pdf. When downloading the object is changed and I need to update it as I cannot allow the same download more than once.
But if I use <f:ajax>
download does not work.
<h:commandLink actionListener="#{repositorioCobrancaAddMB.clear}"
title="${messages['label.print']}"
rendered="#{repositorioCobrancaAddMB.podeGeraBoleto(bean)}"
class="btn btn-white">
<span class="fa fa-file-pdf-o"/>
<f:ajax event="click" execute="@this" render="@all" listener="#{repositorioCobrancaAddMB.geraBoleto(bean)}"/>
</h:commandLink>
Without using ajax the download happens, but does not update my page!
<h:commandLink action="#{repositorioCobrancaAddMB.geraBoleto(bean)}"
actionListener="#{repositorioCobrancaAddMB.clear}"
title="${messages['label.print']}"
rendered="#{repositorioCobrancaAddMB.podeGeraBoleto(bean)}"
class="btn btn-white">
<span class="fa fa-file-pdf-o"/>
</h:commandLink>
Method defining whether or not the user can generate the billet and billet generation methods.
public boolean podeGeraBoleto(Titulo bean) {
return tituloBC.podeGeraBoleto(bean);
}
public String geraBoleto(Titulo bean) {
// return "titulo_list.jsf";
TituloTO tituloTO = tituloBC.geraTituloTO(bean);
if (bean.getNossoNumero() == null || bean.getNossoNumero().isEmpty()) {
tituloBC.geraNossoNumero(bean, tituloTO);
} else {
tituloTO.buildNossoNumeroTO();
}
if (tituloTO.getNossoNumeroTO() == null) {
getMessageContext().add(getResourceBundle().getString("banco.msg.null"), SeverityType.WARN);
return null;
}
byte[] pdf = GeraBoleto.gera(tituloTO);
this.geraPDF(pdf, bean);
return null;
}
public void geraPDF(byte[] pdf, Titulo bean) {
if (pdf == null) {
return;
}
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=" + bean.getNumero() + " - " + bean.getParcela() + ".pdf");
try {
OutputStream out = response.getOutputStream();
out.write(pdf);
out.flush();
out.close();
} catch (IOException ex) {
CriareLog.log(ex);
}
}
Titulobc
public boolean podeGeraBoleto(Titulo bean) {
if (bean.getSituacao().isLiquidado()) {
return false;
}
return bean.getPortador().getBanco().isCobranca() && bean.getPortador().getBloquetoEmissao().isCliente();
}
Someone would know how to download and, at the end of it, refresh the page?
But does it have to be at the end of the download? It can’t be sooner?
– Rogers Corrêa
It is that during the download process I change some information of the boleto, in this case, I inform that it has already had the boleto generated and already has banking information. As long as this change happens, it can be both sooner and later.
– Gilvan André
You are using Jsf 2.0?
– Maicon Carraro
I am using JSF version 2.2.11
– Gilvan André
@Gilvanandré Keep up the trouble?
– Maicon Carraro
Good morning... sorry it took me so long to answer. This problem is occurring in my work, and where I live is holiday on Monday, so I spent 3 days without having anything to do! I am resuming activities today. Even so thank you for the answers and again I apologize!
– Gilvan André