pyfpdf icon indicating copy to clipboard operation
pyfpdf copied to clipboard

How to print Trademark symbol ?

Open shayan-NECX opened this issue 1 year ago • 2 comments

Hi, thanks for the great work on this library. I have to write text to file which contains Trademark symbol. But the library errors out saying:

UnicodeEncodeError: 'latin-1' codec can't encode character '\u2122' in position 0: ordinal not in range(256)

Can you help ? How to print the trademark symbol ?

shayan-NECX avatar Jan 16 '24 18:01 shayan-NECX

Hi @shayan-NECX

Note that PyFPDF is not maintained anymore. See #207. fpdf2 is its successor: https://pypi.org/project/fpdf2/

You simply need to use a font that can render this unicode character. There is some minimal Python code that demonstrate how to do it with fpdf2:

from fpdf import FPDF

pdf = FPDF()
pdf.set_font("Helvetica", size=24)
pdf.add_page()
pdf.cell(text="©")
pdf.output("issue_216.pdf")

If you need more help, please open an issue on https://github.com/PyFPDF/fpdf2 with some minimal code reproducing your example

Lucas-C avatar Jan 16 '24 22:01 Lucas-C

Thanks a lot for the quick response Lucas. I basically want this TM sign to display on PDF

from fpdf import FPDF

pdf = FPDF() pdf.set_font("Arial", size=24) pdf.add_page() pdf.cell(0,0,"™") pdf.output("issue_216.pdf")

But i keep getting error:

File "C:\Users\shaya\anaconda3\envs\mymedcard-desktop\lib\site-packages\fpdf\fpdf.py", line 1170, in _putpages p = self.pages[n].encode("latin1") if PY3K else self.pages[n] UnicodeEncodeError: 'latin-1' codec can't encode character '\u2122' in position 50: ordinal not in range(256)

I will create this in fpdf2 .. i guess

shayan-NECX avatar Jan 16 '24 22:01 shayan-NECX