How to deploy a report (jrxml file) through the Ja

2019-02-16 01:26发布

问题:

How to deploy a report (jrxml file) through the Jasper PHP/REST API to the Jasper Reports Server?

回答1:

For deploying Jasper reports on Jasper Server using Rest-API you can use following method:

Uploading JRXML file

JRXML_DATA=$(cat $PATHTOJRXMLFILE/$JRXML_FILE)

curl  -X POST $JASPER_REST_HOST/jasperserver/rest_v2/resources/reports \
-H "Content-Type:application/jrxml" \
-H "Content-Disposition:attachment; filename=test" \
-H "Content-Description:test file" \
-d "$JRXML_DATA" \
--user $JASPER_USERNAME:$JASPER_USERNAME

Creating ReportUnit

RESOURCEDESCRIPTOR_JSON=$(cat $REPORT/deployable/reportunit.json)

curl  -X POST $JASPER_REST_HOST/jasperserver/rest_v2/resources/reports \
-H "Content-Type:application/repository.reportUnit+json" \
-d "$RESOURCEDESCRIPTOR_JSON" \
--user $JASPER_USERNAME:$JASPER_PASSWORD

ResourceDescriptor for reportUnit example

{
    "uri": "/reports/test_report", 
    "label": "test_report", 
    "description": "description", 
    "permissionMask": "0",
    "version": "0" ,
    "alwaysPromptControls": "true",
    "controlsLayout": "popupScreen",
    "jrxml": {
        "jrxmlFileReference": {
            "uri": "/reports/test"
        }
    }
}

Resource Descriptor references

http://community.jaspersoft.com/documentation/jasperreports-server-web-services-guide/v56/v2-resource-descriptor-types



回答2:

To upload a jrxml file, create a ResourceDescriptor with PROP_HAS_DATA = true and insert the jrxml content in a multipart PUT request.

After a while of researching and investigating I got it running and developed a PHP class that's easy to use.

http://blog.flowl.info/2013/jasper-php-library-on-github/

To upload a jrxml file this code does the job:

// Init the Jasper connection
require_once('Jasper/Jasper.php');
$jasper = new \Jasper\Jasper();
$jasper->login('jasperadmin', 'jasperadmin', 'jasper.host.com:8080');

// Create a Resource Descriptor object for the jrxml file
$jrxml = new \Jasper\JasperJrxml('/reports/test.jrxml');

// Upload the Resource Descriptor object with content
$jasper->createContent($jrxml, file_get_contents('templates/test.jrxml'));

To create a report unit, go on with the following lines:

// Datasource Resource Descriptor
$mongo = new \Jasper\JasperDatasource();
$mongo->setPropIsReference('true');
$mongo->setPropReferenceUri('/datasources/mongo_local_test');

// Put everything together and deploy the report
$report->addChildResource($mongo);
$report->addChildResource($jrxml);

// Want to see the Resource Descriptor of the Report Unit?
// true = pretty print
print_r($report->getXml(true));

// Create a the Report Unit
$jasper->createResource($report);