我有一组对象,我想以模板化格式打印到pdf。 我尝试使用下面的代码将该集合打印为PDF,但在创建文件后,我看不到任何以PDF格式呈现的文本。
java代码如下:
FopFactory fopFactory = FopFactory.newInstance();
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("/home/myfile.pdf")));
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Eden[] ed=new Eden[3];
for(int i=0;i<ed.length;i++)
{
ed[i]=new Eden();
ed[i].setBlow(String.valueOf(i));
}
Source src = new StreamSource(new File("/home/tables.fo"));
Result res = new SAXResult(fop.getDefaultHandler());
transformer.setParameter("dep", ed);
transformer.transform(src, res);
} finally {
out.close();
}
fo文件如下:
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xsl="http://www.w3.org /1999/XSL/Transform">
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page">
<fo:region-body margin="0.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body" font="7pt Times">
<fo:block border="thin solid black" text-align="center">
<xsl:for-each select="dep">
{$blow}
</xsl:for-each>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
答案 0 :(得分:1)
无法使用XSL- F (ormatting) O (对象)输出值。 example you're using实际上并没有进行XSLT转换,这需要能够将值传递给样式表。
以下代码将执行您想要的操作,唯一的区别在于您的代码是XSLT转换:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
public class test {
public static void main(String[] args) throws FOPException, TransformerException, IOException
{
/*..*/
// Step 1: Construct a FopFactory
// (reuse if you plan to render multiple documents!)
FopFactory fopFactory = FopFactory.newInstance();
// Step 2: Set up output stream.
// Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("F:/myfile.pdf")));
try
{
// Step 3: Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
// Step 5: Setup input and output for XSLT transformation
// Setup input stream
Source xsltSource = new StreamSource(new File("F:/myfile.xsl"));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xsltSource);
int[] array = {1,2,3};
transformer.setParameter("test", "1234");
transformer.setParameter("testRaw", array);
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(xsltSource, res);
}
finally
{
out.close();
}
}
}
现在,要在XSL样式表中使用该参数(myfile.xsl对我来说),它必须如下所示:
<xsl:stylesheet version="2.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="test"/>
<xsl:param name="testRaw"/>
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page">
<fo:region-body margin="0.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body" font="7pt Times">
<fo:block border="thin solid black" text-align="center">
<xsl:value-of select="$test"/>
</fo:block>
<fo:block break-before="page" border="thin solid black" text-align="center">
<xsl:value-of select="$testRaw"/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
此样式表定义了两个参数:“test”和“testRaw”,它们都输出到两个单独的页面。
为此,请使用<xsl:value-of select="$paramName"/>
。
在我发布的代码中,这两个值是一个字符串和一个int数组。 请注意:您将 NOT 能够将任何类传递给样式表,并期望XSL转换器了解它。这不行。通常,传递给样式表的参数被视为字符串值。 If you pass anything else, you need to make sure your stylesheet and transformer can actually handle it.
在您的机器上运行代码并查看写入第二页的内容:这只是乱码,因为样式表和变换器无法处理int数组。如果你真的需要处理来自自定义类的数据,我建议将你真正需要的值作为(连接的)字符串传递给样式表。