You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-users@xmlgraphics.apache.org by Fran Simon <fs...@sciquest.com> on 2002/10/23 22:11:23 UTC

unable to display pdf from servlet....

Hi All,

I am having a very strange problem.  I have no trouble generating a pdf
file from a fo file,  but I am having trouble streaming it to the
browser.  Here is a little piece of code from my servlet:

  OutputStream out=3D response.getOutputStream();
  response.setContentType( "application/pdf" );

  if (URS =3D=3D null) {
     // old reporting package
     PDF3Generator pdf=3D new PDF3Generator( out );
     pdf.generate(report);

  } else {
     // transform the XML-FO document into the requested format
     ReportTransformer transformer = new ReportTransformer();
     transformer.transform(document, out);
  }

  out.flush();
  out.close();

where the transform method is:

    public void transform(Document document, OutputStream out) {
        Driver driver =3D new Driver();
        driver.setRenderer(Driver.RENDER_PDF)

        driver.setOutputStream(out);
        driver.render(document);
    }


The problem I have is when I use FOP to generate the report the page is
displayed as a blank.  If I hit refresh on the blank page the report
regenerates and displays.   If I generate using the old reporting
package though it generates a PDF and displays fine first time.  If I 
send the FOP stuff to a file it opens fine in acrobat and in the
browser.

I should say I do a window.open('/webapp/servlet') to display the
report. So this all happens in a popup window. I have tried it in
IE 5.5 and IE 6 with the same results.

I have also tried putting the:
  response.setHeader("Content-Disposition:",
                     "attachment; filename=3Dreport.pdf"); 

after the set context.  This causes a popup which asks if I want to open
or save. If I click open it opens fine.

Does anyone have any idea what I am doing wrong? I have been trying to
figure this out for several days and I think my head is going
to explode!!!!!!

Fran Simon
SciQuest, Inc.










Re: unable to display pdf from servlet....

Posted by Oleg Tkachenko <ol...@multiconn.com>.
J.Pietschmann wrote:

> Ok, for the record again:
Well done, finished faq entry.

-- 
Oleg Tkachenko
eXperanto team
Multiconn Technologies, Israel


Re: unable to display pdf from servlet....

Posted by "J.Pietschmann" <j3...@yahoo.de>.
Fran Simon wrote:
> I am having a very strange problem.  I have no trouble generating a pdf
> file from a fo file,  but I am having trouble streaming it to the
> browser
....
> The problem I have is when I use FOP to generate the report the page is
> displayed as a blank.
...
> I should say I do a window.open('/webapp/servlet') to display the
> report. So this all happens in a popup window. I have tried it in
> IE 5.5 and IE 6 with the same results.

Ok, for the record again:
1. IEx is dumb. Actually, it tries to be smart by second-guessing,
    which turns out to be dumb.
2. You need to set the correct content length for the PDF in the
    sevlet for some (all?) IEx versions to work properly.
3. Make sure you've set the content-type to application/pdf.
4. Use URLs which end in .pdf, or whatever file extension is bound
    to PDF files *on* *the* *client*. That's because IEx looks at the
    URL and guesses what component it should launch *before* it reads
    the content type from the server. Some versions look at the
    original URL, they need /foo/bar.pdf or /foo/bar?par=v.pdf,
    some versions look at the URL after stripping the query string (the
    parameters), they need /foo/bar.pdf or /foo/bar.pdf?par=value.
    For some versions it is best to have *both*, if there is a query
    string.
    Yes, you can configure your web server+servlet to use
    /webapp/servlet.pdf or /webapp/servlet/foo.pdf.
5. Don't use a URL with an extension which might be bound on the client
    to something other than PDF. Usually /foo/bar.xml or
    /foo/bar?xsl=foobar.xsl&xml=foobar.xml will get you into trouble.
    If you can't avoid parameters which might end in with a registered
    file extension, always append a dummy parameter
    /foo/bar?xsl=foobar.xsl&xml=foobar.xml&d=.pdf
Note that the actual behaviour may depend not only on major and minor
version but also on the build number, installed patches and perhaps
installed additional components, i.e. you may find two IEx 5.5
installations handling things differently.

J.Pietschmann