2011-04-06 77 views
3

我目前正在使用Java構建基於PDF的報表。我看過的iText和BIRT,但它似乎是一個很大的努力去學習他們的API就是我試圖找到一個解決方案,可以讓我在HTML中生成報告(事我已經知道了),並輸出爲PDF。誰能提供一些可能的解決方案? - 謝謝! - 鄧肯克雷布斯Java HTML-> PDF解決方案?

+0

您可以查看此:http://stackoverflow.com/questions/235851/using-itext-to-convert-html-to-pdf – Beez 2011-04-06 16:52:51

回答

3

飛碟XHTML轉換爲PDF。太好了。這並不快。如果您的XHTML語法中存在輕微錯誤,它將失敗。 (如<br>當它應該是<br/>

這是讓我開始的鏈接。它似乎使用iText,但一旦你有事情的工作,只需更改HTML和它的更新。

http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html

有可能是一個更好的辦法,這是我做到了。

+0

偉大的,這使得我的生活更容易感謝帖子! – 2011-04-06 16:58:20

2

如果你的HTML源文件的樣式與CSS並不見得很好形成,嘗試PD4ML庫(免費爲非營利性使用)。

+1

歡迎來到堆棧溢出!感謝您發佈您的答案!請務必仔細閱讀[自助推廣常見問題](http://stackoverflow.com/faq#promotion)。另請注意,每次鏈接到您自己的網站/產品時,您都必須*發佈免責聲明。 – 2012-11-05 17:54:52

1

我可以推薦jodconverter它使用的OpenOffice在headless模式

1安裝(爲Linux 「zypper的安裝的LibreOffice」)

2 WIN把它放在路徑變量所以 「soffice」 OpenOffice的可運行來自世界各地,對我來說,這是 「C:\ Program Files文件(x86)的\ LibreOffice的4 \程序」

3 LINUX確保它運行的Java進程的用戶擁有自己的主目錄,因爲 OpenOffice的需要存儲CONFIGS有,對我來說,tomcat運行的過程,但它的主目錄是由根

4附加jodconverter-lib添加到您的Java項目

<dependency> 
    <groupId>com.artofsolving</groupId> 
    <artifactId>jodconverter</artifactId> 
    <version>2.2.1</version> 
</dependency> 

轉換

// ensure open office is running 
String[] commands = new String[] {"soffice","--headless","--accept=socket,host=localhost,port=8100;urp;"}; 
Runtime.getRuntime().exec(commands); 

// convert 
String html = "<div>hey there</div>"; 
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream(); 
DefaultDocumentFormatRegistry defaultDocumentFormatRegistry = new DefaultDocumentFormatRegistry(); 
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); 
connection.connect(); 
DocumentConverter converter = new OpenOfficeDocumentConverter(connection); 
converter.convert(IOUtils.toInputStream(html, Charset.forName("UTF-8")), defaultDocumentFormatRegistry.getFormatByFileExtension("html"), pdfOutputStream, defaultDocumentFormatRegistry.getFormatByFileExtension("pdf")); 
connection.disconnect(); 
byte[] pdfBytes = pdfOutputStream.toByteArray(); 
0

了JavaFx WebKit瀏覽器可用於HTML轉換爲PDF。 對於windows安裝pdf24打印機驅動程序和Linux的使用cups-pdf。 安裝後使用WebEngine的print方法。

如果使用外部庫是對你合適,你可以很容易地使用ui4j打印網頁爲PDF。

1

使用phantomjs,您可以HTML轉換爲PDF格式很容易:

import org.openqa.selenium.phantomjs.PhantomJSDriver; 
import org.openqa.selenium.phantomjs.PhantomJSDriverService; 
import org.openqa.selenium.remote.DesiredCapabilities; 

public class Screenshot { 

    public static final String SCRIPT = "var page = require('webpage').create();\n" + 
      "page.open('@@[email protected]@', function() {\n" + 
      " page.render('@@[email protected]@');\n" + 
      "});\n"; 

    public static void main(String[] args) { 

    final String url = args[0]; 
    final String file = args[1]; 
    final String script = SCRIPT.replace("@@[email protected]@", url).replace("@@[email protected]@", file); 

    final DesiredCapabilities capabilities = new DesiredCapabilities(); 
    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, 
           "/path/to/phantomjs/bin/phantomjs"); 
    try { 
     PhantomJSDriver phantomJSDriver = new PhantomJSDriver(capabilities); 
     phantomJSDriver.executePhantomJS(script); 
    } finally { 
     phantomJSDriver.close(); 
    } 
    } 

} 

如果file名稱與.pdf結束,然後該網頁將被保存爲PDF格式。 Phantomjs還支持PNG,JPG和GIF輸出。

這是一個非常簡單的例子,更一般地,屏幕快照過程是非常定製(組視口大小,啓用/禁用JavaScript的,等等)。有關更多信息,請參閱PhantomJS的screen capturing頁面。