Jeremias and folks,

I rewrote my servlet and got a little progress even if
it still cannot display the tiff data.

When I ran my servlet, from web browser, I can see the
text(i.e. COPY ONLY etc) in PDF format. Those text are
from my XSL file.  But it does not show the image. I
ran my new servlet--GetByteStream seperately, it can
display tiff image.  It seems like the byte stream
which takes the binary data did not pass to XSL. 

I enclosed the codes here. Can you please tell me
anything wrong with my codes?

Many thanks,

Jerry

//servlet to display tiff data in PDF format 
public class DisplayImage extends HttpServlet {
    ...
   private void
sendPDFBinaryResponse(HttpServletResponse
res,HttpServletRequest req) throws IOException,
ServletException{
                                

                if (log == null) {
                log = new
ConsoleLogger(ConsoleLogger.LEVEL_WARN);
            MessageHandler.setScreenLogger(log);
         }

        

                try {

            ByteArrayOutputStream out = new
ByteArrayOutputStream();

           
            Driver driver = new Driver();
            driver.setLogger(log);
            driver.setRenderer(Driver.RENDER_PDF);
            
            
            File xsltfile = new
File("C:/workspace/MaxTradDownload/Web
Content/html/Tiff2Pdf.xsl");

            
            try {


                        res.setContentType("application/pdf");
            driver.setOutputStream(out);

            //Setup XSLT
            TransformerFactory factory =
TransformerFactory.newInstance();
            Transformer transformer =
factory.newTransformer(new StreamSource(xsltfile));
        
                
                        String GetByteStreamServletURL="http://"; +
req.getServerName() + ":" + req.getServerPort() +
"/MaxTradDownload" + "/GetByteStreamServlet?" +
req.getQueryString();
                                
                
                //*assign ByteArrayInputStream bais to image
variable in xsltfile, seems like it is not working.
                transformer.setParameter("image",
GetByteStreamServletURL);


       
        
            //Resulting SAX events (the generated FO)
must be piped through to FOP
            Result result = new
SAXResult(driver.getContentHandler());

            //Start XSLT transformation and FOP
processing
            transformer.transform(new
StreamSource("c:/temp/DummyXML.xml"), result);
            
                  
            byte[] content = out.toByteArray();


            res.setContentLength(content.length);
            res.getOutputStream().write(content);
            res.getOutputStream().flush();

            
        } finally {
            out.close();
        }


        } catch (Exception ex) {
            throw new ServletException(ex);
        }

        }
  
       ...
  
}

//new servlet to return byte stream to DisplayImage
servlet

public class DisplayImage extends HttpServlet {
   ...
 private void sendBinaryResponse(HttpServletResponse
res,
                                  HttpServletRequest
req, byte[] bt,
                                  String
sContTypeHeader,
                                  String
sEncodingHeader, String encoding) {
                                        
                
  
    res.addHeader("Expires", "Wed, 26 Feb 1970
08:21:57 GMT");
    res.addHeader("Cache-Control", "no-cache");

    res.setContentType(sContTypeHeader);
    try {
      OutputStream out = null;
        res.setContentType(sContTypeHeader);

      
              if (sEncodingHeader != null) {
          res.setHeader("Content-Encoding",
sEncodingHeader);
        }
        res.setContentLength(bt.length);
        out = res.getOutputStream();
          
        out.write(bt);
     
      out.flush();
      out.close();
    }
    catch (Throwable e) {}
  }

  ...

}

//here is my XSL file
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:fo="http://www.w3.org/1999/XSL/Format";
exclude-result-prefixes="fo">

<xsl:template match="/">


        <fo:root font-family="Times Roman" font-size="12pt"
text-align="center"
xmlns:fo="http://www.w3.org/1999/XSL/Format";>

                <fo:layout-master-set>
        <fo:simple-page-master
  margin-right="1.5cm"
  margin-left="1.5cm"
  margin-bottom="2cm"
  margin-top="1cm"
  page-width="21cm"
  page-height="29.7cm"
  master-name="left">
      <fo:region-before extent="1cm"/>
      <fo:region-body margin-top="1cm"/>
      <fo:region-after extent="1.5cm"/>
    </fo:simple-page-master>

</fo:layout-master-set>

<fo:page-sequence id="N2528" master-reference="left">

<fo:static-content flow-name="xsl-region-after">
  <fo:block text-align-last="center" font-size="10pt">
    <fo:page-number/>
  </fo:block>
</fo:static-content>

<fo:flow flow-name="xsl-region-body">
<fo:block font-size="18pt" font-weight="bold">Copy
Only</fo:block>
<fo:block>

  <fo:block>
        <fo:external-graphic width="50pt" height="50pt"
overflow="hidden" src="url('[EMAIL PROTECTED]')"/>
  </fo:block>
  
  
  <fo:block font-size="16pt" font-weight="bold"
space-before.minimum="1em"
space-before.optimum="1.5em"
space-before.maximum="2em"/>
  This document is just Copy only, Non Negotiable
</fo:block>
</fo:flow>
</fo:page-sequence>

</fo:root>
</xsl:template>

</xsl:stylesheet>


--- Jerry <[EMAIL PROTECTED]> wrote:
> Jeremias,
> 
> Thank you so much for your detailed information. It
> really helps me a lot.
> 
> Based on your suggestion, I am going to implement
> it.
> I will let you know what I get.
> 
> Best regards,
> 
> 
> 
> Jerry 
> --- Jeremias Maerki <[EMAIL PROTECTED]>
> wrote:
> > Hello Jerry,
> > 
> > On 07.04.2004 19:07:05 Jerry wrote:
> > > Jeremias,
> > > 
> > > Thank you for your suggestions.
> > > 
> > > The soultion you gave me here is very intrigue
> to
> > me.
> > > However, I cannot fully understand your point.
> > Would
> > > you please clarify for me if the following
> > approach is
> > > not what you mean?
> > > 
> > > 1. I need to write a new servlet which will
> return
> > a
> > > ByteArrayInputStream which take the binary tiff
> > data
> > > from database. 
> > 
> > Just nitpicking: Your new servlet writes the TIFF
> > file byte by byte to
> > the Request's OutputStream.
> > 
> > > 2. Modify my current servlet and set the URL to
> my
> > new
> > > servlet as a parameter to the XSL file using the
> > > following:
> > > 
> > > //assume the new servlet is GetByteStreamServlet
> > > String
> > NewServletURL="/GetByteStreamServlet?id=123"  
> > 
> > Right! Something like that, if the base URL is
> set.
> > YOu might have to
> > use http://localhost/....
> > 
> > > TransformerFactory factory =
> > > TransformerFactory.newInstance();
> > >             Transformer transformer =
> > > factory.newTransformer(new
> > StreamSource(xsltfile));
> > >         
> > >           
> > >           //pass NewServletURL to image variable
> in
> > > xsltfile, is this corect?
> > >           transformer.setParameter("image",
> > > NewServlet);
> > 
> > Correct.
> > 
> > > 
> > > If above correct, I also modify XSL file like
> > this:
> > >   ...
> > >  <fo:external-graphic width="50pt" height="50pt"
> > > overflow="hidden" src="URL('[EMAIL PROTECTED]')"/>
> > >  ...
> > 
> > Correct.
> > 
> > > If I use this way, how can set Source parameter
> of
> > > transformer.transform((Source)?, res)? I am not
> > sure
> > > of this part.
> > 
> > The source document is probably a dummy XML
> > document, just like this:
> > 
> > <dummy/>
> > 
> > That's enough.
> > 
> > > Please correct me if I misunderstood your point.
> > 
> > No, you did alright. The XSLT you posted on the
> list
> > earlier will
> > probably not work however. It doesn't look right.
> At
> > least an embracing
> > xsl:template is missing:
> > 
> > <xsl:template match="/">
> >    <!-- your xsl-fo code here -->
> > </xsl:template>
> > 
> > 
> > Create the TIFF-serving Servlet first and then get
> > your XSLT stylesheet
> > together from the command-line. You can test the
> > TIFF-Servlet from your
> > browser. If both are working go back to your
> > original servlet and finish.
> > 
> > For the next time, please don't write directly to
> > those who answer
> > questions. Others won't be able to profit from the
> > solutions I'm giving
> > you, as I can't tell whether you'd agree if I
> simply
> > sent my answer to
> > the list again.
> > -- 
> > Jeremias Maerki <[EMAIL PROTECTED]>
> > 
> 
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! Small Business $15K Web Design Giveaway 
> http://promotions.yahoo.com/design_giveaway/
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> For additional commands, e-mail:
> [EMAIL PROTECTED]
> 


__________________________________
Do you Yahoo!?
Yahoo! Small Business $15K Web Design Giveaway 
http://promotions.yahoo.com/design_giveaway/

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to