Search code examples
javaimagespringpropertiesjasper-reports

Spring JasperReports Images Location


JasperReport requires (by default) that images be in "WEB-INF/classes/". I'd like to share images between PDFs and normal JSP pages. I'd rather not clutter up classpath with garbage image files. How do I force JR to use a different location for images?


Solution

  • I recommend creating the following report parameters:

    • ROOT_DIR -- "/reports/"
    • IMAGE_DIR -- $P{ROOT_DIR} + "images/"
    • STYLE_DIR -- $P{ROOT_DIR} + "styles/"
    • SUBREPORT_DIR -- $P{ROOT_DIR} + "subreports/"
    • COMMON_DIR -- $P{SUBREPORT_DIR} + "common/"

    This allows the images directory to be relative to the ROOT_DIR path. It also allows you to change ROOT_DIR dynamically. The parameters must be declared in their relative order.

    In your case, using an absolute path:

    • ROOT_DIR -- "/home/user/"
    • IMAGE_DIR -- $P{ROOT_DIR} + "Pictures/"

    Note that changing between operating systems, directory structures, environments (e.g., migrating to JasperReports Server from JasperReports & JSF) and so forth, can be accomplished without having to modify the report. (Well, some modifications are required for JasperReports Server.)