Right to Left Reporting:
Do you have a scenario where you need to build both left-to-right and right-to-left(used in languages like arabic) reports? People generally end up creating two different layouts - that looks like mirror images of each other. This would cause confusion, maintenance issue and it's not cost effective. You can instead use the jasper reports API to get this done.
Use the following two simple methods to get this working. You could add to this method for more effective mirroring:
public static void mirrorLayout(JasperDesign design){
int pageWidth = design.getPageWidth();
int rightMargin =
design.getRightMargin();
int leftMargin = design.getLeftMargin();
JRBand titleBand = design.getTitle();
JRElement[] titleBandElements =
titleBand.getElements();
mirrorDesign(titleBandElements,(pageWidth -
leftMargin - rightMargin));
design.setRightMargin(leftMargin);
design.setLeftMargin(rightMargin);
}
And the method mirrorDesign does the actual mirroring:
protected static void mirrorDesign(JRElement[] elements, int totalWidth) {Though JasperReports doesn't provide zipping option directly (because JasperReports is a Reporting tool, and Zipping the report doesn't lie within the project scope), a little tweek to your code can give you the desired output. Here's sample to zip the output that you get while exporting the report. Note: The same can be used for any type of export (Excel, PDF, RTF, etc). The code snippet shows zipping for PDF export:
for (int i = 0; i < elements.length; i++) {
JRElement element = (JRElement) elements[i];
int mirrorX = totalWidth - element.getX() - element.getWidth();
element.setX(mirrorX);
if (element instanceof JRFrame)
{
JRFrame frame = (JRFrame) element;
mirrorDesign(frame.getElements(), frame.getWidth());
}
Zipping the exported Reports:
response.setContentType("text/xml;charset=utf-8");
response.setHeader("content-type", "text/pdf; charset=UTF-8");
response.setHeader("Content-Disposition","attachment;
filename=\""+ zipFileName.toString() + ".zip\"");
JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setParameter(JRPdfExporterParameter.JASPER_PRINT,jasperPrint);
pdfExporter.setParameter(JRPdfExporterParameter.IS_128_BIT_KEY, Boolean.TRUE);
pdfExporter.setParameter(JRPdfExporterParameter.IS_COMPRESSED,Boolean.FALSE);
pdfExporter.setParameter(JRPdfExporterParameter.FORCE_SVG_SHAPES,
Boolean.TRUE);
pdfExporter.setParameter(JRPdfExporterParameter.FORCE_LINEBREAK_POLICY, Boolean.TRUE);
pdfExporter.setParameter(JRPdfExporterParameter.IGNORE_PAGE_MARGINS,Boolean.FALSE);
ZipOutputStream zos = new ZipOutputStream(out);
pdfExporter.setParameter( JRXlsExporterParameter.OUTPUT_STREAM, zos);
ZipEntry ze = new ZipEntry(fileName + ".pdf");
zos.putNextEntry(ze);
pdfExporter.exportReport();
zos.closeEntry();
zos.close();
out.close();
5 comments:
Very Useful... Thanks!
What madam! No new entries!
:)
Hi Shyam, gr8 to see u on my blog!!!
Enge shyam... time constraint... so i have almost stopped blogging... :)
Hey Sis..Lets rocks together...!
sure sisy... rockin already! ;)
Post a Comment