ReptarX / pyfpdf

Automatically exported from code.google.com/p/pyfpdf
GNU Lesser General Public License v3.0
0 stars 0 forks source link

Unicode problem in fpdf #68

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
A. What steps will reproduce the problem?

1. Unzip the unicode fonts pack 'fpdf_unicode_font_pack.zip' (available in the 
downloads section of this site) into <web2py root>/gluon/contrib/fpdf/font

2.in the models:
db.define_table('post',
                Field('minima', 'text'),
                Field('user_id', db.auth_user),
                Field('created_on', 'datetime',default=request.now,
                       update=request.now,
                       writable=False,
                       readable=False)
               )

3. Report function in pdfReport.py controller:
def simpleListingFpdf():
    from gluon.contrib.fpdf import FPDF
    pdf = FPDF()
    pdf.add_page()

    pdf.add_font('DejaVuSans','','DejaVuSans.ttf',uni=True)
    pdf.set_font('DejaVuSans','',12)

#   define table head and foot rows:
    head = THEAD(TR(TH(unicode("Μήνυμα", 'utf8'), _width="50%")
                   ,TH("User ID", _width="20%")
                   ,TH("Καταχωρήθηκε", _width="30%")
                   ,_bgcolor="#00FF00"
                   )
                 )
    foot = TFOOT(TR(TH("Footer 1", _width="50%")
                   ,TH("Footer 2", _width="20%")
                   ,TH("Footer 3", _width="30%")
                   ,_bgcolor="#E0E0E0"
                   )
                 )
#   create the data object: fetch data from the database:
    qryMessages = db(db.post.id>0).select(db.post.minima,
                                          db.post.user_id,
                                          db.post.created_on)
    for i in qryMessages:
        if not i.minima: i.minima=' '
        if not i.user_id: i.user_id=' '
        if not i.created_on: i.created_on=' '

#   create html table from 'rows' list:
    rows = []
    for i in range(len(qryMessages)):
        col = i%2 and "CCFF66" or "FF0000" #color each row differently
        rows.append(TR(TD(qryMessages[i].minima)
                      ,TD(qryMessages[i].user_id)
                      ,TD(qryMessages[i].created_on)
                      ,_bgcolor=col
                      )
                    )
#   create the table object
    body = TBODY(*rows)

    table = TABLE(*[head,foot,body]
                  ,_border="1"
                  ,_align="center"
                  ,_width="100%"
                  )

    if request.extension == "pdf":
        from reportsStart import PDFfromHTML
        pdfReport = PDFfromHTML()

        current.response.title = "web2py Report: Δοκιμή (Test)"
        pdfReport.add_page()

        pdfReport.add_font('DejaVuSansCondensed','', \
                           'DejaVuSansCondensed.ttf',uni=True)
        pdfReport.add_font('ArialUnicode','','ARIALUNI.TTF',uni=True)
        pdfReport.set_font('ArialUnicode','',14)

        pdfReport.write_html(table.xml())
        response.headers['Content-Type']='application/pdf'
        return pdfReport.output(dest='S')
#   else return normal html view:
    return dict(table=table)

4. Report definition PDFfromHTML in /modules/reportsStart.py
class PDFfromHTML(FPDF, HTMLMixin):
    current.response.pageheader = "A Simple Page Header"

#   page header
    def header(self):
        self.set_y(10)
        self.add_font('UbuntuItalic','','Ubuntu-RI.ttf',uni=True)
        self.set_font('UbuntuItalic','',10)
#    Title
        self.cell(195,10,current.response.pageheader,0,0,'L')
#    Draw a line
        self.line(10,20,110,20)
#    Line break
        self.ln(20)

#   page footer
    def footer(self):
#    Position at 1.5 cm from bottom
        self.set_y(-15)

        self.add_font('ArialUnicode','','ARIALUNI.TTF',uni=True)
        self.set_font('ArialUnicode','',12)
#    Page number
        textForTheCurrentPage = "Σελ.(Page) %s/%s" % (self.page_no(),\
                                 self.alias_nb_pages())
        self.cell(0,10,textForTheCurrentPage,0,0,'C')

B. What is the expected output? What do you see instead?
Expected output is a pdf showing user messages in Greek characters (unicode) in 
table format.
The above code provides the message table, but, instead of Greek characters, 
one sees indecipherable characters (more like Chinese than Greek)

C. What version of the product are you using? On what operating system?
Regardles of version. Same thing happens in web2py version 2.7.4, as well as 
2.8.2. OS is Windows 7 x64

D. Additional notes
Mariano solved the problem by:

1. Adding the unicode font in reportsStart.py header:
class PDFfromHTML(FPDF, HTMLMixin):
    current.response.pageheader = "A Simple Page Header"
#page header
    def header(self):
        self.set_y(10)
        self.add_font('DejaVuSans','','DejaVuSans.ttf',uni=True)
        self.add_font('DejaVuSans','B','DejaVuSans-Bold.ttf',uni=True)
        self.set_font('DejaVuSans','',10)
        ...etc...(note the 'B' in the second adding)

2. Wrapping the table object generated in simpleListingFpdf() of the 
pdfReport.py controller around TAG.FONT(), using the said DejaVuSans font:

def simpleListingFpdf():
.......................
.......................
    if request.extension == "pdf":
        from reportsStart import PDFfromHTML
        pdfReport = PDFfromHTML()

        current.response.title = "web2py Report: Δοκιμή (Test)"
        pdfReport.add_page()
        pdfReport.add_font('DejaVuSans','','ARIALUNI.TTF',uni=True)
        pdfReport.set_font('DejaVuSans','',14)

        html = TAG.FONT(table, _face="DejaVuSans", )

        response.headers['Content-Type']='text/html'

        pdfReport.write_html(html.xml())
        response.headers['Content-Type']='application/pdf'
        return pdfReport.output(dest='S')
#    else return normal html view:
    return dict(table=table)

Original issue reported on code.google.com by t_gleza...@yahoo.com on 13 Dec 2013 at 9:21

GoogleCodeExporter commented 9 years ago

Original comment by sundou19...@gmail.com on 28 Jan 2014 at 7:17

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for reporting!

The problem is solved (copying here from the original post so it will be more 
clear):

1. Adding the unicode font in reportsStart.py header:
class PDFfromHTML(FPDF, HTMLMixin):
    current.response.pageheader = "A Simple Page Header"
#page header
    def header(self):
        self.set_y(10)
        self.add_font('DejaVuSans','','DejaVuSans.ttf',uni=True)
        self.add_font('DejaVuSans','B','DejaVuSans-Bold.ttf',uni=True)
        self.set_font('DejaVuSans','',10)
        ...etc...(note the 'B' in the second adding)

2. Wrapping the table object generated in simpleListingFpdf() of the 
pdfReport.py controller around TAG.FONT(), using the said DejaVuSans font:

def simpleListingFpdf():
.......................
.......................
    if request.extension == "pdf":
        from reportsStart import PDFfromHTML
        pdfReport = PDFfromHTML()

        current.response.title = "web2py Report: Δοκιμή (Test)"
        pdfReport.add_page()
        pdfReport.add_font('DejaVuSans','','ARIALUNI.TTF',uni=True)
        pdfReport.set_font('DejaVuSans','',14)

        html = TAG.FONT(table, _face="DejaVuSans", )

        response.headers['Content-Type']='text/html'

        pdfReport.write_html(html.xml())
        response.headers['Content-Type']='application/pdf'
        return pdfReport.output(dest='S')
#    else return normal html view:
    return dict(table=table)

Original comment by reingart@gmail.com on 5 Feb 2014 at 1:45

GoogleCodeExporter commented 9 years ago
Issue 52 has been merged into this issue.

Original comment by reingart@gmail.com on 5 Feb 2014 at 4:23