UTF-8 support

17 views
Skip to first unread message

Delusional Insanity

unread,
Jan 15, 2023, 5:14:21 PMJan 15
to reportlab-users
I can't create a PDF from UTF-8 encoded text. I get a document full of black squares (see the attachment).

My code:

```python

import tempfile

from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter

from faker import Faker

FAKER = Faker(locale="hy-AM")
text = FAKER_HY.text(max_nb_chars=10_000)
text = text.replace("\n", " ")

pdf_name_reportlab = tempfile.NamedTemporaryFile(prefix="reportlab_", suffix=".pdf", dir="/tmp/tmp/").name

def generate_pdf_reportlab():
    styles = getSampleStyleSheet()
    style_paragraph = styles['Normal']
    story = []

    doc = SimpleDocTemplate(
        pdf_name_reportlab,
        pagesize=letter,
        bottomMargin=.4 * inch,
        topMargin=.6 * inch,
        rightMargin=.8 * inch,
        leftMargin=.8 * inch
    )

    paragraph = Paragraph(text, style_paragraph)
    story.append(paragraph)
    doc.build(story)

generate_pdf_reportlab()
```

I also tried TTF font (Vera) but it didn't work either.

```python

from reportlab.pdfbase import pdfmetrics

pdfmetrics.registerFont(TTFont('Vera', 'Vera.ttf'))
pdfmetrics.registerFont(TTFont('VeraBd', 'VeraBd.ttf'))
pdfmetrics.registerFont(TTFont('VeraIt', 'VeraIt.ttf'))
pdfmetrics.registerFont(TTFont('VeraBI', 'VeraBI.ttf'))

doc = SimpleDocTemplate(
    buffer,
    pagesize=letter,
    bottomMargin=.4 * inch,
    topMargin=.6 * inch,
    rightMargin=.8 * inch,
    leftMargin=.8 * inch,
    initialFontName="Vera"
)
```

Screenshot from 2023-01-15 23-12-02.png
Reply all
Reply to author
Forward
0 new messages