2017-08-10 26 views
0

私はspring-mvcプロジェクトに取り組んでいます。私はJasperReportsを使用してレポートしています。 pdf形式のレポートを生成するリンクがあります。リンクは正常に動作しており、レポートが生成されますが、問題はpdfレポートには画像が表示されません。私jrxmlファイルからの抜粋:
JasperReportがpdfに変換したときに画像を表示しない

<frame> 
    <reportElement x="0" y="0" width="70" height="67" uuid="0af2e978-7c0c-4805-afb4-4069d1297a12"/> 
    <image onErrorType="Blank" evaluationTime="Report"> 
     <reportElement mode="Opaque" x="0" y="0" width="70" height="67" uuid="c572c836-8d67-480a-b1b1-d603940e6c74"/> 
     <imageExpression><![CDATA["images/geLogo.jpg"]]></imageExpression> 
    </image> 
</frame> 


は、私は再びチェックした画像は、私のプロジェクトで私のwebapp/imagesフォルダに存在しています。私は自分のJSPページでこの同じイメージを使用しています。

コードはJasperReportからPDFを生成するために使用:同じパスでJasperReportデザイナーで画像を表示するものの画像は、PDFに来ていない理由

try { 
     JasperPrint print = service.loadReceipt(RECEIPT_NAME, paymentId); 
     pdfFile = JasperExportManager.exportReportToPdf(print); 
     OutputStream outStream = res.getOutputStream(); 
     res.setContentType("application/pdf"); 
     res.addHeader("Content-disposition", "inline; filename=Receipt.pdf"); 
     outStream.write(pdfFile); 
     outStream.flush(); 
     outStream.close(); 
    } 

あなたは私を伝えることができます。私は、EclipseにJasperReportsプラグインを使用してレポートを設計しました。私はjboss 6.4でwarファイルを使ってプロジェクトを展開します。
ありがとうございます。

--------
私は@ KDavid-Valerioの答えから戦争の中でプロジェクトの構造をチェックするというアイデアを得ました。実際のプロジェクト構造とは異なります。まだ動作していないよう

Project 
    images 
     image1.jpg 
     image2.jpg 
    WEB-INF 
     classes 
      reports 
       report1.jrxml 
       report2.jrxml 


:戦争の構造は次のようになります。
------- UPDATE --------
ジャスパーレポートを埋めコード:

public JasperPrint loadReceipt(String reportName, String paymentId, String imagePath) { 
     HashMap<String, Object> params = new HashMap<String, Object>(); 
     JasperReport report = null; 
     JasperPrint print = null; 

     try { 
      if (jrReportMap == null) { 
       jrReportMap = new HashMap<String, JasperReport>(); 
      }   

      if (jrReportMap.get(reportName) == null) { 
       report = JasperCompileManager.compileReport(reportManager.load(reportName)); 
       jrReportMap.put(reportName, report); 
       log.info(Logger.EVENT_SUCCESS, "--- Report Compilation done --- " + reportName); 
      } else { 
       report = jrReportMap.get(reportName); 
       log.info(Logger.EVENT_SUCCESS, "--- Report already Compiled --- " + reportName); 
      } 

      params.put("paymentId", paymentId); 
      params.put("realPath", imagePath); 

      try { 
       Connection conn = reportDataSource.getConnection(); 
       print = JasperFillManager.fillReport(report, params, conn); 
       conn.close(); 
      } catch (SQLException e) { 
       System.err.println("--- SQL ERR - to get connection -----"); 
       log.error(Logger.EVENT_FAILURE, "--- Report already Compiled --- " + reportName); 
       e.printStackTrace(); 
      } 
     } catch (JRException e1) { 
      log.error(Logger.EVENT_FAILURE, "Oops... Something wrong while rendering the report !!!"); 
      e1.printStackTrace(); 
     } 
     return print; 
    } 
+0

、実行時にJasperReportで使用されるパス、それは違います設計者が使用したものからあなたのJavaコードでは、WAR内の.jasperファイルに相対的な画像のパスを置く必要があります。 –

+0

私は多くの異なった道を試みました。何も動作していないようです。私はC:/ somethingのような外部パスを試したことさえあります。それは動作しません。 – Waquar

+0

私は自分のプロジェクトでどのようにしたのかを説明する答えを書くつもりです。これがあなたを助けてくれることを願っています。 –

答えて

0

私はのServletContextを使用して、この問題への解決策を見つけました。

@Autowired 
ServletContext context; 

Autowireこの依存性は、あなたのプロジェクトが展開された後、任意のディレクトリの実際のパスを取得するためのメソッドgetRealPath()を使用しています。 jbossにコードをデプロイするたびに変わっていくことがわかった。PDFファイルを生成

コード:

byte[] pdfFile = null; 
    String realPath = context.getRealPath("/images/"); 
    try { 
     JasperPrint print = service.loadReceipt(RECEIPT_NAME, paymentId, realPath); 
     pdfFile = JasperExportManager.exportReportToPdf(print); 
     OutputStream outStream = res.getOutputStream(); 
     res.setContentType("application/pdf"); 
     res.addHeader("Content-disposition", "inline; filename=Receipt.pdf"); 
     outStream.write(pdfFile); 
     outStream.flush(); 
     outStream.close(); 
} 

コードレポートを埋めるために:

public JasperPrint loadReceipt(String reportName, String paymentId, String realPath) { 
     HashMap<String, Object> params = new HashMap<String, Object>(); 
     JasperReport report = null; 
     JasperPrint print = null; 

     try { 
      if (jrReportMap == null) { 
       jrReportMap = new HashMap<String, JasperReport>(); 
      }   

      if (jrReportMap.get(reportName) == null) { 
       report = JasperCompileManager.compileReport(reportManager.load(reportName)); 
       jrReportMap.put(reportName, report); 
       log.info(Logger.EVENT_SUCCESS, "--- Report Compilation done --- " + reportName); 
      } else { 
       report = jrReportMap.get(reportName); 
       log.info(Logger.EVENT_SUCCESS, "--- Report already Compiled --- " + reportName); 
      } 

      params.put("paymentId", paymentId); 
      params.put("realPath", realPath); 

      try { 
       Connection conn = reportDataSource.getConnection(); 
       print = JasperFillManager.fillReport(report, params, conn); 
       conn.close(); 
      } catch (SQLException e) { 
       System.err.println("--- SQL ERR - to get connection -----"); 
       log.error(Logger.EVENT_FAILURE, "--- Report already Compiled --- " + reportName); 
       e.printStackTrace(); 
      } 
     } catch (JRException e1) { 
      log.error(Logger.EVENT_FAILURE, "Oops... Something wrong while rendering the report !!!"); 
      e1.printStackTrace(); 
     } 
     return print; 
    } 

そして最後にjrxmlスニペット:

<frame> 
    <reportElement x="0" y="0" width="70" height="67" uuid="0af2e978-7c0c-4805-afb4-4069d1297a12"/> 
    <image onErrorType="Blank" evaluationTime="Report"> 
     <reportElement mode="Opaque" x="0" y="0" width="70" height="67" uuid="c572c836-8d67-480a-b1b1-d603940e6c74"/> 
     <imageExpression><![CDATA[$P{realPath}+"/geLogo.jpg"]]></imageExpression> 
    </image> 
</frame> 
0

代わりのパラメータを使用し<imageExpression><![CDATA["images/geLogo.jpg"]]></imageExpression>

を使用。 例えば、:。

<imageExpression><![CDATA[$P{imagePath}]]></imageExpression> 

IMAGEPATHそれがWAR内の画像の相対URLを含む文字列です。

たとえば、あなたのWARは次のような構造を持っている場合:

YourWebProject 
     WebContent 
      reports 
       report1.jasper 
       report2.jasper 
       ... 
       reportn.jasper 
      images 
       geLogo.jpg 

あなたはそれをレポートパスへの画像の相対パスを与える必要がありIMAGEPATHパラメータを埋める


。あなたはユーザーgeLogo.jpgイメージにしたい場合、それは次のようになります。

String imagePath = "../images/geLogo.jpg"; 
+0

私は戦争の中でプロジェクトの構造をチェックすることを考えたことはありません。今私はそれを確認した、それは私がEclipseワークスペースで見るものとは異なる。
戦争の中の構造は次のとおりです。
'プロジェクト \t画像 \t \t image1.jpg \t \t image2.jpg \t WEB-INF \t \tクラス \t \t \tレポート \t \t \t \t report1.jrxml \t \t \t \t report2.jrxml' – Waquar

+0

@ワクワクあなたが有用な私の答えを設立した場合は、それを確認してくださいseul :) –

+0

@Waquarレポートを埋め込むJavaコードを投稿できますか? –

関連する問題