1

I have a Spring Boot application where I am using the Jasper Reports Library. On the development machine, I could get both a subreport and an image with the path of

src/main/resources/static/[subreport.jasper | image.svg]

However, when packaging as a WAR and deploying to a Tomcat server, the path no longer worked.

What is one way that will work to allow the report to work both on the development and production machines?

Tim
  • 1,605
  • 6
  • 31
  • 47

1 Answers1

3

After much searching and trial and error, I finally have something that works.

Subreports are not too difficult.

  1. Create a SUBREPORT_DIR parameter.
  2. Set the default value to "static/".
  3. When calling the subreport in the main report, use the expression $P{SUBREPORT_DIR}+"SubreportName.jasper". If the subreport is in another folder, just add that in front of the SubreportName.

Images were a little trickier. It might be different if your image is in the main report. I had images in the subreport, and the image file was in the same directory as the subreport. I thought it should work to use a relative path like ./image.svg, but nothing along those lines would work.

I had the same problem trying to reference the main report. See Can't access static file from Spring Boot application running in Tomcat servlet

From what worked there, it gave me the idea to try it with the image, which did work. Here are the steps I needed to do:

  1. In Jaspersoft Studio, add spring-core-5.0.2.RELEASE.jar to the build path.
  2. On the subreport, add org.springframework.core.io.ClassPathResource; to the list of imports. In the jrxml, this looks like: <import value="org.springframework.core.io.ClassPathResource"/>.
  3. Add a SUBREPORT_DIR parameter with a default value of "static/".
  4. On the image, set the expression to: net.sf.jasperreports.renderers.BatikRenderer.getInstance(new ClassPathResource($P{SUBREPORT_DIR}+"image.svg").getFile()).

I would have to think there would be a more straightforward way to reference files in a relative manner, but at least I know this works.

Tim
  • 1,605
  • 6
  • 31
  • 47