You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by ausias vives <au...@yahoo.co.uk> on 2004/04/26 18:07:08 UTC

How to return a PDF file

Hello,

One of my methods produce a PDF file that I must
return as the page to be seen directly in the browser,
I'm not sure about the best way to get it right.

Also I'd like that page to be seen in a new window, Is
there a component that allows that? PopupLink doesn't
allow parameteres to be passed, and I need them.

Thanks


	
	
		
____________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping" 
your friends today! Download Messenger Now 
http://uk.messenger.yahoo.com/download/index.html

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: How to return a PDF file

Posted by Drew Davidson <dr...@ognl.org>.
Jamie Orchard-Hays wrote:

> Allen Gooch, who works here at Darden, wrote this service to display a
> document. It should get you going. There are two main parts, the Service and
> the Link to it. The service is specified in a library file, which is then
> specified in the .application file. Hopefully this will help you out.

There is a possible gotcha with this code.  I wrote a PDF generator action that did something similar.

The headers:

             response.setHeader("Cache-Control", "no-cache");
             response.setHeader("Cache-Control", "no-store");
             response.setHeader("Cache-Control", "must-revalidate");
             response.setHeader("Cache-Control", "max-age=10");
             response.setHeader("Expires", "0");
             response.setHeader("Pragma", "no-cache");

really messed up Acrobat 4 & 5 when viewed in IE.  IE would present the user with a "you are 
downloading a non-secure document" window, then proceed to fail to download the file.  The only fix 
that worked was to remove all the cache control headers; I was using 4 of the above techniques above 
(Expires, Pragma, Cache-Control: no-cache, no-store).

Just FYI in case you see odd problems with Acrobat and IE.

- Drew
-- 
+---------------------------------+
< Drew Davidson | OGNL Technology >
+---------------------------------+
|  Email: drew@ognl.org          /
|    Web: http://www.ognl.org   /
|    Vox: (520) 531-1966       <
|    Fax: (520) 531-1965        \
| Mobile: (520) 405-2967         \
+---------------------------------+


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: How to return a PDF file

Posted by Jamie Orchard-Hays <ja...@dang.com>.
Allen Gooch, who works here at Darden, wrote this service to display a
document. It should get you going. There are two main parts, the Service and
the Link to it. The service is specified in a library file, which is then
specified in the .application file. Hopefully this will help you out.


----------------Code--------------------
//this gets and returns the Document
public class Resume extends AbstractService {

    private static final Log log = LogFactory.getLog(ResumeService.class);
    public static final String SERVICE_NAME = "resume";

    public String getName() {
        return SERVICE_NAME;
    }

    public void service(IEngineServiceView engineServiceView, IRequestCycle
cycle, ResponseOutputStream outputStream) throws ServletException,
IOException {
        // get our key for resume retrieval
        Object[] parameters = getParameters(cycle);
        Integer resumeId = null;
        if(parameters.length > 0){
            resumeId = (Integer) parameters[0];
        }
        log.info("Resume Service retrieving resume [" + resumeId + "]");
        Integer studentId = null;
        //see if we're dealing with another student.
        if(parameters.length > 1){
            studentId = (Integer) parameters[1];
            log.info("Resume Service retrieving resume for studentId [" +
studentId + "]");
        }

        try {
            // fetch our resume
            ResumesData resume = null;
            if(studentId == null && resumeId == null){
                resume =
WebBusinessMethodsFactory.getStudentNetworkingMethods().findMyBookResume();
                log.info("Resume Service successfully retrieved book resume
from getStudentNetworkingMethods");
            } else if(studentId == null){
                resume =
WebBusinessMethodsFactory.getStudentResumesMethods().findResume(resumeId);
                log.info("Resume Service successfully retrieved resume [" +
resumeId + "]");
            }else{
                resume =
WebBusinessMethodsFactory.getStudentNetworkingMethods().findResume(resumeId)
;
                log.info("Resume Service successfully retrieved resume for
some other student besides logged in user [" + resumeId + "]");
            }
            // don't allow the browser to cache it
            HttpServletResponse response =
cycle.getRequestContext().getResponse();
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Cache-Control", "no-store");
            response.setHeader("Cache-Control", "must-revalidate");
            response.setHeader("Cache-Control", "max-age=10");
            response.setHeader("Expires", "0");
            response.setHeader("Pragma", "no-cache");

            // give it a filename and disposition
            String filename = resume.getNotes() + "." + resume.getFileExt();
            response.setHeader("Content-Disposition", "inline;filename=" +
filename);

            // write it out
            outputStream.setContentType(resume.getMimeType());
            byte[] resumeBuffer = resume.getResume();
            outputStream.write(resumeBuffer);
        } catch (SystemException e) {
            log.info("Resume Service failed to retrieve resume [" + resumeId
+ "]");
            throw new ApplicationRuntimeException(e);
        }

    }

    public ILink getLink(IRequestCycle cycle, IComponent component, Object[]
parameters) {
        return constructLink(cycle, SERVICE_NAME, null, parameters, false);
    }

}
----------------.library specification---------------
<library-specification>

  <service name="resume"
    class="edu.darden.career.student.tapestry.ResumeService"/>

</library-specification>

-----------------------------Code-----------------
//this generates a link to the document
public abstract class Resume extends AbstractLinkComponent {

    private ILinkRenderer renderer;

    public abstract Integer getResumeId();

    public abstract void setResumeId(Integer resumeId);

    //optional
    public abstract Integer getStudentId();

    public abstract void setStudentId(Integer studentId);

    protected void renderComponent(IMarkupWriter writer, IRequestCycle
cycle) {
        IEngineService service =
cycle.getEngine().getService(ResumeService.SERVICE_NAME);
        Object[] parameters = null;
        if (getStudentId() == null && getResumeId() == null) {
            parameters = new Object[0];
        } else if (getStudentId() == null) {
            parameters = new Object[]{getResumeId()};
        } else {
            parameters = new Object[]{getResumeId(), getStudentId()};
        }
        ILink link = service.getLink(cycle, this, parameters);
        writer.begin("a");
        writer.attribute("href", link.getURL());
        renderInformalParameters(writer, cycle);
        renderBody(writer, cycle);
        writer.end();
    }

    public String getAnchor() {
        return null;
    }

    public ILinkRenderer getRenderer() {
        return this.renderer;
    }

    public boolean isDisabled() {
        return false;
    }

    public void setRenderer(ILinkRenderer renderer) {
        this.renderer = renderer;
    }

}

----------------.page file--------------------
<component-specification class="edu.darden.career.student.tapestry.Resume"
                         allow-body="yes"
                         allow-informal-parameters="yes" >

  <parameter name="resumeId" direction="in" required="no"
type="java.lang.Integer"/>
  <parameter name="studentId" direction="in" required="no"
type="java.lang.Integer"/>

</component-specification>

-------------.application file--------------
<library id="resume" specification-path="/WEB-INF/ResumeService.library"/>


----- Original Message ----- 
From: "ausias vives" <au...@yahoo.co.uk>
To: <ta...@jakarta.apache.org>
Sent: Monday, April 26, 2004 12:07 PM
Subject: How to return a PDF file


> Hello,
>
> One of my methods produce a PDF file that I must
> return as the page to be seen directly in the browser,
> I'm not sure about the best way to get it right.
>
> Also I'd like that page to be seen in a new window, Is
> there a component that allows that? PopupLink doesn't
> allow parameteres to be passed, and I need them.
>
> Thanks
>
>
>
>
>
> ____________________________________________________________
> Yahoo! Messenger - Communicate instantly..."Ping"
> your friends today! Download Messenger Now
> http://uk.messenger.yahoo.com/download/index.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org