-
1. Re: Open PDF in a new window
juangon Mar 8, 2010 4:38 AM (in response to meex)This could be done using a4j:htmlCommandLink:
<a4j:htmlCommandLink actionListener="#{controller.sendFile}" value="Send File" target="_blank">
And then, in the ActionListener simply you have to send the byte array the same as it was a Servet:
byte[] dataToSend; dataToSend=getDataToSend(); //TO BE DONE HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); out = response.getOutputStream(); response.setContentType("application/pdf"); response.setContentLength(dataToSend.length); response.setHeader("Content-Disposition", "inline; filename=\"File.pdf\""); out.write(dataToSend); out.flush(); FacesContext.getCurrentInstance().responseComplete();
-
2. Re: Open PDF in a new window
meex Mar 8, 2010 4:59 AM (in response to juangon)thanks for your response!
Your approach seems to be the same as described at: http://ahoehma.wordpress.com/2008/03/31/jsf-file-download/
I thought there's a more simple mehtod without streaming the bytes :-(
...but it seems to be the only solution for my problem :-)
Markus
-
3. Re: Open PDF in a new window
juangon Mar 8, 2010 5:01 AM (in response to meex)I guess there isn't any other way to send a file, but sending bytes. That's J2EE (Servlet) and HTTP way to do it.
-
4. Re: Open PDF in a new window
jflint May 21, 2010 5:06 PM (in response to juangon)Hi guys,
I've been using similar solution to show PDF in the new window (<h:commandButton ... onclick="document.forms['mainForm'].target='_blank'">)
using ADF Faces and now Seam/RichFaces.
It works fine in IE and FF, but now I need to support Safari/Chrome and this work strange in those browsers (all on Windows).
The problem is, I can only click on this button only once, after it all buttons and links on the page is not responding on click anymore, only refreshing page manually (F5 or Ctrl-R) helps.
I've tried other components, but they did not work:
<h:outputLink> opens new window, but shows the parent page, not PDF.
<a4j:commandButton> no new window, no PDF.
I suspect is the problem is with target="_blank", or is this a browsr's configuration problem?
Does anybody have an idea what is wrong?
-
5. Re: Open PDF in a new window
ahoehma Sep 1, 2010 2:44 AM (in response to jflint)Hi,
Please check your html after the click ... the <a href.../> is then outside the form ... I guess
I had the same effect some time ago ... and Ilya told me ... don't use blocklevel-elements (divs etc.) inside a ***comandLink
Regards
Andreas
-
6. Re: Open PDF in a new window
ilya_shaikovsky Sep 1, 2010 5:07 AM (in response to jflint)In order to return PDF - you should use h:commandLink. It's not availale via ajax. Just put the PDF in output stream in your action on command link.
-
7. Re: Open PDF in a new window
jflint Sep 23, 2010 11:51 AM (in response to ilya_shaikovsky)Thanks guys for your responses. I was able to figure out what was the problem.
It was indeed, "target='_blank'" what caused this behavior and not a component problem.
To open new window with PDF, JavaScript function can be used instead:
function showPDF(formId) {
var day=new Date();
var id=day.getTime();
var pdfWin = window.open('',id,"width=800,height=700,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes");
document.getElementById(formId).target=id;
if (window.focus) {
pdfWin.focus()
}
}and it's used in "onclick" event:
<a4j:form id="mainForm">
...
<h:commandButton title="View"
onclick="showPDF('mainForm');"
action="#{pageaction.viewPDF(form_nbr)}"/>...
This works fine in Safari, IE, FF, Chrome.
-
8. Re: Open PDF in a new window
jflint Nov 29, 2010 6:28 PM (in response to jflint)Does anybody have a problem with the opening PDF in new window with Adobe Reader X , or it's just me being lucky?
Now it's IE who does not work - new window is opened, but without any PDF in it.
Good news is, Firefox and Chrome have no such problem.
May be I need to add some renderer options or headers?
Content in the HttpServletResponse is set as "application/pdf".
-
9. Re: Open PDF in a new window
liuliu Nov 30, 2010 6:15 AM (in response to meex)why not generate the pdf on server, then open it in a iframe?
-
10. Re: Open PDF in a new window
jflint Nov 30, 2010 10:23 AM (in response to liuliu)Not sure how would I use iframe. There is no PDF HTML files for this new window.
PDF stream is generead on the server via XSLT using Apache FOP and sends to the HttpServletResponse which will be redirected to the new window created in this JS:
function showPDF(formId) {
var day=new Date().getTime();
var pdfWin = window.open("",id,"width=800,height=700,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes");
document.getElementById(formId).target=id;
if (window.focus) {
pdfWin.focus()
}
} -
11. Re: Open PDF in a new window
jflint Nov 30, 2010 4:34 PM (in response to jflint)Looks like the problem in the Adobe Reader plug-in for IE.
Many peoples complaining about similar problem, so I'll ask this question on the Adobe forum.
-
12. Re: Open PDF in a new window
juangon Dec 1, 2010 2:09 AM (in response to jflint)have you tried setting "Content-length" http header?
-
13. Re: Open PDF in a new window
jflint Dec 1, 2010 10:49 AM (in response to juangon)I did not try it for two reasons:
1. if code works on IE, Firefix, Safari, Chrome with Reader 9 and on Firefix, Safari, Chrome but not on IE with Reader 10, it's clearly problem with IE and Reader 10. Actually, this solution has been working fine for the last four years on Firefox and IE with Reader versions 6,7,8,9.
So, why fix what is not broken?
2. I'm not sure how to get the length of the resulting PDF stream. It's a result of XSLT by Apache FOP and I do not know what the size of the result stream would be.
Here is how it's done:
HttpServletResponse resp = (HttpServletResponse) facesContext.getExternalContext().getResponse();
resp.setContentType("application/pdf");
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, resp.getOutputStream());
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(userSession.getPaystubXSL()));
transformer.setParameter("versionParam", "2.0");
Source src = new StreamSource(xmlInputStream);
Result res = new SAXResult(fop.getDefaultHandler());// Here is the actual transformation:
transformer.transform(src, res);// Done.
resp.flushBuffer();
resp.getOutputStream().flush();
resp.getOutputStream().close(); -
14. Re: Open PDF in a new window
juangon Dec 1, 2010 3:59 PM (in response to jflint)1. Yep it's a bit strange. Only thing I can tell you is I am doing that way and always working, no matter whatever browser or Reader I am using.
2. In this line:
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, resp.getOutputStream());
You'll have to provide a ByteArrayOutputStream: e.g. (some methods and/or classes perhaps aren't exactly the same ;-D ):
ByteArrayOutputStream bos=new ByteArrayOutputStream();
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, bos);
.
. //GENERATE PDF HERE
int length=bos.getBytes().length; //SET CONTENT-LENGTH WITH THIS LENGTH
resp.getOutputStream().write(bos.getBytes());
.
.