2017-06-20 68 views
0

我想在寫入之前將現有的PDF複製到另一個PDF中。 這裏是我的代碼將現有的PDF複製到ByteOutputStream:Pdfcopy返回一個空白pdf

 documentPDF.open() 

    for(int i=0;i<nbPages;){ 
      copy.addPage(copy.getImportedPage(reader, ++i)) 
    } 
    documentPDF.close() 
    copy.close() 


    PdfReader reader= new PdfReader("DocDeBase30.pdf"); 
    reader.selectPages("1-5") 
    def documentPDF =new Document() 
    ByteArrayOutputStream baos = new ByteArrayOutputStream() 
    PdfCopy copy = new PdfCopy(documentPDF, baos) 
    documentPDF.open() 
    for(int i=0;i<nbPages;){ 
      copy.addPage(copy.getImportedPage(reader, ++i)) 
    } 
    documentPDF.close() 
    copy.close() 
    reader.close(); 
    return baos 

然後我用這一步渲染PDF文件,以我的觀點:

 response.setContentType("application/pdf") 
       response.setHeader('Content-disposition', "attachment; filename=intercalaire.pdf") 
       response.outputStream << result 
       response.outputStream.flush() 
    result.close() 

,其中結果是返回BAOS。

該文件正在我的瀏覽器中正確打開,但它是空白的,沒有被複制。

爲您的信息,它工作正常時,我使用物理路徑,而不是使用ByteArrayOutputStream但我必須在我的應用程序中使用動態文檔,因此物理路徑不是一個選項。

有什麼想法?

THX提前

+0

也許你正在這裏解釋的字節:http://itext.2136553.n4.nabble.com/Blank-PDF-after-it-is-transfered-through-SMTP-td2228773。 html沒有看到PDF,我們可以告訴你關於這個問題的其他信息。如果頁面在那裏,但頁面的內容爲空,則您的服務器被配置爲提供純文本並且不能提供二進制數據。 –

回答

0

THX布魯諾Lowagie你是對剃鬚字節爲解釋在這裏:http://itext.2136553.n4.nabble.com/Blank-PDF-after-it-is-transfered-through-SMTP-td2228773.html

閱讀的鏈接後,我由ByteArrayOutputStream轉換成一個byte [] 使問題迎刃而解這裏是代碼我不得不改變來解決問題:

 byte[] pdf=result.toByteArray() 

    response.setContentType("application/octet-stream") 
       response.setHeader('Content-disposition', "attachment; filename=intercalaire.pdf") 
       response.outputStream << pdf 
       response.outputStream.flush() 
    result.close() 

現在它工作正常

Thx再次布魯諾Lowagie