I'm learning a little about jasper / ireport and I'm having some trouble generating a report with an image.
I followed some tutorials I found on the internet but I'm making some mistake that I can not see. My test report is extremely simple. Contains only one image. I am compiling the report and passing my image by parameter, however when I generate the pdf from the report what appears is
"java.io.ByteArrayInputStream@6df0eb9d"
See my code:
public class ReportGenerator {
/**
* gera o relatório.
* @param image {@link BufferedImage}
*/
public void generateReport(BufferedImage image){
try {
InputStream input = toStream(image);
Projeto projeto = new Projeto();
projeto.setImage(input);
List<Projeto> lista = new ArrayList<Projeto>();
lista.add(projeto);
Map parameter = new HashMap();
parameter.put("IMAGE",projeto.getImage());
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("report1.jrxml");
JasperReport jasper = JasperCompileManager.compileReport(inputStream);
JasperPrint print = JasperFillManager.fillReport(jasper, parameter, new JRBeanCollectionDataSource(lista));
JasperExportManager.exportReportToPdfFile(print, "RelatorioClientes.pdf");
} catch (JRException e) {
e.printStackTrace();
}
}
/**
* Converte um {@link BufferedImage} em {@link InputStream}
* @param image {@link BufferedImage}
* @return {@link InputStream}
*/
private InputStream toStream(BufferedImage image){
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "png", bos);
byte[] vetor = bos.toByteArray();
InputStream input = new ByteArrayInputStream(vetor);
return input;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
This project class has only one inputStream representing the image, and its get / set methods.
In ireport I created an image attribute and in its expression class I define it as InputStream. and in the image expression I define the IMAGE parameter I created.
Can anyone help me?
Thank you
PS: I'm adding prints the way I set up the report in ireport