搜索,因爲我可能沒有找到解決我的問題在這裏,我希望StackOverflow的組合頭腦將推動我在正確的方向。如何獲得Apache FOP的PreviewDialog實際顯示我的文檔?
我的問題如下,我正在開發郵件系統用戶代理的打印和打印預覽部分。我獲得了特定的XSLT模板,這些模板在變換XML後將生成格式化對象文檔。使用Apache FOP,我已經能夠將FO文檔渲染成PDF,這很好,但我也想在打印預覽對話框中顯示它。 Apache FOP包含一個名爲PreviewDialog
的類,它在構造函數中需要我可以生成的FOUserAgent
以及實現接口的對象。
Renderable
接口在FOP包中有一個實現類,稱爲InputHandler
,它在其構造函數中接受一個標準io File
對象。現在,這是麻煩開始的地方。我正在將FO文檔存儲爲臨時文件,並將其作爲File
對象傳遞給InputHandler
實例,然後將其傳遞給PreviewDialog
。我看到對話框出現在我的屏幕上,並且在狀態欄的底部顯示它正在生成文檔,這就是它的全部功能。
這是我正在嘗試使用的代碼。它不是生產代碼,所以它不漂亮:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.cli.InputHandler;
import org.apache.fop.render.awt.viewer.PreviewDialog;
public class PrintPreview {
public void showPreview(final File xslt, final File xmlSource) {
boolean err = false;
OutputStream out = null;
Transformer transformer = null;
final String tempFileName = this.getTempDir()
+ this.generateTempFileName();
final String tempFoFile = tempFileName + ".fo";
final String tempPdfFile = tempFileName + ".pdf";
System.out.println(tempFileName);
final TransformerFactory transformFactory = TransformerFactory
.newInstance();
final FopFactory fopFactory = FopFactory.newInstance();
try {
transformer = transformFactory
.newTransformer(new StreamSource(xslt));
final Source src = new StreamSource(xmlSource);
out = new FileOutputStream(tempFoFile);
final Result res = new StreamResult(out);
transformer.transform(src, res);
System.out.println("XSLT Transform Completed");
} catch (final TransformerConfigurationException e) {
err = true;
e.printStackTrace();
} catch (final FileNotFoundException e) {
err = true;
e.printStackTrace();
} catch (final TransformerException e) {
err = true;
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println("Initializing Preview");
transformer = null;
out = null;
final File fo = new File(tempFoFile);
final File pdf = new File(tempPdfFile);
if (!err) {
final FOUserAgent ua = fopFactory.newFOUserAgent();
try {
transformer = transformFactory.newTransformer();
out = new FileOutputStream(pdf);
out = new BufferedOutputStream(out);
final Fop fop = fopFactory.newFop(
MimeConstants.MIME_PDF, ua,
out);
final Source foSrc = new StreamSource(fo);
final Result foRes = new SAXResult(fop.getDefaultHandler());
transformer.transform(foSrc, foRes);
System.out.println("Transformation Complete");
} catch (final FOPException e) {
err = true;
e.printStackTrace();
} catch (final FileNotFoundException e) {
err = true;
e.printStackTrace();
} catch (final TransformerException e) {
err = true;
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if (!err) {
System.out.println("Attempting to Preview");
final InputHandler inputHandler = new InputHandler(fo);
PreviewDialog.createPreviewDialog(ua, inputHandler, true);
}
}
// perform the clean up
// f.delete();
}
private String getTempDir() {
final String p = "java.io.tmpdir";
return System.getProperty(p);
}
private String generateTempFileName() {
final String charset = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890";
final StringBuffer sb = new StringBuffer();
Random r = new Random();
int seed = r.nextInt();
r = new Random(seed);
for (int i = 0; i < 8; i++) {
final int n = r.nextInt(71);
seed = r.nextInt();
sb.append(charset.charAt(n));
r = new Random(seed);
}
return sb.toString();
}
}
任何幫助,將不勝感激。
感謝您的信息。我還沒有機會試用它。目前我使用的是我自己找到的解決方案,一旦我給你一個嘗試,也許我們會重構代碼並更改解決方案。 PDFBox的面板是好的,但它不會很好。我希望FOP能讓事情變得更好一些。 – JRSofty 2011-02-24 07:47:18