• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PDF file display on the browser

 
smitha rai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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

 
Tim Moores
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is IE set up to display PDFs?
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Nitin Surana
Ranch Hand
Posts: 129
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2895
46
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2895
46
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic