You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by Carmelo Montanez <ca...@nist.gov> on 2001/09/24 21:16:43 UTC

Anything wrong with this code>

Hi All:

   I know that my browser can display PDF propertly now, however the following
piece of code (that generates PDF data) sends the data back to the client 
(IE6) and still  does not not looks like PDF at all.  Can someone please tell me
if there is something wrong with this code.  By the way
(out = response.getOutputStream() and "foFile" comes from an "FO" file).



    public void renderFO(InputSource foFile, HttpServletResponse response)
     throws IOException
    {
      try {
        ByteArrayOutputStream outPDF = new ByteArrayOutputStream();
        response.setContentType("application/pdf");
       
          Driver driver = new Driver(foFile, outPDF);
          driver.setRenderer(Driver.RENDER_PDF);
          driver.setOutputStream(outPDF);
          driver.run();

          byte[] content = outPDF.toByteArray();
          response.setContentLength(content.length);
          out.write(content);
          out.flush();
      }     
      catch (Exception ex) {
          System.out.println(ex);
      }
    
Thanks,
Carmelo Montanez

Carmelo Montanez
NIST Stop 8970
Gaithersburg, MD 20899

carmelo@nist.gov


Re: Anything wrong with this code (please look at attachements)

Posted by Alex McLintock <al...@yahoo.com>.
 --- Carmelo Montanez <ca...@nist.gov> wrote: > Thanks it worked for me too now both in Netscape
and
> IE.  However what if I wanted to embed to PDF results from
> FOP inside some HTML code for formatting
> purposes?, is that possible at all.  What I mean is
> is possible to send the PDF and the HTML back to
> the client in some fashion?

Forget FOP for a minute and ask yourself whether such a thing is possible at all?

Where have you seen an html document with PDF embedded in it? It *might* be possible
to use the acrobat plugin in this way but try to find an example and I bet you
can't. Whenever I have seen the acrobat plugin it either opens up a new window or
takes over the entire window. 

You can of course send back an html page which *links* to other pdf documents, 
but that is about it. 

Then you might want to look at frames - it might be possible to have html in one
frame and a pdf document in another...

none of this has anything to do with FOP though.

Alex



=====
Alex McLintock        alex@OWAL.co.uk    Open Source Consultancy in London
OpenWeb Analysts Ltd, http://www.OWAL.co.uk/ 
SF and Computing Book News and Reviews: http://news.diversebooks.com/
Get Your XML T-Shirt <t-shirt/> at http://www.inversity.co.uk/

____________________________________________________________
Do You Yahoo!?
Get your free @yahoo.co.uk address at http://mail.yahoo.co.uk
or your free @yahoo.ie address at http://mail.yahoo.ie

---------------------------------------------------------------------
To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
For additional commands, email: fop-dev-help@xml.apache.org


Re: Anything wrong with this code (please look at attachements)

Posted by Carmelo Montanez <ca...@nist.gov>.
Thanks it worked for me too now both in Netscape and
IE.  However what if I wanted to embed to PDF results from
FOP inside some HTML code for formatting
purposes?, is that possible at all.  What I mean is
is possible to send the PDF and the HTML back to
the client in some fashion?

Greetings,
Carmelo
----- Original Message ----- 
From: "Matt Savino" <ma...@synergizethis.com>
To: <fo...@xml.apache.org>
Sent: Wednesday, September 26, 2001 7:20 PM
Subject: Re: Anything wrong with this code (please look at attachements)


> For one thing it looks like you are mixing PDF and HTML content in your
> response:
> 
>       ServletOutputStream out = response.getOutputStream();
>       try {
>         out.println("<HTML>\n");
>         out.println("<HEAD>\n");
>         out.println("<BODY>\n");
>         out.println("<CENTER><H1>FOP and PDF
> Creation.</H1></CENTER>\n");
>         renderFO(response);
>         out.println("</BODY>\n");
>         out.println("</HTML>\n");
>      }
> and 
>        
>    byte[] content = outPDF.toByteArray();
>           response.setContentLength(content.length);
>           response.getOutputStream().write(content);
>           response.getOutputStream().println("<BR><BR>");
>           response.getOutputStream().println("There should be a PDF
> image above");
>           response.flushBuffer();
>           response.getOutputStream().flush();
> 
> For some reason, Netscape doesn't mind this. But IE seems to create an
> Http error that's related to the discrepancy in the content length of
> the response. (At least with Weblogic). I removed all your 
> out.println("..") calls and the code works.
> 
> Also I have noticed a weird bug where IE sometimes doesn't recognize
> PDF, even though you set the response type to "application/pdf". I was
> using the FopServlet example from .20.1. It worked fine if used
> Netscape. The weird thing is if I clicked back, then forward in IE, it
> recognized the PDF and loaded the plugin. I think I solved the problem
> by mapping the servlet URI to FopServletTest.pdf and sending the FO
> filename as a POST form instead of a GET. 
> 
> Hope this helps,
> Matt
> 
> > Carmelo Montanez wrote:
> > 
> > Hello all
> > 
> > Thanks Sempri for all your comments.  For anyone interested, I am
> > attaching a very
> > slimed down copy of my Servlet.  I am hardwiring the file name (FO
> > file) inside the
> > servlet (for convenience of running). I am also attaching a "dummy"
> > HTML file
> > to call the servlet (no parameters are retrieved from the HTML by the
> > servlet).
> > I am also attaching the FO file
> > 
> > I am running this using Tomcat.   If anyone please try an attempt to
> > run the
> > servlet.  I am still getting "stuff" (as you will see) on
> > the browser.  Please look
> > at the "import" statements (most of them are comments) to see
> > which classes you need, I can can provide them if need be.
> > 
> > I am using FOP's latest version.
> > 
> > Greetings,
> > Carmelo
> > 
> >                    Name: testPDF.java
> >    testPDF.java    Type: unspecified type (application/octet-stream)
> >                Encoding: quoted-printable
> > 
> >                 Name: test.html
> >    test.html    Type: Hypertext Markup Language (text/html)
> >             Encoding: 7bit
> > 
> >                                  Name: cbpbp-background-color4.fo
> >    cbpbp-background-color4.fo    Type: unspecified type
> >                                        (application/octet-stream)
> >                              Encoding: quoted-printable
> > 
> >     ---------------------------------------------------------------
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
> > For additional commands, email: fop-dev-help@xml.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
> For additional commands, email: fop-dev-help@xml.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
For additional commands, email: fop-dev-help@xml.apache.org


Re: Anything wrong with this code (please look at attachements)

Posted by Matt Savino <ma...@synergizethis.com>.
For one thing it looks like you are mixing PDF and HTML content in your
response:

      ServletOutputStream out = response.getOutputStream();
      try {
        out.println("<HTML>\n");
        out.println("<HEAD>\n");
        out.println("<BODY>\n");
        out.println("<CENTER><H1>FOP and PDF
Creation.</H1></CENTER>\n");
        renderFO(response);
        out.println("</BODY>\n");
        out.println("</HTML>\n");
     }
and 
       
	   byte[] content = outPDF.toByteArray();
          response.setContentLength(content.length);
          response.getOutputStream().write(content);
          response.getOutputStream().println("<BR><BR>");
          response.getOutputStream().println("There should be a PDF
image above");
          response.flushBuffer();
          response.getOutputStream().flush();

For some reason, Netscape doesn't mind this. But IE seems to create an
Http error that's related to the discrepancy in the content length of
the response. (At least with Weblogic). I removed all your 
out.println("..") calls and the code works.

Also I have noticed a weird bug where IE sometimes doesn't recognize
PDF, even though you set the response type to "application/pdf". I was
using the FopServlet example from .20.1. It worked fine if used
Netscape. The weird thing is if I clicked back, then forward in IE, it
recognized the PDF and loaded the plugin. I think I solved the problem
by mapping the servlet URI to FopServletTest.pdf and sending the FO
filename as a POST form instead of a GET. 

Hope this helps,
Matt

> Carmelo Montanez wrote:
> 
> Hello all
> 
> Thanks Sempri for all your comments.  For anyone interested, I am
> attaching a very
> slimed down copy of my Servlet.  I am hardwiring the file name (FO
> file) inside the
> servlet (for convenience of running). I am also attaching a "dummy"
> HTML file
> to call the servlet (no parameters are retrieved from the HTML by the
> servlet).
> I am also attaching the FO file
> 
> I am running this using Tomcat.   If anyone please try an attempt to
> run the
> servlet.  I am still getting "stuff" (as you will see) on
> the browser.  Please look
> at the "import" statements (most of them are comments) to see
> which classes you need, I can can provide them if need be.
> 
> I am using FOP's latest version.
> 
> Greetings,
> Carmelo
> 
>                    Name: testPDF.java
>    testPDF.java    Type: unspecified type (application/octet-stream)
>                Encoding: quoted-printable
> 
>                 Name: test.html
>    test.html    Type: Hypertext Markup Language (text/html)
>             Encoding: 7bit
> 
>                                  Name: cbpbp-background-color4.fo
>    cbpbp-background-color4.fo    Type: unspecified type
>                                        (application/octet-stream)
>                              Encoding: quoted-printable
> 
>     ---------------------------------------------------------------
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
> For additional commands, email: fop-dev-help@xml.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
For additional commands, email: fop-dev-help@xml.apache.org


Anything wrong with this code (please look at attachements)

Posted by Carmelo Montanez <ca...@nist.gov>.
Hello all

Thanks Sempri for all your comments.  For anyone interested, I am attaching a very
slimed down copy of my Servlet.  I am hardwiring the file name (FO file) inside the
servlet (for convenience of running). I am also attaching a "dummy" HTML file
to call the servlet (no parameters are retrieved from the HTML by the servlet).
I am also attaching the FO file

I am running this using Tomcat.   If anyone please try an attempt to run the
servlet.  I am still getting "stuff" (as you will see) on the browser.  Please look
at the "import" statements (most of them are comments) to see
which classes you need, I can can provide them if need be.

I am using FOP's latest version.

Greetings,
Carmelo

Re: Anything wrong with this code>

Posted by Semprini Davide <da...@cineca.it>.
Hi,

> When the PDF data is sent back to my client.  Am I suppose to tell my 
> client
>
> (IE6) that this is a PDF file (or a stream).  I do set 
> response.contentType to
>
> "application/pdf". 
>
This is an information that you send correctly to the client setting 
contentType() with this code:

response.setContentType("application/pdf");

> Am I suppose to do something else?
>
what do you want to do?

D.Semprini

>  
>
> Carmelo
>
>  
>
>  
>
>     ----- Original Message -----
>
>     From: Semprini Davide <ma...@cineca.it>
>
>     To: fop-dev@xml.apache.org <ma...@xml.apache.org>
>
>     Sent: Tuesday, September 25, 2001 3:51 AM
>
>     Subject: Re: Anything wrong with this code>
>
>
>     Hi,
>
>     PDF file is a binary file!!!
>     Use this code it's work:
>
>     byte[] content = outPDF.toByteArray();
>        response.setContentLength(content.length);
>        response.getOutputStream().write(content);
>        response.getOutputStream().flush();
>
>     D.Semprini
>
>
>     Carmelo Montanez wrote:
>
>>     Hi All:
>>
>>      
>>
>>        I know that my browser can display PDF propertly now, however
>>     the following
>>
>>     piece of code (that generates PDF data) sends the data back to
>>     the client
>>
>>     (IE6) and still  does not not looks like PDF at all.  Can someone
>>     please tell me
>>
>>     if there is something wrong with this code.  By the way
>>
>>     (out = response.getOutputStream() and "foFile" comes from an "FO"
>>     file).
>>
>>      
>>
>>      
>>
>>      
>>
>>         public void renderFO(InputSource foFile, HttpServletResponse
>>     response)
>>          throws IOException
>>         {
>>           try {
>>             ByteArrayOutputStream outPDF = new ByteArrayOutputStream();
>>             response.setContentType("application/pdf");
>>           
>>               Driver driver = new Driver(foFile, outPDF);
>>               driver.setRenderer(Driver.RENDER_PDF);
>>               driver.setOutputStream(outPDF);
>>               driver.run();
>>
>>      
>>
>>               byte[] content = outPDF.toByteArray();
>>               response.setContentLength(content.length);
>>               out.write(content);
>>               out.flush();
>>           }    
>>           catch (Exception ex) {
>>               System.out.println(ex);
>>           }
>>
>>        
>>
>>     Thanks,
>>
>>     Carmelo Montanez
>>
>>      
>>
>>     Carmelo Montanez
>>     NIST Stop 8970
>>     Gaithersburg, MD 20899
>>
>>      
>>
>>     carmelo@nist.gov <ma...@nist.gov>
>>
>


Re: Anything wrong with this code>

Posted by Carmelo Montanez <ca...@nist.gov>.
When the PDF data is sent back to my client.  Am I suppose to tell my client
(IE6) that this is a PDF file (or a stream).  I do set response.contentType to 
"application/pdf".  Am I suppose to do something else?

Carmelo


  ----- Original Message ----- 
  From: Semprini Davide 
  To: fop-dev@xml.apache.org 
  Sent: Tuesday, September 25, 2001 3:51 AM
  Subject: Re: Anything wrong with this code>


  Hi,

  PDF file is a binary file!!!
  Use this code it's work:

  byte[] content = outPDF.toByteArray();
     response.setContentLength(content.length);
     response.getOutputStream().write(content);
     response.getOutputStream().flush(); 

  D.Semprini


  Carmelo Montanez wrote:

    Hi All:

       I know that my browser can display PDF propertly now, however the following
    piece of code (that generates PDF data) sends the data back to the client 
    (IE6) and still  does not not looks like PDF at all.  Can someone please tell me
    if there is something wrong with this code.  By the way
    (out = response.getOutputStream() and "foFile" comes from an "FO" file).



        public void renderFO(InputSource foFile, HttpServletResponse response)
         throws IOException
        {
          try {
            ByteArrayOutputStream outPDF = new ByteArrayOutputStream();
            response.setContentType("application/pdf");
           
              Driver driver = new Driver(foFile, outPDF);
              driver.setRenderer(Driver.RENDER_PDF);
              driver.setOutputStream(outPDF);
              driver.run();

              byte[] content = outPDF.toByteArray();
              response.setContentLength(content.length);
              out.write(content);
              out.flush();
          }     
          catch (Exception ex) {
              System.out.println(ex);
          }
        
    Thanks,
    Carmelo Montanez

    Carmelo Montanez
    NIST Stop 8970
    Gaithersburg, MD 20899

    carmelo@nist.gov 




Re: Anything wrong with this code>

Posted by Semprini Davide <da...@cineca.it>.
Hi,

PDF file is a binary file!!!
Use this code it's work:

byte[] content = outPDF.toByteArray();
   response.setContentLength(content.length);
   response.getOutputStream().write(content);
   response.getOutputStream().flush();

D.Semprini


Carmelo Montanez wrote:

> Hi All:
>
>  
>
>    I know that my browser can display PDF propertly now, however the 
> following
>
> piece of code (that generates PDF data) sends the data back to the client
>
> (IE6) and still  does not not looks like PDF at all.  Can someone 
> please tell me
>
> if there is something wrong with this code.  By the way
>
> (out = response.getOutputStream() and "foFile" comes from an "FO" file).
>
>  
>
>  
>
>  
>
>     public void renderFO(InputSource foFile, HttpServletResponse response)
>      throws IOException
>     {
>       try {
>         ByteArrayOutputStream outPDF = new ByteArrayOutputStream();
>         response.setContentType("application/pdf");
>       
>           Driver driver = new Driver(foFile, outPDF);
>           driver.setRenderer(Driver.RENDER_PDF);
>           driver.setOutputStream(outPDF);
>           driver.run();
>
>  
>
>           byte[] content = outPDF.toByteArray();
>           response.setContentLength(content.length);
>           out.write(content);
>           out.flush();
>       }    
>       catch (Exception ex) {
>           System.out.println(ex);
>       }
>
>    
>
> Thanks,
>
> Carmelo Montanez
>
>  
>
> Carmelo Montanez
> NIST Stop 8970
> Gaithersburg, MD 20899
>
>  
>
> carmelo@nist.gov <ma...@nist.gov>
>