Jump to content
We've recently updated our Privacy Statement, available here ×
  • Export Report on Executing in JasperReports Server UI Via REST API


    akovach
    • Features: Reports Version: v7.1, v7.1.0 Product: JasperReports® Server

    Requirement

    There might be a situation, when end user needs report export on report execution in JasperReports Server. It is possible to achieve with Rest API and a Scriptlet in a report.


    Solution

    Step 1

    Lets create a Scriptlet which will make export of the report. For this we need to create a java file with the next code:

    [toc]
    package com.scriptlet.export;
    
    import net.sf.jasperreports.engine.JRDefaultScriptlet;
    import net.sf.jasperreports.engine.JRScriptletException;
    
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class Scriptlet extends JRDefaultScriptlet {
        public void afterReportInit() throws JRScriptletException {
            try {
                URL obj = new URL("http://HOST:PORT/jasperserver-pro/rest_v2/reports/path/to/report/report.pdf?j_username=UserName&j_password=UserPassword");
                HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
                connection.setRequestProperty("Accept", "application/json");
                connection.setRequestMethod("GET");
                connection.setDoOutput(true);
                connection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    Step 2

    Then we need to compile this file and create a jar library with its class. You can use Eclipse or Intellij Idea for this.

    Step 3

    After this we should add this library to classpath of the project in Jaspersoft Studio:

    Right Click on "My Reports" Project > Build Path > Configure Build Path > Libraries > Add External Libraries >(your jar file)

    Step 4

    After this we can create a report and create a scriptlet there. For this go to "Scriptlets" section from "Outline" window > Right Click on "Scriplets" and then  click on "Create Scriptlet" > Give Name="Scriptlet" and Class="com.scriptlet.export.Scriptlet"  (with out double quotes) > Observe that as quick as you create Scriptlet, Jasper Studio engine creates a parameter

               in  "Parameters" node. Here it will create "Scriptlet_SCRIPTLET"

    Now, create a TextField with the following expression:

    $P{Scriptlet_SCRIPTLET}.afterReportInit()
    

    Step 5

    Publish report to the server and run it. You should get pdf export along with report output in JasperReports Server UI.


    References

    Reports Services of REST API

    Scriptlet Tutorial

    Simple Scriptlet Example


    ref case - akovach 01673546 

    scriptlet.jar


    User Feedback

    Recommended Comments

    There are no comments to display.



    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...