如何将iTextPDF文档转换为字节数组 [英] How to convert iTextPDF Document to Byte Array
问题描述
在内存中创建后,我需要将 iTextPDF文档文件转换为 byte [] 。我已经测试过,我没有正确创建PDF的问题。问题是如何将其转换为字节数组以存储在DB中。
I need to convert iTextPDF Document file to byte[] after it's created in memory. I have already tested that I've no problem with creating PDF properly. The problem is how to convert it to byte array to store in DB.
这是我的代码:
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
我在数据库 null 的值> blob 专栏。
I got value of null at database blob column.
这是我的文档域对象:
文档域对象
@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;
}
}
推荐答案
我遇到了类似的问题......我创建了Document并在我创建它的类中,我可以将它保存到File,它运行得很好。但是,当我尝试将其作为Stream返回时,我会得到一个空值。
I had a similar problem... I'd create the Document and inside the class where I created it, I could save it to File, and it worked great. However, when I tried to return it as a Stream I would get a null value.
问题是文档关闭后(document.close()),它也会关闭Stream。
The problem was that once the Document was closed (document.close()), it would close the Stream also.
解决方法是在创建Document并创建PdfWriter输出时创建一个ByteArrayOutputStream。然后,我可以用PDF字节做任何我想做的事情...在我的情况下,我将它们转换为StreamedContent发送给用户。
The work-around was to create a ByteArrayOutputStream when I created the Document and have the PdfWriter output to it. I could then do whatever I wanted with the PDF bytes... in my case I converted them to a StreamedContent to send to the user.
创建一个变量来保存bytes:
Create a variable to hold the bytes:
private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
让PdfWriter在创建文档时将数据输出到byte []:
Have the PdfWriter output the data to the byte[] as it creates the document:
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后,只需获取字节并按照自己的意愿处理。
Once you're done generating the PDF, just get the Bytes and do with them as you will.
byte[] pdfBytes = byteArrayOutputStream.toByteArray();
我不知道你的reportService类是什么样的,但这可能是一个放置的好地方它。
I don't know what your reportService class looks like, but this may be a good place to put it.
希望这有帮助。
这篇关于如何将iTextPDF文档转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!