Convert html page to pdf

229 views
Skip to first unread message

abc_coder

unread,
Jun 20, 2012, 8:58:18 AM6/20/12
to turbo...@googlegroups.com
Hi All

I want to convert my web page to pdf, write it to file and allow users to download it for print.
But I don't have any idea how do this with current version of tg.
I found some examples for xhtml2pdf but I couldn't make it working with my app.
The doc for xhtml2pdf are very poor and I had problems with finding any examples.

I tried also shell script wkhtmltopdf but it's not working for pages with authentication.

Is there any easy way of doing that? how can I get data from genshi template to convert it to pdf?

I would be very grateful for giving some good code examples.

Timuçin Kızılay

unread,
Jun 20, 2012, 9:08:06 AM6/20/12
to turbo...@googlegroups.com
I need that too.
maybe a quick example to generate pdf files would be better suited. Not
convert html to pdf but generating directly to pdf would be better.

Alessandro Molina

unread,
Jun 20, 2012, 2:02:58 PM6/20/12
to turbo...@googlegroups.com
Minimal example with xhtml2pdf is:

from xhtml2pdf.pisa import CreatePDF
from tg import render_template (or from tg.render import
render as render_template for TG2.1)

html = render_template({"param1": value1, "param2": value2},
"genshi", "myapp.templates.page",
doctype=None)

f = StringIO()
CreatePDF(StringIO(html), f)
f.seek(0)
return f

There is even an example to integrate it as a middleware somewhere if
I'm not wrong.
> --
> 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.
>

Adrian Jasiński

unread,
Jun 21, 2012, 3:13:02 AM6/21/12
to turbo...@googlegroups.com
Thanks a lot for example. It didn't work but I found a solution with
work. After some modifications:

from xhtml2pdf.pisa import CreatePDF, startViewer
from tg.render import render as render_template
import cStringIO
html = render_template({"param1": 1, "param2": 2}, "genshi",
"myapp.templates.page", doctype=None)
dest = '/home/user/files/pdf/test.pdf'
result = file(dest, "wb")
pdf = CreatePDF(cStringIO.StringIO(html), result)
result.close()
import paste.fileapp
f = paste.fileapp.FileApp('/home/user/files/pdf/test.pdf')
from tg import use_wsgi_app
return use_wsgi_app(f)

It's work fine without national characters but with it in template
it's returning the error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0119' in
position 2497: ordinal not in range(128)

I have in my script:
# -*- coding: utf-8 -*-

and also in template:

<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"
py:if="False"/>

How can I set default encoding to utf-8 for render? Or is there any
other solution?

please help





2012/6/20 Alessandro Molina <alessand...@gmail.com>:
Message has been deleted

abc_coder

unread,
Jun 22, 2012, 9:24:11 AM6/22/12
to turbo...@googlegroups.com
Solution:

pdf = CreatePDF(cStringIO.StringIO(html.encode("UTF-8")), result, encoding="utf-8")
>> turbogears+unsubscribe@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 post to this group, send email to turbo...@googlegroups.com.
> To unsubscribe from this group, send email to turbogears+unsubscribe@googlegroups.com.

Stuart Zurcher

unread,
Apr 17, 2013, 2:07:13 PM4/17/13
to turbo...@googlegroups.com
>> 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 post to this group, send email to turbo...@googlegroups.com.
> To unsubscribe from this group, send email to turbogears+...@googlegroups.com.

Stuart Zurcher

unread,
Apr 17, 2013, 2:16:14 PM4/17/13
to turbo...@googlegroups.com
I know this is an older post, however, used this to access my template with parameters passed...

        import urllib, tg
        import xhtml2pdf.pisa as pisa 
        dest = "test-website.pdf"
        env = []
        env = request.environ
        url = "http://"
        url += env['HTTP_HOST']
        url +=  '/def_for_html?selected=' + kw['selected']

       
        result = file(dest, "wb")
        pdf = pisa.CreatePDF(
            urllib.urlopen(url),
            result,
            log_warn=1,
            log_err=1,
            path=url,
            link_callback=pisa.pisaLinkLoader(url).getFileName  # retrieves css and js files for html
            )
        result.close()
        import paste.fileapp
        f = paste.fileapp.FileApp(dest)

        from tg import use_wsgi_app
        return use_wsgi_app(f)

The css that html2pdf handles is very limited... floats were my main problem.  Had to change html to tables as floats kept throwing up divide by 0 errors.
       

Stuart Zurcher

unread,
Apr 22, 2013, 1:34:47 PM4/22/13
to turbo...@googlegroups.com
I ended up using WeasyPrint. It works great with css and page breaking.  The only issue I ran into was cairo ran out of memory on one of my photos so I had to limit the photo size.

pip install pillow  #helps to install PIL in virtualenv and resolve link issues
pip install weasyprint

 
def printPDF(self, **kw)
        from weasyprint import HTML
 
        url = "http://"

        env = []
        env = request.environ
        url += env['HTTP_HOST']
        url += '/urlName?params=' + kw['params']
        pdf = HTML(url).write_pdf()
        return (pdf)

Reply all
Reply to author
Forward
0 new messages