Jump to content
We've recently updated our Privacy Statement, available here ×

Export report to Image (JPG, BMP or any Format)


mnshah9

Recommended Posts

Hi All

 

I want to export report to any image format, My report is PAI Chart or any other chart i want to export that as image. Is it possible though JasperReport? if yes how i can implement it

 

I tried it with JRGraphics2DExporter but it won't wrok. Can u pls help me.

 

Thanks

Maulik

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Here's an incomplete part of code I use in our application:

Code:

zoom = (float)1.6;
logger.info("Rendering images (format "+new String(format)+"«») zoom: "+new Float(zoom).toString());

/* To get a correct by OS image Writer */
Iterator writers = ImageIO.getImageWritersByFormatName(new String(format));
ImageWriter writer = (ImageWriter)writers.next();
JasperPrintManager printManager = new JasperPrintManager();
BufferedImage[] rendered_images = new BufferedImage[(jasperPrint.getPages().size())];
ByteArrayOutputStream baos = new ByteArrayOutputStream();

output = new byte[(jasperPrint.getPages().size())][];
for(int i = 0; i < jasperPrint.getPages().size(); i++)
{
try {
rendered_images = (BufferedImage)printManager.printPageToImage(jasperPrint, i, zoom);
} catch (JRException e) {
String message = "Unable to export report to pdf.";
logger.error(message, e);
throw new JasperFillingException(message);
}

writer.setOutput(new MemoryCacheImageOutputStream(baos));

try {
writer.write(new IIOImage(rendered_images, null, null));
} catch (IOException e) {
String message = "Unable to write image to the given output stream...";
logger.error(message, e);
throw new JasperServerException(message);
}

output = baos.toByteArray();
baos.reset();
writer.reset();
}
logger.info("Rendered Images: "+ jasperPrint.getPages().size());

 

Enjoy,

Link to comment
Share on other sites

  • 6 years later...

Yes!! last answer works!!!!! and you can scale the image without loss of resolution and quality.  I used JRGraphics2DExporter, but it doesn work using the zoom options, but using JasperPrintManager it works!!!!!!

I have scaled the image to 1.6

Here is my code based on last post but updated to the new Jasperreports versions (5.5.0 in my case)

 

JasperPrint is the compiled jrxml with data filled into the report

.....JasperReport jasperReport = compileReport("myReport.jrxml");parameters.put("title", "MyReport");.....Fill Data........print = JasperFillManager.fillReport(jasperReport, parameters, new JRBeanCollectionDataSource(data));[/code]

FilePath is the destination where the image report will be written.

private void extractPrintImage (String filePath, JasperPrint print){           File file = new File(filePath);       OutputStream ouputStream= null;         try{           ouputStream= new FileOutputStream(file);             DefaultJasperReportsContext.getInstance();           JasperPrintManager printManager = JasperPrintManager.getInstance(DefaultJasperReportsContext.getInstance());                      BufferedImage rendered_image = null;              rendered_image = (BufferedImage)printManager.printPageToImage(print, 0,1.6f);         ImageIO.write(rendered_image, "png", ouputStream);          }catch(Exception e){       e.printStackTrace();  }    }[/code]

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...