Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3699592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:07:18+00:00 2026-05-19T05:07:18+00:00

Search as I may I have not found a solution to my problem here

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T05:07:18+00:00Added an answer on May 19, 2026 at 5:07 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem using the Java search function in Eclipse on a particular
I search for nurple in a file. I found it, great. But now, every
Which search engine would you recommend for a Commerce website? We have millions of
I implemented a Lucene search solution awhile back, and it got me interested in
Search the web for the phrase I cosay. I run across this phrase being
@search_results = Array.new duplicates = Set.new results.each { |result| @search_results.push(result) unless duplicates.add?(result[:url]) } This
i search a good tutorial to learn how to use entity framework in the
Binary search is harder to implement than it looks. Although the basic idea of
When implementing a needle search of a haystack in an object-oriented way, you essentially
I want to implement search functionality for a website (assume it is similar to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.