java - 在 XSLT 中有 URL 时使用 fop 和 XSL 生成 PDF
问题描述
在 XSLT 中有 URL 时使用 fop 和 XSL 生成 PDF
我正在使用 FOP 2.0 和 XSLT 生成 PDF。在这里,我从 web url 获取 XSL。我的一个 XSL URL 包含和导入其他 XSL 的 URL。如果它是单个 XSL,我可以生成 PDF。如果我在 Web 上的一个 XSLT 中有多个 URL。FOP 无法自动连接到其他 URL[使用 XSLTS 的示例]
xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" \
xmlns:fo="http://www.w3.org/1999/XSL/Format" 版本="1.0">
<xsl:include href="abc.xsl"/>
<xsl:include href="xyz.xsl"/>
<xsl:include href="wgh.xsl"/>
这就是将 XSL 包含在一个 XSL 中的方式。在这种情况下,我的 FOP 没有重定向到那些 xsls 并且无法生成 PDF
错误:
系统 ID 未知;第 3 行;第 34 列;样式表文件出现 IO 异常:header.xsl SystemId Unknown;第 4 行;第 34 列;样式表文件出现 IO 异常:footer.xsl SystemId Unknown;第 5 行;第 36 列;样式表文件出现 IO 异常:mainbody.xsl SystemId Unknown;第 6 行;第 41 列;样式表文件出现 IO 异常:secondarybody.xsl SystemId Unknown;第 10 行;第 38 列;org.xml.sax.SAXException:ElemTemplateElement 错误:布局 javax.xml.transform.TransformerException:ElemTemplateElement 错误:布局 13:58:27.326 [http-nio-auto-1-exec-2] 调试 org.apache.fop.fo .FOTreeBuilder - 构建格式化对象树 SystemId 未知;第 10 行;第 38 列;找不到名为:布局的模板
PDF 生成器的代码:
公共类 PdfGenerator {
private static final Logger LOG=LoggerFactory.getLogger(PdfGenerator.class);
public List<OutputStream> generatePdfs(List<Content> xmlList, int reqestListSize,String xslPath)
{ 尝试 {
List<OutputStream> pdfOutputStreams= new ArrayList();
for(int p = 0; p <reqestListSize; p++) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
String jaxbType = "com.abc.model"; // model package
JAXBContext context = JAXBContext.newInstance(jaxbType);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty("jaxb.formatted.output",Boolean.TRUE);
marshaller.marshal(xmlList.get(p),bos);
ByteArrayInputStream inStream = new ByteArrayInputStream(bos.toByteArray());
StreamSource xmlSource = new StreamSource(inStream);
// create an instance of fop factory
FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
// a user agent is needed for transformation
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
ByteArrayOutputStream tempOutputStream = new ByteArrayOutputStream();
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, tempOutputStream);
pdfOutputStreams.add(p, tempOutputStream);
// Setup XSLT
TransformerFactory transformerFactory = TransformerFactory.newInstance();
URL url = new URL(xslPath);
InputStream xslFile = url.openStream(); ( **http://home.www.test.com/abc_web/xsl/test.xsl** ( Using an url to get XSLT. faild loading due to XSL :include) )
StreamSource xsltStreamSource = new StreamSource(xslFile);
Transformer transformer = transformerFactory.newTransformer(xsltStreamSource);
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
// That's where the XML is first transformed to XSL-FO and then
// PDF is created
transformer.transform(xmlSource, res);
}
return pdfOutputStreams;
}catch(Exception ex) {
LOG.error("Error", ex);
return new ArrayList();
}
解决方案
只需更换
URL url = new URL(xslPath);
InputStream xslFile = url.openStream();
StreamSource xsltStreamSource = new StreamSource(xslFile);
和
StreamSource xsltStreamSource = new StreamSource(xslPath);
并且 XSLT 处理器应该能够解析任何相关的导入或包含。
或者你需要SystemId
在你的xsltStreamSource
. 但是我建议的单行应该可以很好地完成这项工作。
推荐阅读
- python - 用自定义宽度动画 Kivy Bezier 曲线?(有点傻的问题)
- python - python tkinter,是否可以将多个函数绑定到一个
? - android - 在android studio的内部存储中删除
- javascript - 我的 p5.js 代码滞后我不知道为什么?
- ruby-on-rails - 如何防止未经授权的用户在 Devise 中注册?
- vb.net - 我无法使用 websocket 接收任何数据
- python - 我可以矢量化两个基于纬度/经度寻找最接近邮政编码的 for 循环吗?
- c - 另一个网格中的 Gtk.Grid 在窗口上不可见
- omnet++ - Omnet ++中NED中接口实体和门项之间的区别?
- flutter-layout - 在颤动中重复背景图案图像