Fair enough, Richard, I'm providing sample code here, and I will link to it 
in the issue report.
Comments in the sample code explain the 0x95 codec error and also ask some 
questions.
Thanks for your help.

Download TTF font DejaVuSans.ttf and copy it into gluon/contrib/fpdf/
You can download it from the FPDF site 
http://code.google.com/p/pyfpdf/downloads/detail?name=fpdf_unicode_font_pack.zip&can=2&q=

Add this function to controllers/default.py

def issue(): return dict()


Replace views/generic.pdf
{{"""
replace the standard views/generic.pdf file with this file
"""
import os
# we comment this as we are using a custom model function
# from gluon.contrib.generics import pdf_from_html
filename = '%s/%s.html' % (request.controller,request.function)
if os.path.exists(os.path.join(request.folder,'views',filename)):
    html=response.render(filename)
else:
    html=BODY(BEAUTIFY(response._vars)).xml()
pass
# note the pyfpdf instead of pdf in the function call
# pyfpdf is defined in views/issue.html
=pyfpdf_from_html(html)
}}

Create views/default/issue.html
{{"""
Put this file in [app]/default/issue.html then browse .../default/issue.pdf.
You should see a PDF file with two bullets, both with garbage characters, 
one for an accented a, the other one for the euro sign.
Now comment out the line after #1# below and uncomment the similar line 
after #2#.
The aim is to print out the two characters correctly. Browse again the pdf 
page to test...
Instead you will get a codec error involving character 0x95.
0x95 is the bullet sign that FPDF adds for UL() below (counter-proof: 
replace OL() for UL() to get going)
This reproduces issue #66 that I reported on the FPDF google code site 
http://code.google.com/p/pyfpdf/issues/detail?id=66.

I'm still left scratching my head on how to print the euro character and 
the accented a character at the same time.
I tried playing with codecs in #3# below to no avail.
"""
def text(a):
    print repr(a)
    return a.decode("utf-8").encode("utf-8")
    #3# setting encode 'latin-1' prints accented characters (but errors out 
on euro, which needs utf-8 encoding)

def pyfpdf_from_html(html):
    import os
    from gluon.contrib.fpdf import FPDF, HTMLMixin
    
    class MyFPDF(FPDF, HTMLMixin):
        pass
    
    pdf = MyFPDF()
    pdf.add_font('DejaVu', '', 'DejaVuSans.ttf', uni=True)
    pdf.set_font('DejaVu', '', 12)
    pdf.add_page()
    #1#this works but the default font doesn't include the euro character
    pdf.write_html('%s' % html)
    #2#this errors out with 'utf8' codec can't decode byte 0x95 in position 
5: invalid start byte
    ###note that 0x95 is the bullet sign that FPDF adds for UL() below - 
replace it with OL() to get going
    #pdf.write_html('<font face="DejaVu" size="12">%s<font>' % html)
    return XML(pdf.output(dest='S'))
}}

{{=HTML(BODY(UL([text('à'),text('€')])))}}



On Monday, October 28, 2013 9:46:10 PM UTC+1, Richard wrote:
>
> did you user your_string.decode('utf8') before passing to FPDF??
>
> I think you have mistake in your code, you should provide example code in 
> you issue report!!
>
> Richard
>
>
> On Mon, Oct 28, 2013 at 2:40 PM, step <step...@gmail.com <javascript:>>wrote:
>
>> I discovered and reported in 
>> http://code.google.com/p/pyfpdf/issues/detail?id=66 an issue with 
>> gluon/contrib/FPFD. Basically, rendering HTML <UL> tags as PDF may trigger 
>> a utf8 codec error because fpdf/html.py sets the bullet character as 
>> '\x95'. As a work-around I have replaced '\x95' with '*'.
>>  
>> -- 
>> Resources:
>> - http://web2py.com
>> - http://web2py.com/book (Documentation)
>> - http://github.com/web2py/web2py (Source code)
>> - https://code.google.com/p/web2py/issues/list (Report Issues)
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "web2py-users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to web2py+un...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to