我需要从JSON对象生成PDF文档。我正在做以下
在使用XSL将XML转换为HTML时,我在XSL中使用<img>
标记的图像,该图像未解析并且正在生成的PDF中。除了那个图像,其余的内容在PDF中也很好。
我的网络应用程序中的src/main/resource
目录中有这些图像。
我如何在XSL中包含图像以生成PDF格式。
我的网络应用程序资源目录中有这些图像。
请找我的XSL:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/receipt">
<html>
<head>
</head>
<body>
<table style="width: 100%;">
<tr>
<td style="text-align: center; width: 25%;">
<img src="img/Open.png" style="width: 50%;" />
</td>
</td>
<td style="font-size: 12px; vertical-align: top; width: 25%;">
46-68 Kléber<br/>
Tél: +1 <br/>
Fax: +1
</td>
<td style="font-size: 12px; vertical-align: top; width: 25%;">
725 € <br />
</td>
<td style="text-align: center; width: 25%;">Date <xsl:value-of select="date" /></td>
</tr>
</table>
</body>
</html>
请找到我的XML:
<?xml version="1.0" encoding="UTF-8"?>
<receipt>
<incoterm_term>DAP</incoterm_term>
<oracle_number>5019346</oracle_number>
<address_name>FKR KREFELD REGELTECHNIK KG</address_name>
</receipt>
请找到用于将JSON转换为PDF的java代码
public String getOrderAcknowledgementPDF(JSONObject obj) throws IOException, DocumentException {
ClassLoader classLoader = getClass().getClassLoader();
StringBuilder xml = new StringBuilder();
String xmlheader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
xml.append(xmlheader);
xml.append("\n");
xml.append("<receipt>");
xml.append(XML.toString(obj));
xml.append("</receipt>");
Transformer transformer;
try {
transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new File(xslPath)));
} catch (TransformerConfigurationException | TransformerFactoryConfigurationError e1) {
throw ExceptionUtils.handle((Exception) e1);
}
DOMResult result = new DOMResult();
try {
transformer.transform(new StreamSource(new ByteArrayInputStream(xml.toString().getBytes("UTF-8"))), result);
} catch (TransformerException e) {
throw ExceptionUtils.handle(e);
}
ITextRenderer renderer = new ITextRenderer();
try {
renderer.getFontResolver().addFont("C:/WINDOWS/FONTS/ARIAL.TTF", true);
} catch (DocumentException e) {
throw ExceptionUtils.handle(e);
}
Document resultDoc = (Document) result.getNode();
renderer.setDocument(resultDoc, "");
renderer.layout();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
renderer.createPDF(baos);
baos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());
}