I want to add some text strings to PDFs using reportlab
.
The strings look like this: 663-9999
.
I’m looping over a list of users, where each user['joinCode']
is the string described above.
The actual code is:
for user in user_join_codes:
print(f"Creating flyer for user {user['firstName']}")
output_pdf = PdfWriter()
qr_image = make_qr_code_image(user['joinCode'])
qr_pdf = image_to_pdf(qr_image)
for page_num in range(len(flyer_empty.pages)):
page = flyer_empty.pages[page_num]
if page_num == 1:
code = user['joinCode']
# RELEVANT code block for adding text
packet = BytesIO()
can = canvas.Canvas(packet, pagesize=(595, 421))
can.setFillColorRGB(14/256, 36/256, 55/256)
can.setFont("Helvetica", 10)
can.drawString(507, 210, code)
can.save()
packet.seek(0)
text_pdf = PdfReader(packet)
page.merge_page(text_pdf.pages[0])
# END BLOCK FOR ADDING TEXT
page.merge_transformed_page(
qr_pdf.pages[0], ctm=scale_and_translate)
output_pdf.add_page(page)
The result is:
663-
is always rendered correctly, the other digits are always garbled.
- Removing
663-
from the string still renders the other digits like this - Removing the last digits and keeping
663-
renders663-
correctly - Adding a string to the end of the code renders like in the screenshot followed by the new string e.g.
663-{garbled} foo
- Selecting and copying the garbled numbers from the PDF shows it’s
3670605810382647614253575417292383967641815316908283763829597428782587468021373564105610263482174716208958528373575306731259504502702539247632493843278497248058314298480108701980652914868025692567809415493570945694238969316393846459416740923416281376970508643461386369314240739483819462595736730797362575805701839621891891730529808108534358697316873269848203864868967491626858676913462017584909895258406141952753195898169609373659157274296
(or a similar sequence of numbers) - using
drawString
on any other random string e.g.'hello world'
works without a problem - The
type(user['joinCode'])
is<class 'str'>
print(code)
prints the correct string e.g.663-9999
in the terminal
How can I make sure those digits show up correctly?