Display byte array back to pdf and display in IE

S

SG

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.
(e-mail address removed)
 
A

Alex

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

SG

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.
 
A

Alex

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
 
R

Roedy Green

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.
 
I

Ingo R. Homann

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
 
Joined
Jan 15, 2008
Messages
1
Reaction score
0
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top