将字体从C:\Windows\Fonts目录下拷贝到了fpdf文件夹。如果打印的字是'你好世界'时不会报错。但是换为其他字时就报cmap value too big/small import fpdf pdf =fpdf.FPDF() pdf.add_font('STSONG','',r'D:\test\venv\Lib\site-packages\fpdf\STSONG.TTF',True) #此句话添加字体标准宋体 pdf.add_page() pdf.set_font('STSONG', '', 35)#设置字体为标准宋体,size为35 pdf.cell(100, 100,'跟我说谢谢你') pdf.output('helloword_01.pdf')
报错:
D:\test\venv\Scripts\python.exe D:/test/pdf操作/fpdftest.py
D:\test\venv\lib\site-packages\fpdf\ttfonts.py:670: UserWarning: cmap value too big/small: -35825
warnings.warn("cmap value too big/small: %s" % cm)
D:\test\venv\lib\site-packages\fpdf\ttfonts.py:670: UserWarning: cmap value too big/small: -35869
warnings.warn("cmap value too big/small: %s" % cm)
D:\test\venv\lib\site-packages\fpdf\ttfonts.py:670: UserWarning: cmap value too big/small: -36318
warnings.warn("cmap value too big/small: %s" % cm)
Traceback (most recent call last):
File "D:\test\pdf操作\fpdftest.py", line 12, in <module>
pdf.output('helloword_01.pdf')
File "D:\test\venv\lib\site-packages\fpdf\fpdf.py", line 1079, in output
f=open(name,'wb')
PermissionError: [Errno 13] Permission denied: 'helloword_01.pdf'
Process finished with exit code 1
点击报错中的ttfonts.py,将else及其后边几句注释掉后,竟然不报错了,并且能正常生成中文。
cmap.append(0) # Mapping for last character cmapstr = b('') for cm in cmap: if cm >= 0: cmapstr += pack(">H", cm) # else: # try: # cmapstr += pack(">h", cm) # except: # warnings.warn("cmap value too big/small: %s" % cm) # cmapstr += pack(">H", -cm)