HTML to PDF

64 views
Skip to first unread message

León Domingo

unread,
Sep 18, 2011, 3:54:27 AM9/18/11
to TurboGears
Hi,
I've created a little function that makes use of wkhtmltopdf (http://
code.google.com/p/wkhtmltopdf/) to get PDF content from HTML content.

I would like to use some controllers to get a PDF version of the HTML
content defined in the corresponding template.

@expose('foo.html')
def foo(self, *args, **kwargs):
return dict(....)

I've been making some experiments with a middleware class.

response.content_type = 'application/pdf'
response.body = to_pdf(response.body)

return response(environ, start_response)

It works but I need something to know if I have to make the
translation or not

if is_a_suitable_response:
response.content_type = 'application/pdf'
response.body = to_pdf(response.body)

return response(environ, start_response)

and I don't know how I can "mark" the response to do so. In the other
hand, I don't know if this is the right way to do that.

Thanks a lot

León

Michael Pedersen

unread,
Sep 18, 2011, 10:10:17 PM9/18/11
to turbo...@googlegroups.com
Is there a reason you can't do something like this instead?

class RootController(BaseController):
     @expose('pdf', content_type='application/pdf')
     def foo(self, *args, **kw):
       return string_object_that_contains_pdf_file_contents

It's the way I've done it elsewhere, and it works nicely.


--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to turbo...@googlegroups.com.
To unsubscribe from this group, send email to turbogears+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en.




--
Michael J. Pedersen
My IM IDs: Jabber/pede...@icelus.tzo.com, AIM/pedermj022171
          Yahoo/pedermj2002, MSN/pederm...@hotmail.com
My Online Resume: http://www.icelus.org/
Twitter: pedersentg

Antti Haapala

unread,
Sep 19, 2011, 1:01:54 AM9/19/11
to turbo...@googlegroups.com

2011/9/18 León Domingo <leon.d...@ender.es>
Hi,
I've created a little function that makes use of wkhtmltopdf (http://
code.google.com/p/wkhtmltopdf/) to get PDF content from HTML content.

I would like to use some controllers to get a PDF version of the HTML
content defined in the corresponding template.

I've been making some experiments with a middleware class.

It works but I need something to know if I have to make the
translation or not

and I don't know how I can "mark" the response to do so. In the other
hand, I don't know if this is the right way to do that.

We are using xhtml2pdf in our project; it had a WSGI Middleware already,
that looks for 'pisa.topdf' in request environ; if present, it translates html
to pdf and also sets the default filename. In the controller, if the response would be ok, I do

request.environ["pisa.topdf"] = "filename.pdf"

In my opinion this is exactly the "wrong thing to do", talking to middleware
using environ, or even talking at all... but it works.

León Domingo

unread,
Sep 19, 2011, 2:20:41 AM9/19/11
to TurboGears
Hi Michael,
I can't do that cause I need TG to give me the HTML content. The first
thing i tried to do was to render the template "myself" but i got some
errors. For example, I was using something like {{tg.url('....')}} and
render_jinja2 fails because it didn't know what "tg" means. I guess
that road gets me to a place where I need "tmpl_context" and things
like that, so I would have to set up things the way TG already does.

León
> My IM IDs: Jabber/peder...@icelus.tzo.com, AIM/pedermj022171
>           Yahoo/pedermj2002, MSN/pedermj022...@hotmail.com

Alessandro Molina

unread,
Sep 19, 2011, 9:13:27 AM9/19/11
to turbo...@googlegroups.com
Uhm, I would suggest you to setup a after_render hook and decide if
the render result must be converted to a PDF or not.
In the case it doesn't have to be converted you can leave it go as
HTML, otherwise you can call wkhtmltopdf

León Domingo

unread,
Sep 19, 2011, 9:32:05 AM9/19/11
to TurboGears
Hi Alessandro,
And how would it be this way? I don't know exactly how "after_render"
has to be used. As far as I know, you define a function with a
"response" parameter and then a decorator "after_render" passing that
function in.

def _after_render(r):
# something to be done
r.content_type = 'application/pdf'
r.body = to_pdf(r.body)

@expose('foo.html')
@after_render(_after_render)
def foo(self, *args, **kwargs):
return dict(....)

is it correct? I was trying this way but...can I modify "response"
inside "_after_render"? I thought it was not possible...

Thanks

León

On 19 sep, 15:13, Alessandro Molina <alessandro.mol...@gmail.com>
wrote:

Alessandro Molina

unread,
Sep 19, 2011, 10:38:32 AM9/19/11
to turbo...@googlegroups.com
you can use Decoration.get_decoration(controller_method).register_hook('after_render',
callable)
and callable will be execute after rendering the template with the
rendering result as parameter.

León Domingo

unread,
Sep 27, 2011, 3:54:35 AM9/27/11
to TurboGears
Thanks everyone. At last I've implemented a "mix of solutions"

On Sep 19, 4:38 pm, Alessandro Molina <alessandro.mol...@gmail.com>
wrote:

Matthew Boehm

unread,
May 22, 2012, 2:56:40 PM5/22/12
to turbo...@googlegroups.com
I am trying to implement a PDF converter as well, but have run into some problems:

* after_render hooked functions only receive a literal with the rendered result. Modifying this result/returning the modified version in the hooked function does not change what is written to the response object.
* If you try writing directly to the response object, sometime after your function returns, the entire original response is written to the response object.

Is there a way to modify the response's content and headers in a function hooked to after_render?

>> > To unsubscribe from this group, send email to turbogears+unsubscribe@googlegroups.com.


>> > For more options, visit this group athttp://groups.google.com/group/turbogears?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "TurboGears" group.
> To post to this group, send email to turbo...@googlegroups.com.

> To unsubscribe from this group, send email to turbogears+unsubscribe@googlegroups.com.

Alessandro Molina

unread,
May 23, 2012, 4:11:43 PM5/23/12
to turbo...@googlegroups.com
Actually after_render hook permits to modify the rendered page.

It gets as parameter a dictionary where the key "response" is the
response getting out from that request.
Replacing the content of that key replaces the response sent by TurboGears.
>> >> > turbogears+...@googlegroups.com.
>> >> > For more options, visit this group
>> >> > athttp://groups.google.com/group/turbogears?hl=en.
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "TurboGears" group.
>> > To post to this group, send email to turbo...@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > turbogears+...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/turbogears?hl=en.
>> >
>> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "TurboGears" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/turbogears/-/L9-eC9I7gBkJ.
>
> To post to this group, send email to turbo...@googlegroups.com.
> To unsubscribe from this group, send email to
> turbogears+...@googlegroups.com.

Matt

unread,
May 24, 2012, 10:42:45 AM5/24/12
to turbo...@googlegroups.com
My apologies; I was using tg2 2.1.1, and the code seems to have changed to pass a dictionary in 2.1.4.

For those finding this thread after me via google, the solution I ended up going with is using xhtml.wsgi.PisaMiddleware (sample usage in  https://github.com/chrisglass/xhtml2pdf/tree/master/demo/wsgi )

Thanks for your response, Alessandro

邓超

unread,
May 24, 2012, 9:29:09 PM5/24/12
to turbo...@googlegroups.com
You can try phantomJS, it's very easy to convert html to PDF file.

2012/5/24 Matt <bigbo...@gmail.com>



--
Deng Chao

León Domingo

unread,
May 25, 2012, 5:19:03 AM5/25/12
to turbo...@googlegroups.com
Hi,
phantomJS looks like Node.js, isn't it?

Regards,
León Domingo

Ender - Factoría de Software
leon.d...@ender.es
Avda. Cerro del Águila, 7. S23 - 2ª Planta
28703 S.S. de los Reyes (MADRID)
Tlf. 902 01 44 01

   
Reply all
Reply to author
Forward
0 new messages