Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ByteArrayStream to browser  RSS feed

 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Will sending a ByteArrayOutputStream (of a PDF, in this case) to the browser actually render the PDF file?

I've been told that the Adobe plug-in needs a physical file and won't work with a byte stream.

Therefore, I think I'd need to take the 'baos' and create a FileOutputStream, then a physical file first before I tried to show the PDF file in the browser.

Does this make sense?

Appreciate all replies in advance.

Thanks.

M
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To show a PDF (or any binary format understood by the browser), you just have to write the bytes of the PDF to the OutputStream that is ultimately destined for the browser. Depending on whether you're working within, say, the Servlet framework, you may need to write the HTTP headers and preamble to the OutputStream first, or you may need to call some methods to tell the framework that the content is PDF.

It is not necessary, and would often be inefficient, to write the PDF to a server-side file. If you do that, all that will ultimately happen is that some piece of infrastructure will end up streaming the same bytes to the browser.

If the PDF was generated in-memory, but may need to be served up many times, it could be worth caching the PDF on disk, to save the CPU time involved in generating it; that's an optimisation to consider only when proven necessary, though.
[ April 17, 2007: Message edited by: Peter Chase ]
 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter for your reply.

M
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extra bit:

Originally posted by Mike London:
I've been told that the Adobe plug-in needs a physical file and won't work with a byte stream.


Whoever told you that either didn't mean what you thought they meant or doesn't know what they're talking about.

A browser cannot directly access a server-side file and can't know whether the data it has been sent came from a real server-side file or was generated dynamically by a server-side program. It just says to the server "give me the contents of this URL" and the server sends those contents as a stream of bytes, plus some headers and other preamble.

Adobe plug-in, just like any other client-side program, gets fed data which came from the server as a byte stream over the net. It may have got written to some client-side file in the process, but you, as a server-side developer, don't care about that.
 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,

I think the distinction was between the browser plug-in and the standalone PDF (Adobe) viewer. With the standalone PDF viewer, I believe you do need a physical file.

-- M
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but the physical file is on the client, not the server. If I understand correctly, you're programming the server side. You don't care about how the client decides to deal with the PDF once you've sent it. The client probably does write it to a physical file. I don't know, but would hazard a guess that even the plug-in does that.

Even more, as a server-side writer, you probably don't actually tell the client machine specifically to use Adobe Reader (plugin or otherwise) to view the PDF. The client machine decides for itself. The client machine might be using one of the very fine products of Global Graphics, for instance this one. Sorry, blatant advertising.
[ April 18, 2007: Message edited by: Peter Chase ]
 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter for your excellent info and replies!

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't written raw bits out to the browser in a servlet environment, but I've done it in a web server built from scratch. One key thing is to set the mime type header. That's how the browser decides what plugin or application to activate. For example, set the type to "application/pdf" and the browser checks to see if it has an application registered for pdf. I Googled for mime type and found a list of almost 400 that cover types for MS office, audio, video, zip etc.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!