我仍在尝试从 xml 生成 pdf。我找到了一种通过 HTML 的方法,但我会让任何修改,代码仍然包含错误。
你能帮助我吗?生无可恋 :/
一个使用示例代码,我只编辑了 xsl - http://thinktibits.blogspot.cz/2011/05/java-itext-convert-xml-to-pdf-example.html
import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
Java:
public class FAtoPDF {
public static void main(String[] args)
throws IOException, TransformerException,TransformerConfigurationException,FileNotFoundException, DocumentException {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource("sample.xsl"));
transformer.transform(new StreamSource("report.xml"),new StreamResult(new FileOutputStream("sample.html")));
String File_To_Convert = "sample.html";
String url = new File(File_To_Convert).toURI().toURL().toString();
//System.out.println(""+url);
String HTML_TO_PDF = "ConvertedFile.pdf";
OutputStream os = new FileOutputStream(HTML_TO_PDF);
ITextRenderer renderer = new ITextRenderer();
//renderer.setDocument(url);
renderer.setDocument(new File("sample.html"));
renderer.layout();
renderer.createPDF(os);
os.close();
}
}
XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='html' version='4.0' encoding='ISO-8859-1' indent='yes' />
<xsl:template match="/">
<html>
<body>
<h2>FAP - Report from FIO Banka</h2>
<table>
<tr style="background-color:#9acd32;">
<th align="left">ID pohybu</th>
<th align="left">Datum</th>
<th align="left">Objem</th>
<th align="left">Mena</th>
<th align="left">Protiucet</th>
<th align="left">Kod banky</th>
<th align="left">Nazev banky</th>
<th align="left">KS</th>
<th align="left">Uzivatel. identifikace</th>
<th align="left">Typ</th>
<th align="left">Provedl</th>
<th align="left">Komentar</th>
<th align="left">ID Pokynu</th>
</tr>
<xsl:for-each select="AccountStatement/TransactionList/Transaction">
<tr>
<td><xsl:value-of select="column_22"/></td>
<td><xsl:value-of select="column_0"/></td>
<td><xsl:value-of select="column_1"/></td>
<td><xsl:value-of select="column_14"/></td>
<td><xsl:value-of select="column_2"/></td>
<td><xsl:value-of select="column_3"/></td>
<td><xsl:value-of select="column_12"/></td>
<td><xsl:value-of select="column_4"/></td>
<td><xsl:value-of select="column_7"/></td>
<td><xsl:value-of select="column_8"/></td>
<td><xsl:value-of select="column_9"/></td>
<td><xsl:value-of select="column_25"/></td>
<td><xsl:value-of select="column_17"/></td>
</tr>
</xsl:for-each>
</table>
<p>Vygenerovano </p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
问题还可能是 XML/HTML 包含诸如“ščřžýáíéě”之类的字符......
HTML: http: //pastebin.com/qSPUpV1u