Hi,

we had the same challenge to create PDF screenshots from our application. At 
that time I did not know about tapx-templating.

We ended up using a native library wkhtmltopdf on the server that we call from 
a tapestry page and use the referred in the URL as the URL to pass to 
wkhtmltopdf. You can also pass the current SessionID to wkhtmltopdf in order to 
handle state information correcty.

Here's the code

Regards

Moritz


public class PDFScreenShot
{

        @Inject
        private RequestGlobals requestGlobals;


        public StreamResponse onActivate()
        {
                String url = 
requestGlobals.getHTTPServletRequest().getHeader("Referer");
                String sessionID = 
requestGlobals.getHTTPServletRequest().getRequestedSessionId();

                try
                {
                        final File f = File.createTempFile("pdfprint", ".pdf");
                        f.deleteOnExit();
                        String command = "wkhtmltopdf -O Landscape -s A4 
--cookie JSESSIONID "
                                                                + sessionID
                                                                + " "
                                                                + url
                                                                + " "
                                                                + 
f.getAbsolutePath();

                        Process p = Runtime.getRuntime().exec(command);
                        boolean success = false;
                        for (int i = 0; i < 1200; i++)
                        {
                                try
                                {
                                        p.exitValue();
                                        success = true;
                                        break;
                                }
                                catch (Exception e)
                                {
                                }
                                Thread.currentThread();
                                Thread.sleep(100);
                        }
                        if (!success)
                        {
                                p.destroy();
                                return null;
                        }

                        return new StreamResponse()
                        {

                                public String getContentType()
                                {
                                        return "application/pdf";
                                }


                                public InputStream getStream() throws 
IOException
                                {
                                        return new FileInputStream(f);
                                }


                                public void prepareResponse(Response response)
                                {
                                        
response.setHeader("content-disposition",
                                                "attachment; filename=" + 
f.getName());
                                        response.setHeader("filename", 
f.getName());
                                }
                        };

                }
                catch (Throwable e)
                {
                        e.printStackTrace();
                }
                return null;

        }
}
Am 19.07.2010 um 13:01 schrieb Paul Stanton:

> Hi All,
> 
> I have a pretty bizarre problem...
> 
> There's a neat little project called 'flying saucer' which is a handy way to 
> create PDF files (among other things) from HTML.
> 
> In my tapestry project I need to create a PDF file and save it to hard disk.
> 
> To do this, on a separate thread to any http request, I programmatically 
> create some html, push it through the flying saucer API as a stream, and 
> write the output to a file.
> 
> All this is fairly simple, however within a tapestry project the easiest way 
> to create HTML is via tapestry and I'd like to capitalise on this.
> 
> I imagine I can make my own http request to the running tapestry project in 
> order to do this, but I'm not clear on the best way to inject some state 
> information into scope of the request so that it knows what html it needs to 
> generate.
> 
> So forget PDF's and flying saucers for a moment, what is the tidiest way to 
> use tapestry to generate html within the application (ie not on the client)?
> 
> hopefully I've explained what I need to do adequately, sorry if it's not 
> clear.
> 
> Regards, Paul.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> For additional commands, e-mail: users-h...@tapestry.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to