Python Forum
cyrillic symbols in tables in reportlab.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cyrillic symbols in tables in reportlab.
#1
Hello! I want to use Cyrillic in tables, but reportlab outputs black squares instead of characters. Here is an example code:
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

def table_fonts():
    doc = SimpleDocTemplate("table_fonts.pdf", pagesize=letter)
    story = []
    data = [['ф_{}'.format(x) for x in range(1, 6)],
                ['а', 'ф', 'ф', 'ф', 'ф']
                ]
    tblstyle = TableStyle([('TEXTFONT', (0, 0), (0, 1), 'FreeSans') ]) 
                                   
    tbl = Table(data)
    tbl.setStyle(tblstyle)
    story.append(tbl)
    doc.build(story)
if __name__ == '__main__':
    pdfmetrics.registerFont(TTFont('FreeSans', 'FreeSans.ttf')) 
    table_fonts()
Python 3.5.3, reportlab 3.5.13
Reply
#2
see: http://code.activestate.com/recipes/4388...f-library/
Reply
#3
(Mar-19-2019, 07:39 PM)Larz60+ Wrote: see: http://code.activestate.com/recipes/4388...f-library/

Thanks for your answer! Unfortunately this does not work, the problem is not that there is no suitable font, I think,that there should be some other way to change the font in the table is not through canvas

I added this code to my, the one that led above, now it gives me just a blank sheet

If I'm going on and fix the font to a normal string that is printed by using canvas, it's all right and the characters are introduced, the problem is that whatever font I set in canvas, SimpleDocTemplate.build won't use it and I don't know why. Here is a sample code that prints the Cyrillic alphabet canvas

from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont


canvas = Canvas("canvas.pdf", pagesize=A4)
pdfmetrics.registerFont(TTFont('FreeSans', 'FreeSans.ttf'))
canvas.setFont('FreeSans', 32)
canvas.drawString(10, 150, "Сам текст энкодед ин UTF-8")
canvas.drawString(10, 100, "Шревтом FreeSans!")
canvas.showPage()
canvas.save()
That's what I get. Everything works:
Here is the code from the first table, but with the addition of what I drop above:
That's what I get on the way out.
Reply
#4
It's working! It turned out that under registering the fonts, here is this row:
pdfmetrics.registerFont(TTFont('FreeSans', 'FreeSans.ttf'))
It picks up the font from the folder from which it is launched. This font wasn't there, so it didn't work. Why does it output Russian strings using DrawString when there is no font in the folder is a good question, but I have no answer. Here is an example of working code:
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
def table_fonts():
    doc = SimpleDocTemplate("table_fonts.pdf", pagesize=letter)
    story = []
    data = [['ф_{}'.format(x) for x in range(1, 6)],
    ['фф', 'ф', 'ф', 'ф', 'ф']
    ]
    tblstyle = TableStyle([('FONT', (0, 0), (-1, 1), 'DejaVuSerif', 24)
    ])
    tbl = Table(data)
    tbl.setStyle(tblstyle)
    story.append(tbl)
    doc.build(story)
if __name__ == '__main__':
    pdfmetrics.registerFont(TTFont('DejaVuSerif', 'DejaVuSerif.ttf'))
    table_fonts()
make sure that the “DejaVuSerif.ttf" is in the folder next to the file. All the blessings
Reply
#5
Thanks for coming back with how you solved it :)

I think there might be something else going on here. From what I've seen other places (such as: https://stackoverflow.com/a/52450839 or http://www.blog.pythonlibrary.org/2013/0...out-fonts/), reportlab has a big list of system directories it looks for fonts, so you shouldn't be required to have it in the same folder as the file you're running.

But, if it works, and you're happy, then it's GoodEnough™.
Reply
#6
(Mar-20-2019, 07:36 AM)hiroz Wrote: It's working! It turned out that under registering the fonts, here is this row:
pdfmetrics.registerFont(TTFont('FreeSans', 'FreeSans.ttf'))
It picks up the font from the folder from which it is launched. This font wasn't there, so it didn't work. Why does it output Russian strings using DrawString when there is no font in the folder is a good question, but I have no answer. Here is an example of working code:
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
def table_fonts():
    doc = SimpleDocTemplate("table_fonts.pdf", pagesize=letter)
    story = []
    data = [['ф_{}'.format(x) for x in range(1, 6)],
    ['фф', 'ф', 'ф', 'ф', 'ф']
    ]
    tblstyle = TableStyle([('FONT', (0, 0), (-1, 1), 'DejaVuSerif', 24)
    ])
    tbl = Table(data)
    tbl.setStyle(tblstyle)
    story.append(tbl)
    doc.build(story)
if __name__ == '__main__':
    pdfmetrics.registerFont(TTFont('DejaVuSerif', 'DejaVuSerif.ttf'))
    table_fonts()
make sure that the “DejaVuSerif.ttf" is in the folder next to the file. All the blessings

GOD BLESS YOU!!! Big Grin Cool Surfing
I tried for a couple of days without any resuts until I found your post.
I was trying to make(and in the end I managed to make)a software that can make a simple packaging label with ean13 barcode, title (translated in 4 languages with google translate), other specs and logo on it.

I had to make around 700 labels very quick because the truck was waiting at the warehouse, the products could be received because they camed without proper labels....so....being a graphic designer with few friends working in the coding area I started to make a python soft/script.

I managed to make the software but because I had bulgarian and maghiar languages, It was giving me black squares instead of letters...

So here YOU come and save my day and the truck drivers day! After staying at the warehouse waiting for my labels to land the products..

Real life story!! :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python script that deletes symbols in plain text nzcan 3 687 Sep-05-2023, 04:03 PM
Last Post: deanhystad
  [SOLVED] Looking for documentation on Reportlab's canvas.transform() function NeilUK 1 606 Aug-23-2023, 01:21 PM
Last Post: NeilUK
  Right to left alignment in python report using Reportlab jalal0034 1 1,824 Sep-27-2022, 04:25 AM
Last Post: jalal0034
Question Trouble installing modules/libraries and getting Notepad++ to show cyrillic letters Dragiev 6 2,241 Jul-24-2022, 12:55 PM
Last Post: Dragiev
  Label Maker FPDF, Reportlab jamesaarr 1 2,657 Aug-09-2021, 11:57 PM
Last Post: Pedroski55
  Using Reportlab to create a landscape pdf SmukasPlays 2 5,396 Aug-09-2020, 09:31 PM
Last Post: SmukasPlays
  Unexpected output: symbols for derivative not being displayed saucerdesigner 0 2,041 Jun-22-2020, 10:06 PM
Last Post: saucerdesigner
  Help! - How to create a Title for a Reportlab Table crabbylou 0 5,337 Mar-29-2020, 09:14 PM
Last Post: crabbylou
  ReportLab Polypop77 0 1,829 Mar-20-2020, 01:17 PM
Last Post: Polypop77
  Replacing symbols by " Tiihu 1 1,859 Feb-13-2020, 09:27 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020