1
 <action name="PDF" class="OwnerDetail" method="createPDF">
     <result name="success" type="stream">
        <param name="contentType">application/pdf</param>
        <param name="inputName">inputStream</param>
        <param name="contentDisposition">attachment;filename="RegistrationSummaryReport.pdf"</param>
        <param name="bufferSize">1024</param>
    </result>
</action>

The above code generates PDF as an attachment. But I need to open the PDF in a new window. Kindly provide your suggestions

Roman C
  • 49,761
  • 33
  • 66
  • 176
MN Karthikeyan
  • 61
  • 2
  • 10
  • Are you saying that iText is built in into Struts? I don't understand how this small piece of XML would be sufficient to create a PDF. iText code looks very, very different. (I know, because I'm the original author of iText.) – Bruno Lowagie Nov 24 '14 at 07:23
  • @BrunoLowagie : NO . The action method is a different one. I havent shared that code. The above code is only for Struts.xml – MN Karthikeyan Nov 24 '14 at 07:30
  • In any case: you are trying to do something that isn't supported in PDF. You'll have to provide some JavaScript in the HTML to achieve what you want. I suggest that you use other tags than PDF and iText because that's not where you'll find the solution: nothing in ISO-32000-1 defines functionality that meets your needs. – Bruno Lowagie Nov 24 '14 at 07:38
  • @BrunoLowagie is right, this is not iText related. Try tagging your questions with the tags related to the actual questions, not to the technologies used elsewhere in your application – Andrea Ligios Nov 24 '14 at 10:40
  • [Here](http://stackoverflow.com/questions/12265702/pdf-generation-using-itext-in-struts-2-result-type-stream-not-working) is an answer using iText and `HttpServletResponse OutputStream`. – k_rollo Mar 15 '15 at 06:34

1 Answers1

2

You need to change the contentDisposition. This is an HTTP header, so this is needed when using other technologies than Struts2 too (Servlets, for example).

Content-Disposition has two main values that are interesting for your case:

  • attachment : asks the user which is the action needed between downloading the file, or opening it with a Desktop application.

  • inline (default): tries opening the file in a new tab (or window) with a browser plugin. If a plugin is not found for that Content-Type, it asks the user to choose a Desktop application for opening it.

Then you need simply:

<param name="contentDisposition">
    inline;filename="RegistrationSummaryReport.pdf"
</param>

or just

<param name="contentDisposition">
    filename="RegistrationSummaryReport.pdf"
</param>

EDIT

As suggested in a comment by @BrunoLowagie, I may have omitted an important part.

While it's true that you need inline to open the document in the browser, it's also true that a further step (that I've taken for granted, while it may be not) is needed to open that document in another Tab/Window instead that on the current one, : you need to call the action by specifying the target attribute, or by using javascript window.open():

<s:url var="myUrl" action="downloadPdf" namespace="/foobar" />

<!-- In a new Tab/Window without javascript -->  
<s:a href="%{myUrl} target="_blank">
    download
</s:a>

<!-- In a new Tab/Window with javascript -->    
<s:a href="javascript:window.open('%{myUrl}');>
    download
</s:a>

Read more on this related answer.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Looking at the question, I already see a tag defining the Content-Disposition as `attachment` which opens a dialog so that the end user can save the form. What the OP wants is a PDF that is shown in line **in a new browser window.** Your answer tells him how to show the PDF inline **in the same browser window.** I don't think that was the question. – Bruno Lowagie Nov 24 '14 at 11:32
  • @BrunoLowagie Thanks, I've taken it for granted, while it may be not... Updated. – Andrea Ligios Nov 24 '14 at 13:35
  • Great link and good update! There is a *new window* functionality in PDF, but as documented in the PDF specification, it only works in a standalone PDF viewer: it can open a new PDF viewer window, but it can not open another browser window (nor does it work from within a browser window). – Bruno Lowagie Nov 24 '14 at 13:40
  • Thanks @AndreaLigios . I used javascript for opening the PDF in a new window and it worked ! – MN Karthikeyan Nov 25 '14 at 03:13