7

I am trying to generate the pdf from following python programming but generated output doesn't display hebrew letters correctly

# -*- coding: utf-8 -*-
from reportlab.pdfgen import canvas
def hello(c):
    c.drawString(100,100, "מה שלומך")
c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()
Jona
  • 1,218
  • 1
  • 10
  • 20
user634615
  • 647
  • 7
  • 17

2 Answers2

11

This code (see below) works! All you need to do is place ArialHB.ttf (or any other font that supports Hebrew characters) into site-packages/reportlab/fonts...

The desired output will be at the bottom of the pdf page.

# -*- coding: utf-8 -*-

from reportlab.pdfgen import canvas 
from reportlab.pdfbase import pdfmetrics 
from reportlab.pdfbase.ttfonts import TTFont 

pdfmetrics.registerFont(TTFont('Hebrew', 'ArialHB.ttf'))

def hello(c):
    c.setFont("Hebrew", 14)
    c.drawString(10,10, u"מה שלומך".encode('utf-8'))

c = canvas.Canvas("hello.pdf") 
hello(c) 
c.showPage()
c.save()
Gerald Spreer
  • 413
  • 4
  • 11
2

If you use the proper decode call like "מה שלומך".decode("utf-8") it works.

# -*- coding: utf-8 -*-
from reportlab.pdfgen import canvas
def hello(c):
    c.drawString(100,100, "מה שלומך".decode("utf-8"))
c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
Gerald Spreer
  • 413
  • 4
  • 11
  • 3
    Or, more simply, use `u"מה שלומך"`. – Martijn Pieters Jun 09 '12 at 09:14
  • 1
    Hrm, Reportlab states that both `unicode` and `utf-8` encoded strings are supported, so this cannot be the answer. See the [PDF userguide](http://www.reportlab.com/docs/reportlab-userguide.pdf), page 41, section 3.1. – Martijn Pieters Jun 09 '12 at 09:23
  • My experience has been that ReportLab flubs on Unicode and you need to use UTF-8. I've had many problems with non-ASCII characters solved just by making sure everything is UTF-8. – Gordon Seidoh Worley Jun 09 '12 at 16:12
  • above solution doesn't work for me. I have already tried these permutation and combinations. – user634615 Jun 10 '12 at 08:03