搜索,我可能在这里没有找到解决我的问题的方法,我希望 StackOverflow 的综合思想能将我推向正确的方向。
我的问题如下,我正在开发消息系统用户代理的打印和打印预览部分。我得到了特定的 XSLT 模板,这些模板在转换 XML 后会生成一个 Formatting Objects 文档。使用 Apache FOP,我已经能够将 FO 文档呈现为 PDF,这一切都很好,但我还想在打印预览对话框中显示它。Apache FOP 包含这样一个类PreviewDialog
,它在其构造函数中需要FOUserAgent
我可以生成的 a 和一个实现Renderable
接口的对象。
该Renderable
接口在 FOP 包中有一个实现类,该类被调用InputHandler
,它在其构造函数中接收一个标准 ioFile
对象。现在这里是麻烦开始的地方。我目前将 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();
}
}
对此的任何帮助将不胜感激。