Apache FOP と Java を使用して PDF を生成しようとしています。コマンドラインFOPを使用してpdfを作成できる有効なxsl-foファイルを使用しています。
Apache FOP ライブラリを使用して FOP を実行しようとすると、問題が発生します。Java/php ブリッジを介して実行されます。花嫁は適切に構成され、java / php が通信します。Java 側では、xsl-fo を含む文字列を受け取り、pdf を含む文字列を返す関数があります。この関数を実行し、出力を stdout にリダイレクトしてからファイルにリダイレクトするか、Java / php ブリッジを介してリダイレクトすると、pdf は空白で表示され、そのサイズはコマンド ラインで取得した正しい pdf の約 2 倍になります。ある種のエンコーディングの問題があると思います。
誰もこの問題を見たことがありますか?
これが私の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;
}