gpt4 book ai didi

java - 如何将 iTextPDF 文档转换为字节数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:08:51 27 4
gpt4 key购买 nike

iTextPDF 文档 文件在内存 中创建后,我需要将其转换为byte[]。我已经测试过正确创建 PDF 没有问题。问题是如何将其转换为字节数组存储在数据库中。

这是我的代码:

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 列中得到了 null 的值。

这是我的文档域对象:

文档域对象

@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;
}


}

最佳答案

我有一个类似的问题...我创建了文档并在我创建它的类中,我可以将它保存到文件,并且效果很好。但是,当我尝试将其作为 Stream 返回时,我会得到一个空值。

问题是一旦文档关闭 (document.close()),它也会关闭流。

解决方法是在我创建文档时创建一个 ByteArrayOutputStream 并将 PdfWriter 输出到它。然后我可以用 PDF 字节做任何我想做的事……在我的例子中,我将它们转换为 StreamedContent 以发送给用户。

创建一个变量来保存字节:

  private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

让 PdfWriter 在创建文档时将数据输出到 byte[]:

  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 类是什么样的,但这可能是放置它的好地方。

希望这对您有所帮助。

关于java - 如何将 iTextPDF 文档转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11897290/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com