gpt4 book ai didi

java - 如何使用 xsl FO 在 pdf 中打印数组

转载 作者:太空宇宙 更新时间:2023-11-04 07:34:58 28 4
gpt4 key购买 nike

我有一组对象,我想以模板格式打印为 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>

最佳答案

无法使用 XSL-F(格式化)O(对象)输出值。 example you're using实际上并没有进行 XSLT 转换,而 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 数组。 请注意:您将不能只将任何类传递给样式表并期望 XSL 转换器知道它。这是行不通的。通常,传递给样式表的参数被视为字符串值。 If you pass anything else, you need to make sure your stylesheet and transformer can actually handle it.

在你的机器上运行代码,看看第二页写入了什么:这只是胡言乱语,因为样式表和转换器无法处理 int 数组。如果您确实需要处理来自自定义类的数据,我建议将您真正需要的值作为(连接的)字符串传递给样式表。

关于java - 如何使用 xsl FO 在 pdf 中打印数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17036890/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com