Apache FOP 生成空白页
我正在尝试使用 Apache FOP 和 Java 生成 PDF。我正在使用有效的 xsl-fo 文件,我可以使用命令行 FOP 创建 pdf。
当我尝试使用 Apache FOP 库运行 FOP 时,出现问题。跨 java/php 桥运行。新娘正确配置并与java / php进行通信。在java方面,我有一个函数,它接受包含xsl-fo的字符串并返回包含pdf的字符串。当我执行此函数并将输出重定向到 stdout,然后重定向到文件,或通过 java / php 桥时,pdf 显示为空白,其大小大约是我通过命令行检索的正确 pdf 的两倍。我假设我遇到了某种编码问题。
以前有人见过这个问题吗?
这是我的java代码
public String ConvertFoToPdf(String fo) {
// Will contain the results after the transformation.
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Input string
StringReader sr = new StringReader(fo);
// Should be UTF-8;
String strEncoding = Charset.defaultCharset().name();
// Resulting string.
String pdfResult = "";
try
{
// Get an instance of the fop factory
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
// Setup input stream
Source src = new StreamSource(sr);
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Set the encoding on the transformer.
transformer.setOutputProperty(OutputKeys.ENCODING, strEncoding);
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
// Put the byte array stream into a string
pdfResult = out.toString(strEncoding);
}
// Catch all exceptions for simplicities sake.
catch (Exception e){
// Log
}
return pdfResult;
}
I am trying to generate a PDF using Apache FOP and Java. I am using a valid xsl-fo file which I can create a pdf with using the command line FOP.
My problem occurs when I try to run FOP using the Apache FOP Libraries. Running across a java/php bridge. The bride is properly configured and java / php communicate. On the java side I have a function that takes in a string containing xsl-fo and returns a String which contains a pdf. When I execute this function and redirect the output to stdout then to file, or across the java / php bridge, the pdf appears blank and its size is roughly double that of the correct pdf that I retrieve via command line. I assume I am having some kind of encoding problem.
Has anyone seen this issue before?
Here is my java code
public String ConvertFoToPdf(String fo) {
// Will contain the results after the transformation.
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Input string
StringReader sr = new StringReader(fo);
// Should be UTF-8;
String strEncoding = Charset.defaultCharset().name();
// Resulting string.
String pdfResult = "";
try
{
// Get an instance of the fop factory
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
// Setup input stream
Source src = new StreamSource(sr);
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Set the encoding on the transformer.
transformer.setOutputProperty(OutputKeys.ENCODING, strEncoding);
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
// Put the byte array stream into a string
pdfResult = out.toString(strEncoding);
}
// Catch all exceptions for simplicities sake.
catch (Exception e){
// Log
}
return pdfResult;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论