0
I have to print out a Jasper report with attachments, which are images and other Pdfs. My approach is to pass this pdf from Jasper to iText and then merge with the attachments. For the method that does the printing, I pass the data to the report and a list with the path of each attached file. But when I print, if I print only the pdfs, ignoring the images, only the Jasper report is printed, the other Pdfs are not, if I print the PDF along with the images and the other Pdfs, this error is presented:
Grave: java.io.IOException: file:/home/juliana/docroot/arquivos/livro/2019/5/13/1094-17_(5).pdf is not a recognized imageformat.
at com.lowagie.text.Image.getInstance(Unknown Source)
at com.lowagie.text.Image.getInstance(Unknown Source)
Printing Method
public static void imprimirPdfComAnexos(Livro livro, List<ArquivoRelato> arquivosRelato) throws FileNotFoundException, DocumentException, JRException, BadElementException, MalformedURLException, IOException {
FacesContext context = FacesContext.getCurrentInstance();
String caminho = "/relatorios/";
String subPasta = caminho + "livro/";
String relatorio = subPasta + "livro.jasper";
List<Livro> livros = new ArrayList<Livro>();
livros.add(livro);
InputStream reportStream = context.getExternalContext().getResourceAsStream(relatorio);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("SUBREPORT_DIR", context.getExternalContext().getRealPath(subPasta) + File.separator);
map.put("BRASAO_DIR", context.getExternalContext().getRealPath(caminho) + File.separator);
List<InputStream> listPdfs = new ArrayList<InputStream>();
List<String> listImagem = new ArrayList<String>();
try {
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
// Resulting pdf
response.flushBuffer();
FacesContext.getCurrentInstance().responseComplete();
ServletOutputStream servletOutputStream = response.getOutputStream();
JRBeanCollectionDataSource fonteDados = new JRBeanCollectionDataSource(livro);
//JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, map, fonteDados);
InputStream reportInputStream = new ByteArrayInputStream(JasperRunManager.runReportToPdf(reportStream, map, fonteDados));
// Source pdfs
listPdfs.add(reportInputStream);
for (ArquivoRelato ar : arquivosRelato) {
if(ar.getNomeArquivo().substring(ar.getNomeArquivo().lastIndexOf("."), ar.getNomeArquivo().length()).equals("pdf")) {
listPdfs.add(new FileInputStream(new File(ar.getCaminhoArquivo())));
}
else {
listImagem.add(ar.getCaminhoArquivo());
}
}
OutputStream out = new FileOutputStream(new File(context.getExternalContext().getRealPath("/temp/") + "livroAnexos.pdf"));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, out);
document.open();
PdfContentByte cb = writer.getDirectContent();
// Image img = Image.getInstance(bytes)
for (InputStream in : listPdfs) {
PdfReader reader = new PdfReader(in);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
document.newPage();
//import the page from source pdf
PdfImportedPage page = writer.getImportedPage(reader, i);
//add the page to the destination pdf
cb.addTemplate(page, 0, 0);
}
}
for (String in : listImagem) {
document.newPage();
Image image = Image.getInstance(in);
image.setAbsolutePosition(0, 0);
image.setBorderWidth(0);
document.add(image);
}
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=livroAnexos.pdf");
OutputStream output = response.getOutputStream();
out.flush();
document.close();
out.close();
File arquivo = new File(context.getExternalContext().getRealPath("/temp/") + "livroAnexos.pdf");
output.write(org.apache.commons.io.IOUtils.toByteArray(new java.io.FileInputStream(arquivo)));
servletOutputStream.flush();
document.close();
servletOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
That was exactly it, I did not notice that I needed to compare to . extension and not just the extension. Thank you!
– Juliana Marques
Glad you decided! Good luck out there!
– finx