Search code examples
javaservletsjasper-reports

Saving jasper report as pdf on server


I have a servlet that uses jasper reports to create a pdf. This pdf is displayed in the web browser using the JasperExportManager.exportReportToPdfStream() method. This works fine. However, what I want is to save the file in a folder that resides on the server. I have tried to do this with the following code:

String outputFile = getServletContext().getRealPath("/theFolder");
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFile + "/thePDF.pdf");

This code does not produce any errors and runs to completion, but I can't find the file anywhere on the server. What am I doing wrong? Thanks.


Solution

  • If your jasper debug logs are configured well. In the output you will see the path where your report is getting generated.

    OR you can use my code

    String path=System.getProperty("user.dir");
    JasperExportManager.exportReportToPdfFile(this.jp, path + "/reports/Report.pdf");
    

    This will save your file where you want relative to the path of the folder. You can also provide absolute path if necessary.

    Also check if the report is present in the target folder of your Project.