You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Paul Stanton <pa...@mapshed.com.au> on 2010/07/19 13:01:03 UTC

pdf generation using flying saucer

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-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: pdf generation using flying saucer

Posted by Josh Canfield <jo...@gmail.com>.
>
> 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 <pa...@mapshed.com.au> 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: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>



-- 
--
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: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: pdf generation using flying saucer

Posted by Paul Stanton <pa...@mapshed.com.au>.
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 <pa...@mapshed.com.au>
>>
>>  
>>> 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-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>
>>>
>>>     
>>
>>
>>   
>

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


Re: pdf generation using flying saucer

Posted by Paul Stanton <pa...@mapshed.com.au>.
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 <pa...@mapshed.com.au>
>
>   
>> 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-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>>     
>
>
>   

Re: pdf generation using flying saucer

Posted by Christophe Cordenier <ch...@gmail.com>.
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 <pa...@mapshed.com.au>

> 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-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Regards,
Christophe Cordenier.

Committer on Apache Tapestry 5
Co-creator of wooki @wookicentral.com

Re: pdf generation using flying saucer

Posted by Moritz Gmelin <mo...@gmx.de>.
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-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


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