2012-08-10 53 views
5

我需要它在內存創建後iTextPDF文檔文件轉換爲的byte []。我已經測試過,我沒有正確創建PDF的問題。問題是如何將其轉換爲字節數組以存儲在數據庫中。如何iTextPDF文檔轉換爲字節數組

這裏是我的代碼:

Document generatedDocument = reportService.generateRequestForm(scdUser, jsonObject, 0, null); 
reportService.generateRequestForm(scdUser, jsonObject, 0, null); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
PdfWriter pdfWriter = PdfWriter.getInstance(generatedDocument, baos); 
generatedDocument.open(); 
document.setDocument(baos.toByteArray()); // stores as blob 

我在數據庫BLOB列得到了值。

這裏是我的文檔域對象:

文檔域對象

@Entity 
@Table(name = "document") 
public class Document implements java.io.Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "document_id", nullable = false) 
    private int documentId; 
    @Column(name = "document_name", nullable = false, length = 65535) 
    private String documentName; 
    @Column(name = "document_type", nullable = false) 
    private int documentType; 
    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "upload_date", nullable = false, length = 19) 
    private Date uploadDate = new Date(); 
    @Column(name = "document", nullable = false) 
    private byte[] document; // BLOB COLUMN 
    @Column(name = "document_size", nullable = false) 
    private long documentSize; 
    @Column(name = "title", nullable = true, insertable = true, updatable = true, length = 65535, precision = 0) 
    private String title; 
    @Column(name = "tag", nullable = true, insertable = true, updatable = true, length = 65535, precision = 0) 
    private String tag; 
    @Column(name = "description", nullable = true, insertable = true, updatable = true, length = 65535, precision = 0) 
    private String description; 
    @Column(name = "shared", nullable = false, insertable = true, updatable = true, length = 1, precision = 0) 
    private boolean shared = false; 
    @Column(name = "status", nullable = false) 
    private int status = DocumentStatus.READY.getStatus(); 


    public int getDocumentId() { 
     return this.documentId; 
    } 

    public void setDocumentId(int documentId) { 
     this.documentId = documentId; 
    } 

    public String getDocumentName() { 
     return this.documentName; 
    } 

    public void setDocumentName(String documentName) { 
     this.documentName = documentName; 
    } 

    public int getDocumentType() { 
     return this.documentType; 
    } 

    public void setDocumentType(int documentType) { 
     this.documentType = documentType; 
    } 

    public Date getUploadDate() { 
     return this.uploadDate; 
    } 

    public void setUploadDate(Date uploadDate) { 
     this.uploadDate = uploadDate; 
    } 

    public byte[] getDocument() { 
     return this.document; 
    } 

    public void setDocument(byte[] document) { 
     this.document = document; 
    } 

    public long getDocumentSize() { 
     return this.documentSize; 
    } 

    public void setDocumentSize(long documentSize) { 
     this.documentSize = documentSize; 
    } 

    public String getTag() { 
     return tag; 
    } 

    public void setTag(String tag) { 
     this.tag = tag; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public boolean getShared() { 
     return shared; 
    } 

    public void setShared(boolean shared) { 
     this.shared = shared; 
    } 


    public int getStatus() { 
     return status; 
    } 

    public void setStatus(int status) { 
     this.status = status; 
    } 


} 
+0

看看這個問題,然後看看你最後一句話.. – Eugene 2012-08-10 07:40:57

+0

看起來像文件不會因爲那個數據庫錯誤而首先產生。 – 2012-08-10 07:51:24

+0

@Eugene它是一樣的;我堅持byte [] blob列。 – talha06 2012-08-10 08:03:21

回答

2

從評論看來,它沒有任何關係在運行時生成的PDF的方式,而是它的方式存儲在數據庫中。您還需要提供該代碼。

這是我反倒(我的測試):

的持有人:

import java.io.Serializable; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 



@Entity 
public class PDFHolder implements Serializable { 

@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
private long id; 

@Column(columnDefinition = "LONGBLOB") 
private byte[] documentPDF; 

public byte[] getDocumentPDF() { 
    return documentPDF; 
} 

public void setDocumentPDF(byte[] documentPDF) { 
    this.documentPDF = documentPDF; 
} 

public long getId() { 
    return id; 
} 

public void setId(long id) { 
    this.id = id; 
} 
} 

資源庫,它不保存:

@Repository 
public interface PDFHolderRepository extends JpaRepository<PDFHolder, Long> {} 

和實際插入:

private void generateDatabaseRows() { 

    try{ 
     String urlPDF = "http://cetatenie.just.ro/LinkClick.aspx?fileticket=K13DZkoIE2o%3d&tabid=57&mid=593"; 
     URL url = new URL(urlPDF); 

     ByteBuffer byteBufferResponse = this.getAsByteArray(url.openConnection()); 
     byte [] responseArray = byteBufferResponse.array(); 
     System.out.println("Size of the PDF : " + responseArray.length); 

     // Save it to DB 
     PDFHolder pdfHolder = new PDFHolder(); 
     pdfHolder.setDocumentPDF(responseArray); 

     pdfHolderRepository.save(pdfHolder); 

    } catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 


private ByteBuffer getAsByteArray(final URLConnection urlConnection) throws IOException { 
    final ByteArrayOutputStream tmpOut = new ByteArrayOutputStream(); 
    final InputStream inputStream = urlConnection.getInputStream(); 
    final byte[] buf = new byte[1024]; 
    int len; 
    while (true) { 
     len = inputStream.read(buf); 
     if (len == -1) { 
      break; 
     } 
     tmpOut.write(buf, 0, len); 
    } 
    tmpOut.close(); 
    return ByteBuffer.wrap(tmpOut.toByteArray(), 0, tmpOut.size()); 
} 

當然,我在這個類的@Autowired庫:

@Autowired 
PDFHolderRepository pdfHolderRepository; 

MySQL stores the byte array

+0

已編輯的第一篇文章.. – talha06 2012-08-10 11:09:55

+0

@ talha06向我們展示了您的確切位置。 – Eugene 2012-08-10 11:13:28

+0

當我從數據庫中檢索文檔時,我得到空。 (在GUI-MySQL Workbench中我也看到了null)我使用Hibernate將PDF保存到數據庫。當我調試代碼時,我看到文檔已成功生成。所以我想了解如何成功將iTextPDF文檔轉換爲字節數組以便正確保存。 – talha06 2012-08-10 11:18:52

12

我有一個類似的問題......我想創建文檔並在那裏我創造它的類裏面,我可以將它保存到文件,它運作良好。但是,當我試圖將它作爲Stream返回時,我會得到一個空值。

問題是,一旦Document被關閉(document.close()),它也會關閉Stream。

解決方法是創建一個ByteArrayOutputStream,當我創建文檔並將PdfWriter輸出給它時。然後,我可以做任何我想要的PDF字節...在我的情況下,我將它們轉換爲一個StreamedContent發送給用戶。

創建一個變量來保存字節:

private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

有PdfWriter輸出數據的字節[],因爲它創建文檔:

Document document = new Document(PageSize.LETTER, 0.75F, 0.75F, 0.75F, 0.75F); 
    PdfWriter.getInstance(document, byteArrayOutputStream); // Do this BEFORE document.open() 

    document.open(); 
    createPDF(document); // Whatever function that you use to create your PDF 
    document.close(); 

一旦大功告成生成PDF,只需獲取字節,然後按照你的意願做。

byte[] pdfBytes = byteArrayOutputStream.toByteArray(); 

我不知道你的reportService類是什麼樣的,但這可能是一個很好的放置它的地方。

希望這會有所幫助。

+0

很好的答案!幫助了我很多!有沒有一些優雅的方式來設置文件的名稱? Chrome將其自動下載爲「something.pdf」。 – 2014-04-08 10:28:53

相關問題