pythondjangoencodingreportlabiso-8859-2

Django reportlab latin2 encoding


is there any option to convert latin2 letters in a proper way? I need polish letter to my school project. Here is some code how I generate pdf

#!/usr/bin/python
# -*- utf-8 -*-

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter, A4



def GenerujPustyArkusz(c):
    c.setFont("Times-Roman", 8)
    c.drawString(450,750, u"Załącznik nr 2 do Regulaminu")


def test():
    c = canvas.Canvas("test.pdf", pagesize=letter)
    GenerujPustyArkusz(c)
    c.showPage()
    c.save()


test()

And I get this:

Za■■cznik nr 2 do Regulaminu

I tried several encoding tricks with no result.


Solution

  • I think the main problem is that the font you're using doesn't have those polish characters. This code worked for me and showed up the characters you wanted:

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import letter, A4
    from reportlab.pdfbase import pdfmetrics
    from reportlab.pdfbase.ttfonts import TTFont   
    
    def GenerujPustyArkusz(c):
        pdfmetrics.registerFont(TTFont('Verdana', 'Verdana.ttf'))
        c.setFont("Verdana", 8)
        s = u"Załącznik nr 2 do Regulaminu"
        c.drawString(450,750, s)   
    
    def test():
        c = canvas.Canvas("test.pdf", pagesize=letter)
        GenerujPustyArkusz(c)
        c.showPage()
        c.save()  
    
    test()
    

    If you want to use other font you'll have to find the typeface you want that include the polish characters.

    I hope this helps!