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 tak开发者_JAVA百科es 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;
}
精彩评论