Displaying PDF BLOB in new window - GWT

2,891 views
Skip to first unread message

Joshua Carey

unread,
Jan 25, 2012, 1:52:52 PM1/25/12
to google-we...@googlegroups.com
Hi everyone,

I currently am getting a BLOB from oracle and displaying it in a new window.  To accomplish this, I am reading the blob into a byte array, then writing the BYTE array out to the file system as pdf.  I then use "Window.open("URL to PDF on filesystem").  The problem is that I am writing all these blobs as PDF to the filesystem before being able to use the Window.open command in my GWT application.  How can I use Window.open("using the byte array") instead of having to write the PDF to the file system?  This would greatly help me and any help is appreciated.  I am calling the Window.open from within my "Presenter" class.  Just for your information.  Thank you very much.

Joshua

kim young ill

unread,
Jan 25, 2012, 2:40:15 PM1/25/12
to google-we...@googlegroups.com
i would just use a Servlet/jsp at backend to read pdf-blob push the the file back ( set the content-type header correctly ) so the browser can decide to show it with pdf-viewer-plugin or save it to users file system

hth


On Wed, Jan 25, 2012 at 7:52 PM, Joshua Carey <jc1...@gmail.com> wrote:
Hi everyone,

I currently am getting a BLOB from oracle and displaying it in a new window.  To accomplish this, I am reading the blob into a byte array, then writing the BYTE array out to the file si ystem as pdf.  I then use "Window.open("URL to PDF on filesystem").  The problem is that I am writing all these blobs as PDF to the filesystem before being able to use the Window.open command in my GWT application.  How can I use Window.open("using the byte array") instead of having to write the PDF to the file system?  This would greatly help me and any help is appreciated.  I am calling the Window.open from within my "Presenter" class.  Just for your information.  Thank you very much.

Joshua

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/R6jKcLzu6M4J.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Joshua Carey

unread,
Jan 25, 2012, 3:37:09 PM1/25/12
to google-we...@googlegroups.com
Hi, since i'm using a gwt built application built by the company I work for, I don't believe I have the option of adding another servlet or using JSP.  What really sucks is that I have the thing working, however, I am writing a pdf for every single record pulled back from the database.  Often the user only wants to see one or two of the pdf's and to accomplish this, I had to write literally hundreds to the filesystem that are not even being viewed.  This is why I would like to just keep the byte array's for each blob and route the byte array for the pdf the user selects to the Window.open command I currently have.  My issue is that the only way I have figured out how to open a pdf with Window.open in GWT is by writing the pdf to the filesystem first and then giving the path to the PDF on the filesystem.  It would be great if there was a way I could open the window using the byte array containing the pdf blob.  Any ideas?

Alan Chaney

unread,
Jan 25, 2012, 4:01:37 PM1/25/12
to google-we...@googlegroups.com
Well, you could do what kim young ill proposed.

Adding a servlet to an existing GWT application to do what you want is a very small task. Simply create a class which extends HttpServlet, override the get method and add the servlet definition to your web.xml file - the same one as you have in the GWT project. All the libraries required to create a servlet are automatically in place in a GWT project because the RPC servlet and RequestFactory servlets are just sub-classes of HttpServlet.

To conform to the GWT rules you'll need to add it to a package com.yourcompy.blah.blah.server  to make sure that  GWT doesn't try and compile it into javascript. The java compiler will automatically compile it for you.

Then return the pdf with the content type set as application/pdf and, as the previous poster said, your browser will recognize it and handle it accordingly.

I think you'll find the solution above much easier - you may have to spend a few minutes searching the web for a few examples but I'd guess that there are literally thousands of examples of adding a simple servlet to serve pdfs and the like.

Regards

Alan

Joshua Carey

unread,
Jan 25, 2012, 6:33:45 PM1/25/12
to google-we...@googlegroups.com
Ok, thanks for the advice.  Honestly, I would prefer adding all the logic I am slated to code for this company in servlet/jsp, since I am very new to GWT.  I have found things extremely difficult versus them being easy with jsp/servlets.  Let me ask you this.  If the companys current site is gwt and basically one of the tabs, has the logic they wan't me to change and add to.  So say a user clicks the gwt tab for the page they want my changes/additions on, fills in the search criteria, and instead of me going to the gwt classes and servlet they have in place, i redirect to my new servlet that outputs a new jsp, will this open up a completely different browser window, or will it add another tab to the gwt project?  I'm confused at how calling a new servlet or jsp from a GWT app will act?

In originating this question, I was hoping there were arguments with Window.Open where I could output the blob without writing it to PDF first.  But from the sounds of it, Window.open only accepts URL to physical files, not objects like byte arrays or blobs??

Thanks again,

Joshua

Alan Chaney

unread,
Jan 25, 2012, 7:18:22 PM1/25/12
to google-we...@googlegroups.com
On 1/25/2012 3:33 PM, Joshua Carey wrote:
Ok, thanks for the advice.  Honestly, I would prefer adding all the logic I am slated to code for this company in servlet/jsp, since I am very new to GWT.  I have found things extremely difficult versus them being easy with jsp/servlets.  Let me ask you this.  If the companys current site is gwt and basically one of the tabs, has the logic they wan't me to change and add to.  So say a user clicks the gwt tab for the page they want my changes/additions on, fills in the search criteria, and instead of me going to the gwt classes and servlet they have in place, i redirect to my new servlet that outputs a new jsp, will this open up a completely different browser window, or will it add another tab to the gwt project?  I'm confused at how calling a new servlet or jsp from a GWT app will act?

Create a servlet which can go to your database and return the PDF when you do a "get" call. Lets assume that the url to that is:

http://mycompany.com/gwtmodule/pdfservlet/

where the GWT module name is gwtmodule and the web.xml has a servlet definition with a <url-pattern>/pdfservlet/*.pdf</url-pattern>


So, to get a PDF file called file1.pdf

add a link somewhere on your GWT page which is

    <a href='" + GWT.getModuleBaseURL() +  "pdfservlet/file1.pdf" + "'>Link to file1</a>

You can do this by

A.  generating the HTML directly and injecting it into an HTML widget
B.  or by using an InlineHyperlink
C.  or a Hyperlink widget. See the javadocs for details.
D.  or an Anchor widget (possibly the easiest)

For example:

 Anchor anchor = new Anchor();
 anchor.setText("Link to file1.pdf");
 anchor.setHref(GWT.getModuleBaseURL() +  "pdfservlet/file1.pdf");
 
 and then add the anchor to the container widget you want to place it on.

When your user clicks on that link it will generate an HTTP GET request to http://mycompany.com/gwtmodule/pdfservlet/file1.pdf.

Your servlet should then render the PDF as a stream of bytes using the response.getOutputStream() method of the servlet. Don't forget to set the length and the content type into the response as well.


HTH

Alan
Reply all
Reply to author
Forward
0 new messages