Display byte array back to pdf and display in IE

Discussion in 'Java' started by SG, Oct 20, 2005.

  1. SG

    SG Guest

    I get Base64 encoded String for a PDF file. I convert that String to
    character array. Then I decode it back to byte array. Now, I need to
    use the byte array to display pdf.


    I am using a Servlet to display byte array of PDF into IE browser.
    Correct number of bytes are returned from existing PDF. Any help with
    code will be appreciated. They key is, my input argument is byte array,
    not the PDF itself.

    I am using code below:

    I am using code below.

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {

    resp.setContentType("application/pdf");
    resp.setHeader("Expires", "0");
    resp.setHeader("Cache-Control","must-revalidate, post-check=0,
    pre-check=0");
    resp.setHeader("Pragma", "public");

    resp.setHeader("Pragma", "no-cache"); //HTTP 1.0
    resp.setDateHeader("Expires", 0); //prevents caching at the proxy
    server
    resp.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
    resp.setHeader("Cache-Control", "max-age=0");

    resp.setHeader("Content-disposition", "inline; filename=stuff.pdf");

    byte[] inputBytes = getBytesFromYourPDF(...);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    if(inputBytes !=null){
    outputStream.write(inputBytes);
    }
    outputStream.flush();
    }

    Any help with code will be appreciated.
    SG, Oct 20, 2005
    #1
    1. Advertising

  2. SG

    Alex Guest

    So, what exactly is your problem? (I suspect I know it, but would like to
    hear it from the horse's mouth.)

    "SG" <> wrote in message
    news:...
    > I get Base64 encoded String for a PDF file. I convert that String to
    > character array. Then I decode it back to byte array. Now, I need to
    > use the byte array to display pdf.
    >
    >
    > I am using a Servlet to display byte array of PDF into IE browser.
    > Correct number of bytes are returned from existing PDF. Any help with
    > code will be appreciated. They key is, my input argument is byte array,
    > not the PDF itself.
    >
    > I am using code below:
    >
    > I am using code below.
    >
    > public void doGet(HttpServletRequest req, HttpServletResponse resp)
    > throws ServletException, IOException {
    >
    > resp.setContentType("application/pdf");
    > resp.setHeader("Expires", "0");
    > resp.setHeader("Cache-Control","must-revalidate, post-check=0,
    > pre-check=0");
    > resp.setHeader("Pragma", "public");
    >
    > resp.setHeader("Pragma", "no-cache"); //HTTP 1.0
    > resp.setDateHeader("Expires", 0); //prevents caching at the proxy
    > server
    > resp.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
    > resp.setHeader("Cache-Control", "max-age=0");
    >
    > resp.setHeader("Content-disposition", "inline; filename=stuff.pdf");
    >
    > byte[] inputBytes = getBytesFromYourPDF(...);
    > ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    >
    > if(inputBytes !=null){
    > outputStream.write(inputBytes);
    > }
    > outputStream.flush();
    > }
    >
    > Any help with code will be appreciated.
    >
    >
    Alex, Oct 21, 2005
    #2
    1. Advertising

  3. SG

    SG Guest

    This is the problem:
    PDF never gets displayed in browser (neither in IE, not Netscape etc.).

    My input argument is byte array. I want to display PDF in IE browser
    from that byte array from within doGet method of servlet.

    I get correct number of buyes back from PDF.

    Code is:

    // Setting all headers (as shown below)

    byte[] inBytes = getBytesFromPDF(...);
    //I succesfully get bytes from PDF.

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();

    if(inBytes !=null){
    outStream.write(inBytes);
    }
    outStream.flush();

    I have set following headers before executing the above code:

    resp.setContentType("application/pdf");
    resp.setHeader("Expires", "0");
    resp.setHeader("Cache-Control","must-revalidate, post-check=0,
    pre-check=0");
    resp.setHeader("Pragma", "public");

    resp.setHeader("Pragma", "no-cache"); //HTTP 1.0
    resp.setDateHeader("Expires", 0); //prevents caching at the proxy
    server
    resp.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
    resp.setHeader("Cache-Control", "max-age=0");
    resp.setHeader("Content-disposition", "inline; filename=stuff.pdf");

    Someone suggested to add dummy PDF filename for displaying. I added
    stuff.pdf as dummy PDF name with no luck.
    SG, Oct 21, 2005
    #3
  4. SG

    Alex Guest

    First of all, ditch all "setHeader" calls. Then after setContentType call,
    add this:

    resp.setContentLength(inBytes.length);

    You may still have a problem with IE, if you back out of the page displaying
    your PDF doc, and then move into it again, using BACK and FORWARD buttons.
    You will be better off saving your PDF doc in a temporary file, and
    redirecting the response, like this:

    String fileName = ...
    <make the file>
    resp.sendRedirect(fileName);

    AM

    "SG" <> wrote in message
    news:...
    > This is the problem:
    > PDF never gets displayed in browser (neither in IE, not Netscape etc.).
    >
    > My input argument is byte array. I want to display PDF in IE browser
    > from that byte array from within doGet method of servlet.
    >
    > I get correct number of buyes back from PDF.
    >
    > Code is:
    >
    > // Setting all headers (as shown below)
    >
    > byte[] inBytes = getBytesFromPDF(...);
    > //I succesfully get bytes from PDF.
    >
    > ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    >
    > if(inBytes !=null){
    > outStream.write(inBytes);
    > }
    > outStream.flush();
    >
    > I have set following headers before executing the above code:
    >
    > resp.setContentType("application/pdf");
    > resp.setHeader("Expires", "0");
    > resp.setHeader("Cache-Control","must-revalidate, post-check=0,
    > pre-check=0");
    > resp.setHeader("Pragma", "public");
    >
    > resp.setHeader("Pragma", "no-cache"); //HTTP 1.0
    > resp.setDateHeader("Expires", 0); //prevents caching at the proxy
    > server
    > resp.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
    > resp.setHeader("Cache-Control", "max-age=0");
    > resp.setHeader("Content-disposition", "inline; filename=stuff.pdf");
    >
    > Someone suggested to add dummy PDF filename for displaying. I added
    > stuff.pdf as dummy PDF name with no luck.
    >
    Alex, Oct 21, 2005
    #4
  5. SG

    Roedy Green Guest

    On 21 Oct 2005 08:08:31 -0700, "SG" <> wrote or quoted :

    >This is the problem:
    >PDF never gets displayed in browser (neither in IE, not Netscape etc.).
    >
    >My input argument is byte array. I want to display PDF in IE browser
    >from that byte array from within doGet method of servlet.


    In a thin client situation, the browser sends a request, then the
    sever sends back the pdf file with a header.

    The request contains a list of the sort of responses it is prepared to
    hear.

    In your case, where does the request come from? the browser sending
    in an URL or an Applet? If an Applet, the browser knows nothing about
    the request. The Applet has to deal with the response.

    If in your case there is no Applet involved, I would tackle it this
    way.

    Get a packet sniffer. http://mindprod.com/jgloss/sniffer.html

    Watch your browser download a PDF file not using your code, and watch
    it using your code. Compare the packets and that may give you a clue.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 22, 2005
    #5
  6. Hi SG,

    SG wrote:
    > byte[] inBytes = getBytesFromPDF(...);
    > ...


    (1) Are you sure, your getBytesFromPDF() returns a valid PDF-File?

    (2) Especially when using the IE (in some versions), the Request is
    often executed twice: First, the IE requests the content. It realizes
    from the header, that the content is PDF and should be displayed with
    the AcroReader. It opens AcroReader and passes the URL (not its
    content!) to it. So, AcroReader requests the URL from the server again!
    Without knowing how your getBytesFromPDF works, maybe, this is a problem?

    Ciao,
    Ingo
    Ingo R. Homann, Oct 24, 2005
    #6
  7. SG

    ricuzzo

    Joined:
    Jan 15, 2008
    Messages:
    1
    Ok, in fact this problem is very easy to solve.
    Just remove the line:
    resp.setHeader("Content-disposition", "inline; filename=stuff.pdf");
    from your code,it provokes the "file not found error", i guess adobe is trying to find the stuff.pdf file which doest exists. I write below the code i wrote thats works perfectly for streaming a pdf created with Apache fop.
    <code>

    public void convertXML2PDF(String xmlSource, File xsltFile, File pdfFile, HttpServletResponse response) {
    try {

    System.out.println("Transforming...");

    // configure fopFactory as desired
    FopFactory fopFactory = FopFactory.newInstance();

    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
    // configure foUserAgent as desired

    //La salida no debe es un fichero sino un Sttream para mostrar el pdf.
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    OutputStream respOut = response.getOutputStream(); //antes: new java.io.FileOutputStream(pdfFile);

    try {
    // Construye el fop a partir del bos:
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent,bos);

    // Setup XSLT:
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(xsltFile));

    // Set the value of a <param> in the stylesheet
    transformer.setParameter("versionParam", "2.0");

    // El xml de datos: El xml de entrada de datos serĂ¡ un String.
    Source src = new StreamSource(new StringReader(xmlSource));


    // Resulting SAX events (the generated FO) must be piped through to FOP
    Result res = new SAXResult(fop.getDefaultHandler());

    // NOTA: Al transformar a PDF, se me escribe en el ByteArrayOutputStream automaticamente
    // el contenido de la transformacion, asi que ya no es nulo, sino que es el array de Bytes del PDF.
    transformer.transform(src, res);

    //Metemos en el response el stream obtenido:
    response.setContentType("application/pdf");
    response.setContentLength(bos.toByteArray().length);
    //La siguiente linea hace que no se vea el informe.
    //response.setHeader("Content-disposition","attachment; filename=" +"Informe.pdf" );
    respOut.write(bos.toByteArray());
    respOut.flush();

    } finally {
    bos.close();
    }

    System.out.println("Success!");
    } catch (Exception e) {
    e.printStackTrace(System.err);
    System.exit(-1);
    }
    }

    </code>
    ricuzzo, Jan 15, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,182
    Roedy Green
    Aug 5, 2003
  2. Kirby
    Replies:
    3
    Views:
    612
    Kirby
    Oct 8, 2004
  3. Replies:
    20
    Views:
    9,679
    licebmi
    Sep 7, 2009
  4. Tom McGlynn
    Replies:
    4
    Views:
    836
    Mark Space
    Apr 19, 2008
  5. Ricardo Pog
    Replies:
    1
    Views:
    375
    Austin Ziegler
    Mar 26, 2008
Loading...

Share This Page