Java apachefop生成空白页
Java apachefop生成空白页,java,php,apache-fop,Java,Php,Apache Fop,我正在尝试使用ApacheFop和Java生成PDF。我使用的是一个有效的xsl fo文件,可以使用命令行FOP创建pdf 当我尝试使用ApacheFOP库运行FOP时,就会出现问题。运行在java/php桥上。bride已正确配置,java/php可以通信。在java方面,我有一个函数,它接收包含xsl fo的字符串并返回包含pdf的字符串。当我执行这个函数并将输出重定向到stdout,然后重定向到file,或者通过java/php桥时,pdf显示为空白,其大小大约是我通过命令行检索的正确pd
我正在尝试使用ApacheFop和Java生成PDF。我使用的是一个有效的xsl fo文件,可以使用命令行FOP创建pdf 当我尝试使用ApacheFOP库运行FOP时,就会出现问题。运行在java/php桥上。bride已正确配置,java/php可以通信。在java方面,我有一个函数,它接收包含xsl fo的字符串并返回包含pdf的字符串。当我执行这个函数并将输出重定向到stdout,然后重定向到file,或者通过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;
}
作为测试,您可以将一个有效的.pdf吸入字节数组流,将其转换为字符串,然后将其另存为.pdf。您肯定有编码错误。为什么不直接将字节数组保存到文件中呢?当我将文件读入字节数组,然后通过std out将其输出到文件中时,我仍然存在问题。然后,您似乎无法将二进制文件输出到控制台并期望它们正常工作。控制台无法在字节级别准确复制,这正是您想要做的。