Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 850
lisek Avatar asked Jun 09 '13 09:06

lisek


1 Answers

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!

like image 131
Paulo Bu Avatar answered Sep 30 '22 14:09

Paulo Bu