Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Display byte array back to pdf and display in IE

Thread view: 
SG - 20 Oct 2005 22:27 GMT
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.
sgho@aol.com
Alex - 21 Oct 2005 08:12 GMT
So, what exactly is your problem? (I suspect I know it, but would like to
hear it from the horse's mouth.)

> 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
[quoted text clipped - 37 lines]
> Any help with code will be appreciated.
> sgho@aol.com
SG - 21 Oct 2005 16:08 GMT
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 - 21 Oct 2005 17:43 GMT
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

> This is the problem:
> PDF never gets displayed in browser (neither in IE, not Netscape etc.).
[quoted text clipped - 35 lines]
> Someone suggested to add dummy PDF filename for displaying. I added
> stuff.pdf as dummy PDF name with no luck.
Roedy Green - 22 Oct 2005 01:02 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Ingo R. Homann - 24 Oct 2005 10:09 GMT
Hi SG,

> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.