This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

PDF file display on the browser

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using struts DispatchAction to display the existing file (on the server) on the web browser. My code works on FireFox and Google Chrome. But displays empty screen when executed on IE. Please refer to the Action class and kindly help me out what am I missing in the code inorder to work on all browsers.

Page Access URL: http://localhost:8080/pdfWeb/showpdf.do?action=displayPdf

Action Class

 
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is IE set up to display PDFs?
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the actual content type that's being set? Make sure it's application/pdf which is the official PDF MIME type.
 
Ranch Hand
Posts: 129
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the code of your struts.xml ?

May be this example will help you.
http://www.onlinexamples.com/showfullexample.action?idexamples=20&title=Jasper%20Report%20Example

 
smitha rai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all who responded. Here is the complete code.

struts-config.xml
------------------
<action path="/pdf" type="demo.struts.action.BaseAction" name="saform" parameter="action">
<forward name="home" path="/pdf/home.jsp"/>
</action>

JSP
---
<%
String pdfurl3 = request.getContextPath() + "/pdf.do?action=displayPdf";
%>
Click
<a href="#" onClick="showPdf('<%=pdfurl3%>'); return false;">here to see the pdf file

javascript
----------
var theChild;
function showPdf(url) {
if(theChild!=null) {
if(getCurrentBrowserName() != 'msie') {
theChild.close();
}
}
theChild = popupPdf(url);
theChild.focus();
}

function popupPdf(url) {
alert('Attention: The document will be opened in another window. Please close window after printing or viewing!');
var attributes = 'menubar=no,toolbar=no,scrollbars=yes,resizable=yes,fullscreen=no';
attributes = attributes + ',width=' + (screen.availWidth - 150);
attributes = attributes + ',height=' + (screen.availHeight - 250);
attributes = attributes + ',screenX=50,screenY=50,left=10,top=10';
var wOpen;
wOpen = window.open(url, 'theChild', attributes);

//wOpen.focus();
wOpen.moveTo(75,50);
wOpen.resizeTo(screen.availWidth, screen.availHeight);

return wOpen;

}

function getCurrentBrowserName() {
var browserName = "";

var ua = navigator.userAgent.toLowerCase();
if ( ua.indexOf( "opera" ) != -1 ) {
browserName = "opera";
}
else if ( ua.indexOf( "msie" ) != -1 ) {
browserName = "msie";
}
else if ( ua.indexOf( "safari" ) != -1 ) {
browserName = "safari";
}
else if ( ua.indexOf( "mozilla" ) != -1 ) {
if ( ua.indexOf( "firefox" ) != -1 ) {
browserName = "firefox";
}
else {
browserName = "mozilla";
}
}
else {
browserName = "";
}

return browserName;
}

Action Class
------------
public class BaseAction extends DispatchAction {

public ActionForward displayPdf(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

String filepath = "/test/pdf/sample.pdf";

//For Local:
String baseFolder = "/Users/raghu/Documents/Shared/personal/Final_Out";

String contentType = getServlet().getServletContext().getMimeType(filepath);
response.setContentType(contentType);
//New
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");

OutputStream out = response.getOutputStream();
byte[] pdfContent = this.loadFile(baseFolder + filepath);
out.write(pdfContent, 0, pdfContent.length);
if (out != null) {
out.close();
}

return null;
}

public static byte[] loadFile(String sourcePath) {
byte[] output = null;
InputStream inputStream = null;
try {
inputStream = new FileInputStream(sourcePath);
output = readFully(inputStream);
if (inputStream != null) {
inputStream.close();
}
}
catch(IOException ioex) {
//Handle Exception
}

return output;
}
}
 
Tim Moores
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what's the answer to my question?
 
smitha rai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,

Sorry I did not understand your question. Are there any specific settings to the IE?

 
Tim Moores
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know. But displaying PDFs is browser-specific (some browsers can do it on their own, some with the help of plugins, and some not at all). So I'd start by investigating whether your browser can display PDFs at all, before l'd fiddle with the code.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic