7

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.

Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
lisek
  • 267
  • 4
  • 16
  • Where are you getting that wrong output? In the generated pdf? – Paulo Bu Jun 09 '13 at 13:31
  • Yes, this what I get is copied from pdf (I replaced copied 'nn' with black squares - this is what I see in pdf). – lisek Jun 09 '13 at 13:33
  • Try this line in `GenerujPustyAskusz` method instead: `c.drawString(450,750, "Załącznik nr 2 do Regulaminu".decode('utf-8'))` also, why are you tagging this questions as latin2, I think you're using utf-8? – Paulo Bu Jun 09 '13 at 13:41
  • Letters I need are latin2 (iso-8859-2) like łóżźćńś. I tried this trick with no result. – lisek Jun 09 '13 at 13:47

1 Answers1

16

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!

Paulo Bu
  • 29,294
  • 6
  • 74
  • 73