本文介紹了如何使用PDFBox從HTML創(chuàng)建PDF文件?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在嘗試從HTML內(nèi)容創(chuàng)建PDF。
public byte[] generatePdf(final XhtmlPDFGenerationRequest request) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PDDocument document = new PDDocument();
InputStream stream = new ByteArrayInputStream(request.getContent()
.getBytes());
PDStream pdstream = new PDStream(document, stream);
document.save(baos);
document.close();
return this.toByteArray(baos);
}
當(dāng)我將此byte[]
保存到一個(gè)文件時(shí),該文件為空。我使用PDStream
將輸入流嵌入到文檔中
來自http://pdfbox.apache.org/apidocs/
public PDStream(PDDocument doc,
InputStream str)
throws IOException
從輸入流讀取所有數(shù)據(jù)并將其嵌入到文檔中,這將關(guān)閉InputStream
。
我正在尋找一個(gè)推薦答案到PDF的呈現(xiàn)器。我們用的是iText。我也想用ApachePDFBox做同樣的事情。但是,看起來這是不可能的。
我可以使用Apache FOP或繼續(xù)使用iText。
如果任何人感興趣,以下是iText解決方案:
Java Render XML Document as PDF
如果您正在尋找使用PDF Box進(jìn)行合并的解決方案,這里就是
Merge pdf files using Apache pdf box
這篇關(guān)于如何使用PDFBox從HTML創(chuàng)建PDF文件?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,