【发布时间】:2015-04-14 02:12:35
【问题描述】:
我正在使用此代码使用 iText 生成 PDF。首先,它创建 HTML 到 PDF,然后将 PDF 转换为字节数组或 BLOB 或字节数组。 我不想在我的服务器上创建任何 pdf 的物理存储。首先,我想使用 itext 将 HTML 转换为 PDF 的 blob,然后我想将该 blob 存储在我的 DB 中(我会在 DB 中存储)。
String userAccessToken=requests.getSession()
.getAttribute("access_token").toString();
Document document = new Document(PageSize.LETTER);
String name="/pdf/invoice.pdf";
PdfWriter pdfWriter = PdfWriter.getInstance
(document, new FileOutputStream(requests.getSession().getServletContext().getRealPath("")+"/assets"+name));
document.open();
document.addAuthor("Real Gagnon");
document.addCreator("Real's HowTo");
document.addSubject("Thanks for your support");
document.addTitle("Please read this ");
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
//data is an html string
String str = data;
worker.parseXHtml(pdfWriter, document, new StringReader(str));
document.close();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PdfWriter.getInstance(document, byteArrayOutputStream);
byte[] pdfBytes = byteArrayOutputStream.toByteArray();
link=name;
System.out.println("Byte array is "+pdfBytes);
问题:- 使用 itext 将 html 转换为 pdf BLOB,没有 PDF 的物理存在。
【问题讨论】:
-
这里有什么问题?
-
不,它正在工作。但它会在服务器上创建第一个物理 PDF,我不想在服务器上创建物理 PDF (/pdf/invoice.pdf)。