简体   繁体   中英

Displaying jasper report generated by a web service

I have a java desktop application that should display a jasper report which is obtained from a web service. The web service returns the byte array as such:

JasperDesign jasperDesign = JRXmlLoader.load(input);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametersMap, resultSetDataSource);
byte[] pdfByteArray = JasperExportManager.exportReportToPdf(jasperPrint);
return pdfByteArray;

How do I display the returned byte array in a JRViewer? Can I convert the byte array to a JasperPrint object? I cannot return a JasperPrint object from the web service because I get an "interface" error.

I finally found a solution to my problem. First, instead of returning a byte array from the web service I return a string which contains the xml file as such:

JasperDesign jasperDesign = JRXmlLoader.load(input);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametersMap, resultSetDataSource);
String xmlString = JasperExportManager.exportReportToXml(jasperPrint);
return xmlString;

Next in the client application, I take this string and convert it to a document. I got the following method from http://www.journaldev.com/1237/java-convert-string-to-xml-document-and-xml-document-to-string :

private static Document convertStringToDocument(String xmlStr) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
    DocumentBuilder builder;  
    try {  
        builder = factory.newDocumentBuilder();  
        Document doc = builder.parse( new InputSource( new StringReader( xmlStr ) ) ); 
        return doc;
    } catch (Exception e) {  
        e.printStackTrace();  
    } 
    return null;
}

Next I save the document to the local disk as such:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(new File("theFile.xml"));
Source input = new DOMSource(doc);
transformer.transform(input, output);

and finally I displayed the newly saved xml using the JRViewer constructor:

JRViewer view = new JRViewer("theFile.xml", true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM