[Python] Character encoding error when writing url in FPDF2

Discussão em 'Python' iniciado por Stack, Fevereiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working in Python 3.7 and have the python port of FPDF 2.0.3 (https://pypi.org/project/fpdf2/) running. I'm working with a lot of unicode symbols, and some of them need to be a part of the URL. I can write them as text without problem, but I keep getting error messages when a unicode symbol becomes a part of my URL.

    Tried to escape with html.escape(str), this didn't work Rewritten the code to use write_html(html_as_str), this didn't work either

    this is working code:

    add = "Chronique d‘Égypte (CdE)" ##this is actually pulled in from a MYSQL query using PYMYSQL
    from fpdf import FPDF
    pdf = FPDF('P', 'mm', 'A4')
    pdf.add_page()
    pdf.add_font('base', '', r'C:\Fonts\DejaVuSans.ttf', uni=True)
    pdf.set_font('base', '',12)
    pdf.write(5, add)
    pdf.output("goodfile.pdf", "F")



    With this script I get the string printed. However I want the string to be printed and be a part of a url like this:

    add = "Chronique d‘Égypte (CdE)" ##this is actually pulled in from a MYSQL query using PYMYSQL
    from fpdf import FPDF
    pdf = FPDF('P', 'mm', 'A4')
    pdf.add_page()
    pdf.add_font('base', '', r'C:\Fonts\DejaVuSans.ttf', uni=True)
    pdf.set_font('base', '',12)
    pdf.write(5, add, 'https://www.example.org/index.php?searchterm='+add)
    pdf.output("goodfile.pdf", "F")



    This second script fails with the following error message in my console:

    Traceback (most recent call last):
    File ".\print.py", line 543, in <module>
    pdf.output(Filename+'_CORPUS.pdf', 'F')
    File "C:\Users\me\AppData\Local\Programs\Python\Python37\lib\site-packages\fpdf\fpdf.py", line 1239, in output
    buffer = self.buffer.encode("latin1")
    UnicodeEncodeError: 'latin-1' codec can't encode character '\u2018' in position 1587960: ordinal not in range(256)



    The error message itself is very confusing as the part that adds the string that causes the error happens around line 470; whereas the traceback only mentions line 543.

    I expect the output to be a clickable link in my pdf, opening the default browser and going to the specified URL with the characters as they are in the PDF. I can't replace this character by a normal quote, as it give me other (in this case none) results on that site.

    Also, could someone add a tag FPDF2 to help categorize this correctly?

    Continue reading...

Compartilhe esta Página