简体   繁体   English

使用FOP和XSL-FO时,Result包含哪些信息?如何使用?

[英]What information does Result contain when using FOP and XSL-FO, and how can I use it?

I am using Apache FOP in conjunction with XSL-FO to convert XML into a formatted PDF. 我将Apache FOP与XSL-FO结合使用,将XML转换为格式化的PDF。 Right now I have the following method in Java (ignore the fact that the method is not returning a boolean for now): 现在,我在Java中具有以下方法(忽略该方法暂时不返回布尔值的事实):

public boolean Transform() throws TransformerException, FOPException
{
  Transformer lTransformer;
  java.io.ByteArrayOutputStream lOutStream = new java.io.ByteArrayOutputStream();

  FopFactory lFopFactory = FopFactory.newInstance();
  FOUserAgent lFOAgent = lFopFactory.newFOUserAgent();

  lTransformer = getTransformer(mXsltSource); // returns a new Transformer
  Fop fop = lFopFactory.newFop(mMimeOut, lFOAgent, lOutStream);
  Result res = new SAXResult(fop.getDefaultHandler());

  lTransformer.transform(mSource, res); // transforms xml source to formatted XSL-FO transform
}

However, I am unsure of how to use "res" to make sure that the "lTransformer.transform(StreamSource, Result)" method executed correctly (I am assuming that is the purpose of the Result object). 但是,我不确定如何使用“ res”来确保正确执行“ lTransformer.transform(StreamSource,Result)”方法(我假设这是Result对象的目的)。 I have looked at the javadocs for the transform method, Result class, and SAXResult class, but these have not yielded much help. 我已经看过javadocs的transform方法,Result类和SAXResult类,但是这些并没有产生太大帮助。 Can anybody offer some insight into this? 有人可以对此提供一些见识吗?

TL;DR; TL; DR; What is the purpose of passing a Result in javax.xml.transform.Transformer.transform(StreamSource, Result) , and how can I use this to check that the operation completed successfully? javax.xml.transform.Transformer.transform(StreamSource,Result)中传递结果的目的是什么,如何使用它来检查操作是否成功完成?

The result of the transform is written to that Result object. 转换的结果将写入该Result对象。 If you look at the chain of objects that go into creating the Result object, the output stream you've specified is ultimately where the result is written (you passed a ByteArrayOutputStream into FopFactory.newFop() , and then pass the Fop handler into the Result - the output stream is the ultimate destination of your xsl transformation. 如果查看创建Result对象的对象链,则指定的输出流最终将是写入结果的位置(将ByteArrayOutputStream传递给FopFactory.newFop() ,然后将Fop处理程序传递给Result -输出流是xsl转换的最终目标。

Pass in a FileOutputStream pointing to a file on your disk somewhere instead of the ByteArrayOutputStream , and after the transformation (if everything worked), the file should be a PDF you can view. 传递一个FileOutputStream指向您的磁盘上的文件,而不是某个地方的ByteArrayOutputStream ,并转换(如果一切正常)后,该文件应该是你可以查看PDF文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM