admin管理员组

文章数量:1612065

UnicodeEncodeError: 'latin-1' codec can't encode characters in position: ordinal not in range(256)

目录

UnicodeEncodeError: 'latin-1' codec can't encode characters in position: ordinal not in range(256)

#问题

#解决

#完整错误


#问题

没有设置字体;

注意,如果是商业使用,注意在pdf中使用可以商用免费的并且可以嵌入pdf的字体;

以思源字体为例;

pdf.add_font('siyuan','',r"C:\Users\usr\AppData\Local\Microsoft\Windows\Fonts\SourceHanSansSC-VF.ttf",True)
pdf.set_font('siyuan', '', 100)

from fpdf import FPDF

class PDF(FPDF):
    def header(self):
        # Logo
        #self.image('logo_pb.png', 10, 8, 33)
        # Arial bold 15
        self.set_font('Arial', 'B', 15)
        # Move to the right
        self.cell(80)
        # Title
        self.cell(30, 10, '测试', 1, 0, 'C')
        # Line break
        self.ln(20)

    # Page footer
    def footer(self):
        # Position at 1.5 cm from bottom
        self.set_y(-15)
        # Arial italic 8
        self.set_font('Arial', 'I', 8)
        # Page number
        self.cell(0, 10, 'Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C')

# Instantiation of inherited class
pdf = PDF()
pdf.alias_nb_pages()
pdf.add_page()
#pdf.set_font('Arial', '', 12)
for i in range(1, 41):
    pdf.cell(0, 10, 'Printing line number ' + str(i), 0, 1)
pdf.output('tuto2.pdf', 'F')

#解决

设置字体,能够匹配汉子的字体库

pdf.set_font('Arial', '', 12)

from fpdf import FPDF

class PDF(FPDF):
    def header(self):
        # Logo
        #self.image('logo_pb.png', 10, 8, 33)
        # Arial bold 15
        self.set_font('Arial', 'B', 15)
        # Move to the right
        self.cell(80)
        # Title
        self.cell(30, 10, '测试', 1, 0, 'C')
        # Line break
        self.ln(20)

    # Page footer
    def footer(self):
        # Position at 1.5 cm from bottom
        self.set_y(-15)
        # Arial italic 8
        self.set_font('Arial', 'I', 8)
        # Page number
        self.cell(0, 10, 'Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C')

# Instantiation of inherited class
pdf = PDF()
pdf.alias_nb_pages()
pdf.add_page()
pdf.set_font('Arial', '', 12)
for i in range(1, 41):
    pdf.cell(0, 10, 'Printing line number ' + str(i), 0, 1)
pdf.output('tuto2.pdf', 'F')

#完整错误

---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-8-acbb1446b69a> in <module>
     30 for i in range(1, 41):
     31     pdf.cell(0, 10, '测试' + str(i), 0, 1)
---> 32 pdf.output('tuto2.pdf', 'F')

D:\anaconda\lib\site-packages\fpdf\fpdf.py in output(self, name, dest)
   1063         #Finish document if necessary
   1064         if(self.state<3):
-> 1065             self.close()
   1066         dest=dest.upper()
   1067         if(dest==''):

D:\anaconda\lib\site-packages\fpdf\fpdf.py in close(self)
    244         self._endpage()
    245         #close document
--> 246         self._enddoc()
    247 
    248     def add_page(self, orientation=''):

D:\anaconda\lib\site-packages\fpdf\fpdf.py in _enddoc(self)
   1634     def _enddoc(self):
   1635         self._putheader()
-> 1636         self._putpages()
   1637         self._putresources()
   1638         #Info

D:\anaconda\lib\site-packages\fpdf\fpdf.py in _putpages(self)
   1168             if selfpress:
   1169                 # manage binary data as latin1 until PEP461 or similar is implemented
-> 1170                 p = self.pages[n].encode("latin1") if PY3K else self.pages[n]
   1171                 p = zlibpress(p)
   1172             else:

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 134-135: ordinal not in range(256)

本文标签: CodecencodeUnicodeEncodeErrorlatincharacters