2014-02-20 53 views
0

我在問題「Append PDF at the available space of another PDF file」之前問過,並且我已經在一個頁面中將原生iText和JFreeChart組合成PDF。JFreeChart沒有在生成的Spring MVC和iText中顯示PDF

我使用教程「Spring Web MVC with PDF View Example (using iText 5.x)」將此設置合併到我的Spring MVC應用程序中。

據我所知,使用Spring的AbstractView,進而爲

@Override 
protected void buildPdfDocument(Map<String, Object> map, Document document, 
     PdfWriter pdfWriter, HttpServletRequest request, 
     HttpServletResponse response) throws Exception { 

    // do iText and JFreeCharts 

} 

和顯示JFreeChart實例需要現有的PDF文件中插入它作爲「Creating an iText pdf with embedded JFreeChart」證明實施:

// get the direct pdf content 
PdfContentByte dc = docWriter.getDirectContent(); 

// get a pdf template from the direct content 
PdfTemplate tp = dc.createTemplate(width, height); 

// create an AWT renderer from the pdf template 
Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper()); 
Rectangle2D r2D = new Rectangle2D.Double(0,0, width,height); 
chart.draw(g2,r2D,null); 
g2.dispose(); 

// add the rendered pdf template to the direct content 
// you will have to play around with this because the chart is absolutely positioned. 
// 38 is just a typical left margin 
// docWriter.getVerticalPosition(true) will approximate the position that the content above the chart ended 
dc.addTemplate(tp, 38, docWriter.getVerticalPosition(true)-height); 

現在我的問題是,每當我調用擴展的方法AbstractView(請參閱第二個鏈接),它顯示插入JFreeChart之前的PDF而不是具有創建評論圖表。我沒有遇到任何錯誤,但我想知道是否有解決此問題的方法。

我只想讓用戶使用JFreeChart查看PDF,就像Spring MVC中的普通PDF實現一樣。

正如你所看到的,我只是按照上面的教程進行組合。他們在一個單獨的項目中完美地工作,但是當我與Spring合併時,JFreeChart沒有顯示,特別是顯示了錯誤的PDF文件。


我知道我可以只放一個鏈接到正確的文件調用生成的PDF使用JFreeChart,有點攔截觀看,但我保持我的希望了這種設置,因爲這將是一個方便的方法使用這三種技術完全沒有文件I/O在服務器(我有麻煩)中的麻煩。


下面是類AbstractITextPdfView從「Spring Web MVC with PDF View Example (using iText 5.x)」,我使用的一個片段。

有人能告訴我如何直接打開PDF文件到圖表,即在buildPdfDocument()內?

@Override 
protected void renderMergedOutputModel(Map<String, Object> model, 
     HttpServletRequest request, HttpServletResponse response) 
     throws Exception { 
    // IE workaround: write into byte array first. 
    ByteArrayOutputStream baos = createTemporaryOutputStream(); 

    // Apply preferences and build metadata. 
    Document document = newDocument(); 
    PdfWriter writer = newWriter(document, baos); 
    prepareWriter(model, writer, request); 
    buildPdfMetadata(model, document, request); 

    // Build PDF document. 
    document.open(); 
    buildPdfDocument(model, document, writer, request, response); 
    document.close(); 

    // Flush to HTTP response. 
    writeToResponse(response, baos); 
} 

更新

我通過保存到文件後JFreeChart 「武官」 本身的iText PDF截獲ByteArrayOutputStream

@Override 
protected void renderMergedOutputModel(Map<String, Object> model, 
     HttpServletRequest request, HttpServletResponse response) 
     throws Exception { 
    // IE workaround: write into byte array first. 
    ByteArrayOutputStream baos = createTemporaryOutputStream(); 

    // Apply preferences and build metadata. 
    Document document = newDocument(); 
    PdfWriter pdfWriter = newWriter(document, baos); 
    prepareWriter(model, pdfWriter, request); 
    buildPdfMetadata(model, document, request); 

    // Build PDF document. 
    document.open(); 
    buildPdfDocument(model, document, pdfWriter, request, response); 
    document.close(); 

    FileOutputStream fos = null; 
    try { 
     fos = new FileOutputStream(new File("D:/MUST-HAVE-CHART.pdf")); 
     baos.writeTo(fos); 
    } catch (IOException ioe) { 
     // Handle exception here 
     ioe.printStackTrace(); 
    } finally { 
     fos.close(); 
    } 
    System.out.println(baos.toString()); 

    // Flush to HTTP response. 
    writeToResponse(response, baos); 
} 

發生了什麼事是MUST-HAVE-CHART.PDF沒有圖表,同創建後由Spring MVC提供了一個。與圖表的一個但是被保存在其他地方,如在代碼:

@Override 
protected void buildPdfDocument(Map<String, Object> map, Document document, 
     PdfWriter pdfWriter, HttpServletRequest request, 
     HttpServletResponse response) throws Exception { 

    logger.info("Start of PDF creation"); 

    Domain domain = (Domain) map.get("domain"); 

    ServletContext servletContext = request.getSession() 
      .getServletContext(); 
    File servletTempDir = (File) servletContext 
      .getAttribute("javax.servlet.context.tempdir"); 

    if (servletTempDir == null) 
     throw new IllegalStateException(
       "Container does not provide a temp dir"); 

    File targetFile = new File(servletTempDir, NAME_FILE); 

    pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(
      targetFile)); 

    logger.info("PDF file path: " + targetFile.getAbsolutePath().toString()); 

    document.open(); 

    PdfBuilder.assemble(document, pdfWriter, domain); 

    document.close(); 
    pdfWriter.close(); 

    logger.info("End of PDF creation"); 

    map.put("file", targetFile.getAbsolutePath().toString()); 
} 

正確的保存在javax.servlet.context.tempdirC:/tomcat/work/Catalina/localhost/my_spring_app/pdf_file.pdf

如何讓後者顯示在屏幕上?

回答

0

我有同樣的問題。 我使用下面的代碼解決了這個問題,現在文件正在下載,而不是渲染一個新的pfd-view頁面(我更喜歡這個)。

protected final void renderMergedOutputModel(Map<String, Object> model, 
      HttpServletRequest request, HttpServletResponse response) throws Exception { 

    // IE workaround: write into byte array first. 
    ByteArrayOutputStream baos = createTemporaryOutputStream(); 

    // Apply preferences and build metadata. 
    Document document = newDocument(); 
    PdfWriter writer = newWriter(document, baos); 
    prepareWriter(model, writer, request); 
    buildPdfMetadata(model, document, request); 

    // Build PDF document. 
    document.open(); 
    buildPdfDocument(model, document, writer, request, response); 
    document.close(); 

    //Custom code start 
    String filestorePath = "/var/www/xxx/xxxxx/"; 
    String downloadfileNm = "DeliveryNote.pdf"; 
    String filePath = filestorePath+downloadfileNm; 
    URL url = new URL("file:"+filePath); 
    File file = new File(url.toURI()); 
    if (file.exists()) { 
     response.setContentLength(new Long(file.length()).intValue()); 
     response.setHeader("Content-Disposition", "attachment; filename="+downloadfileNm); 
     FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream());  
    } 
    //Custom code end 

    // Flush to HTTP response. 
    //writeToResponse(response, baos); 

}