pdf generation using flying saucer

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

pdf generation using flying saucer

Paul Stanton-4
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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: pdf generation using flying saucer

Christophe Cordenier
Hi

You should consider tapx-templating component at
http://tapestry.formos.com/nightly/tapx/ to see how page can be rendered

Also, we did that for Wooki at
http://github.com/robink/wooki/blob/master/src/main/java/com/wooki/services/export/FlyingSaucerInputRender.javaSolution
is not perfect but does the trick for wooki's book (i.e. try
download pdf at http://wookicentral.com/demo/book/1)


2010/7/19 Paul Stanton <[hidden email]>

> 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: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


--
Regards,
Christophe Cordenier.

Committer on Apache Tapestry 5
Co-creator of wooki @wookicentral.com
Reply | Threaded
Open this post in threaded view
|

Re: pdf generation using flying saucer

Moritz Gmelin
In reply to this post by Paul Stanton-4
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: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: pdf generation using flying saucer

joshcanfield
In reply to this post by Paul Stanton-4
>
> 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)?
>

I haven't had to do this for a while, but I finally settled on using
HttpClient and letting tapestry do it's thing as though I was a
regular client. Just create a link to the page like would any other
and parameters you need to the context. If you want to use the current
user session then just add the jsessionid to the generated url...

Josh

On Mon, Jul 19, 2010 at 4:01 AM, Paul Stanton <[hidden email]> wrote:

> 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: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>



--
--
http://www.bodylabgym.com - a private, by appointment only, one-on-one
health and fitness facility.
--
http://www.ectransition.com - Quality Electronic Cigarettes at a
reasonable price!
--
TheDailyTube.com. Sign up and get the best new videos on the internet
delivered fresh to your inbox.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: pdf generation using flying saucer

Paul Stanton-4
In reply to this post by Christophe Cordenier
Thanks Chris,

That sounds like a good fit, however the source code at github seems to
be missing? or private?

If so, I'd appreciate it if you could paste some example code here?

Regards, p.

Christophe Cordenier wrote:

> Hi
>
> You should consider tapx-templating component at
> http://tapestry.formos.com/nightly/tapx/ to see how page can be rendered
>
> Also, we did that for Wooki at
> http://github.com/robink/wooki/blob/master/src/main/java/com/wooki/services/export/FlyingSaucerInputRender.javaSolution
> is not perfect but does the trick for wooki's book (i.e. try
> download pdf at http://wookicentral.com/demo/book/1)
>
>
> 2010/7/19 Paul Stanton <[hidden email]>
>
>  
>> 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: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>>    
>
>
>  
Reply | Threaded
Open this post in threaded view
|

Re: pdf generation using flying saucer

Paul Stanton-4
Actually, since it needs to run in it's own memory space (separate from
the real tapestry application) it's not going to work for me.

Paul Stanton wrote:

> Thanks Chris,
>
> That sounds like a good fit, however the source code at github seems
> to be missing? or private?
>
> If so, I'd appreciate it if you could paste some example code here?
>
> Regards, p.
>
> Christophe Cordenier wrote:
>> Hi
>>
>> You should consider tapx-templating component at
>> http://tapestry.formos.com/nightly/tapx/ to see how page can be rendered
>>
>> Also, we did that for Wooki at
>> http://github.com/robink/wooki/blob/master/src/main/java/com/wooki/services/export/FlyingSaucerInputRender.javaSolution 
>>
>> is not perfect but does the trick for wooki's book (i.e. try
>> download pdf at http://wookicentral.com/demo/book/1)
>>
>>
>> 2010/7/19 Paul Stanton <[hidden email]>
>>
>>  
>>> 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: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>
>>>    
>>
>>
>>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]