Oies!
Tenho um botão dentro de um modal que deveria gerar um pdf. Porém, ao usar a4j, o pdf é gerado somente com caracteres desconfigurado.
Tentei usar h:a4j:commandButton, mas sequer o método é invocado.
Botão do modal:
<a4j:commandButton id="pdfConclusaoButton" value="#{m['imprimir']}" immediate="true" actionListener="#{cotacaoPendenteBean.doGerarPdf}" />
ou
<h:commandButton id="pdfConclusaoButton" value="#{m['imprimir']}" immediate="true" actionListener="#{cotacaoPendenteBean.doGerarPdf}" />
Meu Bean:
public void doGerarPdf(ActionEvent ae) {
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse res = (HttpServletResponse) facesContext.getExternalContext().getResponse();
BufferedOutputStream bos = new BufferedOutputStream(res.getOutputStream());
List<OutputStream> bosList = new ArrayList<OutputStream>();
...
byte[] outputBytes = ReportUtil.mergePdf(bosList);
res.setContentType("application/pdf");
res.setHeader("Pragma", "public");
res.setHeader("Cache-control", "must-revalidate");
res.setHeader("Content-Disposition", "attachment; filename=\"Impressao.pdf\"");
res.setContentLength(outputBytes.length);
bos.write(outputBytes, 0, outputBytes.length);
bos.flush();
bos.close();
}
ReportUtil
/**
* Método responsável em realizar o merge entre arquivos pdf
*
* @param pdfData
* @return
* @throws com.lowagie.text.pdf.BadPdfFormatException
* @throws java.io.IOException
* @throws com.lowagie.text.DocumentException
*/
public static byte[] mergePdf(final List<OutputStream> pdfData) throws BadPdfFormatException, IOException, DocumentException {
byte[] outputBytes = null;
PdfCopy writer = null;
Document document = null;
ByteArrayOutputStream baos = null;
for (int i = 0; i < pdfData.size(); i++) {
byte[] pdfBytes = ((ByteArrayOutputStream) pdfData.get(i)).toByteArray();
PdfReader pdfReader = new PdfReader(pdfBytes);
pdfReader.consolidateNamedDestinations();
if (i == 0) {
document = new Document(pdfReader.getPageSizeWithRotation(1));
baos = new ByteArrayOutputStream();
writer = new PdfCopy(document, baos);
document.open();
}
copyPdfPages(pdfReader, writer);
}
if(document != null){
document.close();
outputBytes = baos.toByteArray();
}
return outputBytes;
}
/**
* Método para realização de cópia de páginas no arquivo pdf
*
* @param pdfReader
* @param writer
* @throws java.io.IOException
* @throws com.lowagie.text.pdf.BadPdfFormatException
*/
public static void copyPdfPages(final PdfReader pdfReader, final PdfCopy writer) throws IOException, BadPdfFormatException {
PdfImportedPage page;
int n = pdfReader.getNumberOfPages();
for (int i = 0; i < n;) {
++i;
page = writer.getImportedPage(pdfReader, i);
writer.addPage(page);
}
PRAcroForm form = pdfReader.getAcroForm();
if (form != null) {
writer.copyAcroForm(pdfReader);
}
}
Alguém sabe como resolver?
Tks!!