aspose file tools*
The moose likes Struts and the fly likes PDF file display on the browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "PDF file display on the browser" Watch "PDF file display on the browser" New topic
Author

PDF file display on the browser

smitha rai
Ranch Hand

Joined: Aug 15, 2001
Posts: 33
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
Is IE set up to display PDFs?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

What's the actual content type that's being set? Make sure it's application/pdf which is the official PDF MIME type.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nitin Surana
Ranch Hand

Joined: Jan 21, 2011
Posts: 129

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

Joined: Aug 15, 2001
Posts: 33
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
So what's the answer to my question?
smitha rai
Ranch Hand

Joined: Aug 15, 2001
Posts: 33
Hi Tim,

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

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PDF file display on the browser