如何让 Apache FOP 的 PreviewDialog 真正显示我的文档?
尽管我进行了搜索,但我还没有找到解决我的问题的方法,我希望 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();
}
}
对此的任何帮助将不胜感激。
Search as I may I have not found a solution to my problem here and I'm hoping the combined minds of StackOverflow will push me in the right direction.
My problem is as follows, I'm developing a print and print preview portion of a messaging system's user agent. I was given specific XSLT templates that after transforming XML will produce a Formatting Objects document. With Apache FOP I've been able to render the FO document into PDF which is all fine and good, but I would also like to display it in a print preview dialog. Apache FOP contains such a class called PreviewDialog
which requires in its constructor a FOUserAgent
, which I can generate, and an object implementing the Renderable
Interface.
The Renderable
Interface has one implementing class in the FOP package which is called InputHandler
which takes in its constructor a standard io File
object. Now here is where the trouble begins. I'm currently storing the FO document as a temp file and pass this as a File
object to an InputHandler
instance which is then passed to the PreviewDialog
. I see the dialog appear on my screen and along the bottom in a status bar it says that it is generating the document, and that is all it does.
Here is the code I'm trying to use. It isn't production code so it's not pretty:
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();
}
}
Any help on this would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案似乎是放弃尝试使用 Apache FOP 中的预览对话框,而是使用 Apache PDFBox 使用
PDFPagePanel
类生成打印预览来显示更改。The answer it seems to give up on trying to use the preview dialog in Apache FOP, and instead use Apache PDFBox to generate the print preview using the
PDFPagePanel
class to display the changes.实际上我没有找到一种漂亮的方法来隐藏 debug 和 fop inf 按钮,它们是
显示在工具栏中。我必须创建自己的 PreviewDialog,主要基于 fop,但没有这两个按钮。
Actually I did not find a beautiful way to hide the debug and fop inf button which are
displayed in the toolbar. I had to create my own PreviewDialog mostly based on the fop one but without these two buttons.
必须在转换为 pdf 之前创建 createPreviewDialog
The createPreviewDialog has to be created BEFORE the transformation into pdf