2013-02-28 56 views
0

我要問你我怎麼能輸出一個servlet在Firefox pdf文件19如何在Firefox 19中的流中顯示PDF?

我有輸出PDF流

..... 

public void doPost(HttpServletRequest request, HttpServletResponse response) { 




    try { 

     String namePDF = "filename.pdf"; 
     StringBuffer sb = new StringBuffer(request.getScheme()); 
     sb.append("://").append(request.getServerName()); 
     if (request.getServerPort() > 0) { 
      sb.append(":").append(request.getServerPort()); 
     } 
     sb.append(request.getContextPath()).append("/dirPdf/") 
       .append(namePDF); 

     OutputStream out = response.getOutputStream(); 

     Document document = new Document(); 
     document.setMargins(30, 30, 25, 25); 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     PdfWriter pdfWriter = PdfWriter.getInstance(document, baos); 

     PdfPageEvents events = new PdfPageEvents(); 
     pdfWriter.setPageEvent(events); 

     // step 2: we set the ContentType and create an instance of the 
     // corresponding Writer 
     // writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, 
     // PdfWriter.STANDARD_ENCRYPTION_40); 




     document.open(); 

     compiledModule(request, response, document); 


     document.newPage(); 
     pdfWriter.setPageEmpty(false); 

     // chiudo il documento 
     document.close(); 

     int tot = pdfWriter.getPageNumber() - 1; 


     PdfReader reader = new PdfReader(baos.toByteArray()); 
     reader.consolidateNamedDestinations(); 
     PdfStamper pdfStamper = new PdfStamper(reader,baos); 

     //read from module 
     reader = new PdfReader(sb.toString()); 
     reader.consolidateNamedDestinations(); 

     PdfContentByte cbu = pdfStamper.getUnderContent(tot); 
     PdfImportedPage page = pdfStamper.getImportedPage(reader, 2); 

     cbu.addTemplate(page, 1, 0, 0, 1, 0, 0); 

     pdfStamper.close(); 

     response.setContentType("application/pdf"); 
     response.setContentLength(baos.size()); 
     response.setBufferSize(baos.size()); 

     baos.writeTo(out); 
     out.flush(); 
     out.close(); 
    } catch (Exception e) { 

     logger.error("", e); 
     try { 
      /*if (out != null) 
       out.close();*/ 
      response.sendRedirect("../exception.jsp?message=" 
        + e.getMessage()); 
     } catch (IOException ex) { 
      logger.error("", ex); 
     } 
    } 
} 

.... 

在Internet Explorer和Chrome文件一個servlet正確觀看,而在Firefox 19,我得到以下錯誤

「這個PDF文檔可能無法正確顯示」

我知道有,我可以通過在Firefox 19的設定變更達成一個解決方案,無論如何,因爲會有很多我的應用程序的用戶可能會因此感到氣餒,所以最好知道是否有實際的解決方法讓Firefox顯示PDF。

或者,也許是一個沒有解決方法的錯誤?

預先感謝您!

---更新28/02/2013下午12點52分---

這個問題似乎與事實,我用一些ByteArrayOutputStream複製到輸出流連接。 如果我直接使用outputStream,一切正常。 不幸的是我不得不使用ByteArrayOutputStream,因爲我必須將pdf的最後一頁粘貼到另一頁。

回答

0

我的情況的解決辦法是改變並添加第二個ByteArrayOutputStream

..... 

public void doPost(HttpServletRequest request, HttpServletResponse response) { 




try { 

    String namePDF = "filename.pdf"; 
    StringBuffer sb = new StringBuffer(request.getScheme()); 
    sb.append("://").append(request.getServerName()); 
    if (request.getServerPort() > 0) { 
     sb.append(":").append(request.getServerPort()); 
    } 
    sb.append(request.getContextPath()).append("/dirPdf/") 
      .append(namePDF); 

    OutputStream out = response.getOutputStream(); 

    Document document = new Document(); 
    document.setMargins(30, 30, 25, 25); 

    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); 
    PdfWriter pdfWriter = PdfWriter.getInstance(document, baos); 

    PdfPageEvents events = new PdfPageEvents(); 
    pdfWriter.setPageEvent(events); 

    // step 2: we set the ContentType and create an instance of the 
    // corresponding Writer 
    // writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, 
    // PdfWriter.STANDARD_ENCRYPTION_40); 




    document.open(); 

    compiledModule(request, response, document); 


    document.newPage(); 
    pdfWriter.setPageEmpty(false); 

    // chiudo il documento 
    document.close(); 

    int tot = pdfWriter.getPageNumber() - 1; 


    PdfReader reader = new PdfReader(baos.toByteArray()); 
    reader.consolidateNamedDestinations(); 
    PdfStamper pdfStamper = new PdfStamper(reader,baos2); 

    //read from module 
    reader = new PdfReader(sb.toString()); 
    reader.consolidateNamedDestinations(); 

    PdfContentByte cbu = pdfStamper.getUnderContent(tot); 
    PdfImportedPage page = pdfStamper.getImportedPage(reader, 2); 

    cbu.addTemplate(page, 1, 0, 0, 1, 0, 0); 

    pdfStamper.close(); 

    response.setContentType("application/pdf"); 
    response.setContentLength(baos2.size()); 
    response.setBufferSize(baos2.size()); 

    baos2.writeTo(out); 
    out.flush(); 
    out.close(); 
} catch (Exception e) { 

    logger.error("", e); 
    try { 
     /*if (out != null) 
      out.close();*/ 
     response.sendRedirect("../exception.jsp?message=" 
       + e.getMessage()); 
    } catch (IOException ex) { 
     logger.error("", ex); 
    } 
} 

}

....

貌似有增加byteArrayOutputStream到自己與PdfStamper問題。我不明白只有一個寶箱和兩個寶箱之間的關係。隨意解釋...