搜索我可能没有在这里找到解决问题的方法,我希望StackOverflow的综合思想能够把我推向正确的方向。
我的问题如下,我正在开发邮件系统的用户代理的打印和打印预览部分。我获得了特定的XSLT模板,在转换XML之后将生成格式化对象文档。使用Apache FOP,我已经能够将FO文档呈现为PDF,这一切都很好,但我还想在打印预览对话框中显示它。 Apache FOP包含一个名为PreviewDialog
的类,它在构造函数中需要FOUserAgent
,我可以生成它,以及实现Renderable
接口的对象。
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();
}
}
对此有任何帮助将不胜感激。
答案 0 :(得分:1)
在尝试使用Apache FOP中的预览对话框时似乎放弃了答案,而是使用Apache PDFBox使用PDFPagePanel
类生成打印预览以显示更改。
答案 1 :(得分:1)
实际上我没有找到一种漂亮的方法来隐藏调试和fop inf按钮 显示在工具栏中。我不得不创建我自己的PreviewDialog,主要是基于fop,但没有这两个按钮。
答案 2 :(得分:0)
必须在转换为pdf
之前创建createPreviewDialog FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
AWTRenderer renderer = new AWTRenderer();
renderer.setPreviewDialogDisplayed(false);
foUserAgent.setRendererOverride(renderer);
renderer.setUserAgent(foUserAgent);
PreviewDialog previewDlg = PreviewDialog.createPreviewDialog(foUserAgent, null, false);
renderer.setStatusListener(previewDlg);
renderer.clearViewportList();
TransformerFactory transformFactory = TransformerFactory.newInstance();
Transformer transformerFo = transformFactory.newTransformer(new StreamSource(xslt));
FileOutputStream fileoutFo = new FileOutputStream(tempFoFile);
BufferedOutputStream outFo = new BufferedOutputStream(fileoutFo);
transformerFo.transform (new StreamSource(xml),new StreamResult(outFo));
Transformer transformerPdf = transformFactory.newTransformer();
FileOutputStream fileoutPdf = new FileOutputStream(tempPdfFile);
BufferedOutputStream outPdf = new BufferedOutputStream(fileoutPdf);
final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
foUserAgent, outPdf);
Source srcPdf = new StreamSource(fo);
Result resPdf = new SAXResult(fop.getDefaultHandler());
transformerPdf.transform(srcPdf, resPdf);