RunReport in HTML format for PieChart and LineChart

I'm using JasperServer Pro on Amazon MarketPlace and have 2 chart reports designed with Jasper Studio (and published to server.). In my PHP application, I'm using REST API php wrapper to call RunReport. My code is something like:

$data = $rs->runReport($sample_report, 'html', NULL, NULL, $controls);

I got an image back in the $data whose path is something like:  /jasperserver-pro/rest_v2/reportExecutions/72034391_1395407813827_65/exports/html/attachments/img_0_0_0. Even after I added the host name to this URL, there is no image at that path.

My question is how can I find the proper path for the image that needs to be accessible in my PHP application.


When I ran the report from Jasper Server, it ran properly and the path for the chart image is something like. '/jasperserver-pro/reportimage?jrprint=1665740205_1395408153483_15&image=img_0_0_0', can I use path like this to access the chart image? how can I get those numbers in the URI?


Justin_6's picture
Joined: Feb 24 2014 - 2:07pm
Last seen: 8 years 6 months ago

3 Answers:

As of now, its not possible to include images embedded in a report using the PHP client in HTML format. The URIs you are seeing refer to some resources that require an authenticated session to view. One solution currently would be to host the images in a place accessible to the public (e.g: a separate web server). Naming the images as seen in the output and without a suffix, this should load the images.


For example, if you request the image as below (note, I'm using latest github version, its unclear if youre using version from exchange, or github)

$report = $c->reportService()->runReport("/reports/samples/AllAccounts", "html", null, "");

Then when images are added to the HTML, they'll have a src attribute with the prefix your provided:

<img src="" style="position: absolute; left:145px; top: 22px;" alt="" title="JasperReports Logo"/></div></td>

grantbacon's picture
Joined: Oct 23 2012 - 1:10pm
Last seen: 4 years 6 months ago

If you are using an old version, just remove ->reportService(), and the argument order should be the same.

grantbacon - 8 years 8 months ago

I have been struggling with the missing charts and images in the HTML report when using php client and here is a workaround I found:

1.  Render a report in xml format:

//Import report in XML format
$reportXML = $c->reportService()->runReport('/Reports/Sample_Report', 'xml', null, null, $controls);

2. All images and charts are there embedded in base64 format, find them and extract to a string array:

//Find base64 images encoded in the XML file
$docXML = new DOMDocument();
$docXML->loadXML($reportXML); //mark the use of loadXML
$imagesXML = $docXML->getElementsByTagName('image');
$imagesBase64= array();
foreach ($imagesXML as $image) {    
foreach($image->childNodes as $child) {  
           if ($child->nodeName=='imageSource'){
            }    }

3. Render a report second time this time in the html format:

$reportHTML=$c->reportService()->runReport('/Reports/Sample_Report', 'html', null, null, $controls);

4. Find all img src tags and exchange its content with base64 encoded images from xml document. For some reason in my case the DomDocuments fails to accept rendered HTML report so I used regular expression instead:

$reportHTML2 = preg_replace_callback('#src=["\']([^"\']+)["\']#', function($matches) use (&$imagesBase64) { return 'src="data:image/png;base64,'.trim(array_shift($imagesBase64).'"');}, $reportHTML);

5. and this is it.

echo $reportHTML2;

magrom's picture
Joined: Dec 30 2013 - 8:12am
Last seen: 10 months 1 day ago

I followed your code and while running it, I still get my chart missing. But, while having two charts in my asper report, image get displayed but only one image is being displayed. I don't understand why is this happening so. Please, help me to fix this. Thank you.



pinkyjaiswal501's picture
Joined: Jul 14 2016 - 2:02am
Last seen: 4 years 4 months ago