ADF 11g : Show PDF in a Popup

Luc Bors 9
0 0
Read Time:3 Minute, 20 Second

In one of my previous posts I showed how to use ADF popup components to display external content such as webpages like wikipedia in an inline frame. Based on this post a colleague of mine tried to display a PDF document. That didn’t work. In this post I explain how you can use a servlet to open a PDF document in the inline frame. I will not explain how to invoke popups. If you need to know how to do that, refer to the post mentioned earlier

How to create the servlet ?

The solution for showing a PDF in a popup is to use a servlet. It’s possible to have a servlet deliver PDF content to the browser by specifying the content type of the servlet response to be the ‘application/pdf’ MIME type via ‘response.setContentType(“application/pdf”)’.
In JDeveloper you can create a HTTP servlet very easy via the new gallery. I decided to call the servlet ShowPdfServlet which actually is a pretty descriptive name for this servlet.

pdfServlet

For the servlet mapping I accept the default, meaning that all request containing “/showpdfservlet” in the URL will invoke the servlet. The “create servlet” wizard of JDeveloper will make sure that the servlet and its mapping is added to the web.xml of your application.

servelt mapping

The servlet will get the filename form a request parameter. So in the servlet I can get this value by invoking getParameter on the request.

 String requestedFile = request.getParameter("name");

With this parameter value, and the file path (which I hardcoded for this post) I am now able to create a new File Object.

// I want to invoke a pdf that is located on the machine where the application is running
 this.filePath = "C:\\JDeveloper\\mywork\\11gR2\\showPdfInPopup\\ViewController\\public_html\\WEB-INF\\pdf-docs";
 // Decode the file name (might contain spaces and on) and prepare file object.
 File file = new File(filePath, URLDecoder.decode(requestedFile, "UTF-8"));

Next step is to initiate the servlet response:

response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "filename=\"" + file.getName() + "\"");

And now finally get the file and write it to the response.

try {
    // Open streams.
    input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
    output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
    // Write file contents to response.
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    int length;
    while ((length = input.read(buffer)) > 0) {
             output.write(buffer, 0, length);
     }

How to make sure the servlet is invoked ?

Remember the servlet mapping that I used earlier ? Now I will use this to make sure that I invoke the servlet every time I open the popup containing the iframe. I simply make sure thate the source of the iframe to contains the path that I use in the servletmapping. I also add a ‘name’ request parameter and set its value to (in this case) a value that I get from a managed bean (line 7).

<af:popup id="popupFileSpecs" animate="default"
                 contentDelivery="lazyUncached">
<af:panelWindow id="pw1" modal="true"
                             title="Extra information for your flight"
                             contentHeight="625" contentWidth="700" resize="on">
               <af:inlineFrame id="if1" shortDesc="This is an inline frame"
                                         source="/showpdfservlet?name=#{pageFlowScope.GeneralBean.pdfUrl}"
                                         styleClass="AFStretchWidth" inlineStyle="height:600px;>
                </af:inlineFrame>
  </af:panelWindow>
</af:popup>

Now run the application, and invoke the popup. There you go !
showPDF
Simple as that.

Resources

The BalusC Code
What is a servlet ?
You can download a copy of the workspace here.
This post was originally posted here.

About Post Author

Luc Bors

Luc Bors is Expertise Lead ADF and technical specialist/architect at AMIS, Nieuwegein (The Netherlands). He developed several Workshops and training on ADF and also is an ADF and JHeadstart instructor. Luc is a member of the ADF Methodology group and publishes articles on ADF in oracle technology related magazines, on the AMIS technology blog, (http://technology.amis.nl/blog).
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

9 thoughts on “ADF 11g : Show PDF in a Popup

  1. Hello LUC,

    How can we show other document formats(Except PDF and image) without downloading.

    Thanks,
    Saswat

  2. Hi, i am having the same issue Servlet is getting called twice. I am using jdeveloper 12c and i tried on IE9, chrome Version 39.0.2171.95 m and firefox 34.0.5.

    please hlep me on this issue thanks in advance.

  3. Hi Shivaji, I meet exactly the same problem you described here a year ago (Iframe in popup calls servlet multiple times). Did you find a solution for this problem ?

    Thanks in advance,
    Laurent

    1. Hi Laurent,

      i guess, this issue is solved with latest version of Chrome/IE9.

      i was using older version. but nothing change we did.
      Pls check from your end, if the latest version rectifies this issue.

      Thanks,
      Shivaji

  4. Hi,
    this example work fine but i need to protect my file from saving (pdf toolbar). are there any solutions. Thx

  5. Hi,
    I have followed the steps given above, and iam able to open the pdf file in a browser when i access directly using address bar.

    But when i use the same in popup as shown in this post, MyServelt’s doGet() method getting called multiple times(4 times). please suggest me what would be the reason.

    Please suggest.

    thanks in advance.

  6. Hi,
    I implemented your inline frame pdf popup (adf 11.1.1.5), and it works fine in all good browsers, but in ie8, when I click on close in upper right hand corner, the application freezes, I then need to log out and back in . . . Any suggestions?

  7. It was very good. It helped me a lot.
    But i have an issue.
    What will be component type in case of other different MIME types. Like: PowerPoint, Docx, Doc, xml.

Comments are closed.

Next Post

Running XQuery from Java applications using the XQJ API and the Oracle XDK implementation

While preparing for the new SOA for Java Professionals training program at AMIS, I was recently working on a section discussing XQuery and more specifically the ability to use XQuery from Java as an alternative to JAXB binding to POJOs on the one hand and DOM based XPath searches on […]
%d bloggers like this: