是否有任何API /解決方案可以從XML文件數據和定義生成PDF報告。 例如XML定義/數據可能是:從XML或HTML生成PDF文件
<pdf>
<paragraph font="Arial">Title of report</paragraph>
</pdf>
將HTML轉換爲PDF也將很好地解決我的感覺。
目前我們使用iText API編寫Java代碼。我想將代碼外部化,以便非技術人員可以編輯和更改。
是否有任何API /解決方案可以從XML文件數據和定義生成PDF報告。 例如XML定義/數據可能是:從XML或HTML生成PDF文件
<pdf>
<paragraph font="Arial">Title of report</paragraph>
</pdf>
將HTML轉換爲PDF也將很好地解決我的感覺。
目前我們使用iText API編寫Java代碼。我想將代碼外部化,以便非技術人員可以編輯和更改。
看一看Apache FOP。使用XSLT樣式表將XML(或XHTML)轉換爲XSL-FO。然後使用FOP讀取XSL-FO文檔並將其格式化爲PDF文檔(請參閱Hello World with FOP)。
只要你不想創建一個200頁的pdf,FOP就能正常工作,因爲你會遇到內存問題。 – 2009-11-22 00:47:25
有關FOP和內存,請參閱http://xmlgraphics.apache.org/fop/0.94/running.html#memory。 – 2009-11-22 01:34:38
請參閱我的回答,以獲取有關「XSLT樣式表」的建議。 – 2009-11-22 11:29:28
Prince是那裏最好的工具之一。它使用CSS來表示樣式,所以如果您欣賞將數據從顯示中分離出來的方法(閱讀:您的用戶也可以這麼做),它可能非常適合您。 (瀏覽器通過CSS提供的顯示控制是比較原始的。)
iText有一個用於從XML(和HTML,我認爲)生成PDF的工具。 Here is the DTD,但我覺得很難理清。除此之外,我從來沒有找到任何支持的文檔。我的方法是查看SAXiTextHandler和ElementTags的來源,找出可接受的內容。雖然不理想,但它非常直接。
<itext orientation="portrait" pagesize="LETTER" top="36" bottom="36" left="36" right="36" title="My Example" subject="My Subject" author="Me">
<paragraph size="8" >This is an example</paragraph>
</itext>
...
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.xml.SAXiTextHandler;
...
String inXml = ""; //use xml above as an example
ByteArrayOutputStream temp = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter writer = null;
try
{
writer = PdfWriter.getInstance(document, temp);
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(new ByteArrayInputStream(inXml), new SAXiTextHandler(document));
}
catch (Exception e)
{
// instead, catch the proper exception and do something meaningful
e.printStackTrace();
}
finally
{
if (writer != null)
{
try
{
writer.close();
}
catch (Exception ignore)
{
// ignore
}
} // if
}
//temp holds the PDF
看看JasperReports,它使用iText的導出文件,我想,它的IDE簡單,可以通過非程序員使用。
編輯:我忘了提,你可以直接在應用程序中使用JasperReports引擎,也可以使用iReport "Designer for JasperReports"
您將要使用很好地支持XML格式這一點,因爲它可以讓你的槓桿作用他人的工作。
良好支持的XML格式是DocBook XML - http://www.docbook.org/ - 而這個 - http://sagehill.net/docbookxsl/index.html - 似乎是使用帶有Docbook樣式表和其他格式的XSLT進行XML - > PDF的好資源。
該方法允許您使用任何XSLT處理器和任何XSL/FO處理器來獲得您的結果。這使您可以輕鬆編寫腳本以及根據需要自由切換實現 - 特別是當生成的PDF變得「太大」時,較早的Apache FOP實現嚴重退化。
關於「CSS + XHTML to PDF」技術,請參見[爲什麼使用XSL-FO代替CSS2,用於將HTML轉換爲良好的PDF?](http://stackoverflow.com/q/10641667/287948) 問題和答案。 – 2012-07-26 19:29:23