ADF 11g : Show PDF in a Popup

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.

9 Comments

  1. Saswat January 25, 2015
  2. Apoorv Jain January 9, 2015
  3. Laurent CHATILLON December 30, 2013
    • Shivaji January 4, 2014
  4. majda December 9, 2013
  5. Shivaji May 31, 2012
  6. PG Geldenhuys January 31, 2012
  7. Amit Agarwal January 9, 2012
  8. Alexandre August 2, 2011