0

搜索,我可能在这里没有找到解决我的问题的方法,我希望 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();
}

}

对此的任何帮助将不胜感激。

4

3 回答 3

1

答案似乎放弃了尝试在 Apache FOP 中使用预览对话框,而是使用 Apache PDFBox 使用PDFPagePanel类生成打印预览以显示更改。

于 2011-01-13T12:32:23.580 回答
1

实际上,我没有找到隐藏工具栏中显示的调试和 fop inf 按钮的漂亮方法。我必须创建自己的 PreviewDialog,主要基于 fop 之一,但没有这两个按钮。

于 2011-02-25T18:21:12.857 回答
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);
于 2011-02-23T19:43:57.417 回答