如何将文本框保存为PDF文件
我正在尝试将文本框文本保存为PDF文件 我尝试了以下内容:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter.filedialog import askopenfile
from tkinter.filedialog import *
from fpdf import FPDF
def export(self):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=15)
pdf.cell(40, 10, self.agreemanttext.get("1.0", END).strip())
pdf.output(asksaveasfilename(filetypes=[("PDF file", "*.pdf")]), "F")
def agreemnttext(self):
self.buying_agreementsample_cash ="""text"""
self.agreemanttextframe = Frame(self.agreemantpage, width=900, height=900)
self.agreemanttextframe.place(x=1, y=40)
self.agreemanttext = Text(self.agreemanttextframe, width=124, height=57)
self.agreemanttext.pack()
self.agreemanttext.tag_configure('tag-center', justify='right')
self.agreemanttext.insert('end', self.buying_agreementsample_cash, 'tag-center')
self.agreemanttext.config(state='disabled')
self.exportbutton=Button(self.agreemantpage,text="export as pdf",command=self.export)
self.exportbutton.place(x=80,y=600)
但是我得到了一个错误: p = self.pages [n] .encode(“ latin1”)如果py3k erse self.pages [n] UnicodeCodeError:“ Latin-1”编解码器无法在位置50-54中编码字符:不在范围内(256)
I am trying to save a textbox text as a pdf file
I have tried the following:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter.filedialog import askopenfile
from tkinter.filedialog import *
from fpdf import FPDF
def export(self):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=15)
pdf.cell(40, 10, self.agreemanttext.get("1.0", END).strip())
pdf.output(asksaveasfilename(filetypes=[("PDF file", "*.pdf")]), "F")
def agreemnttext(self):
self.buying_agreementsample_cash ="""text"""
self.agreemanttextframe = Frame(self.agreemantpage, width=900, height=900)
self.agreemanttextframe.place(x=1, y=40)
self.agreemanttext = Text(self.agreemanttextframe, width=124, height=57)
self.agreemanttext.pack()
self.agreemanttext.tag_configure('tag-center', justify='right')
self.agreemanttext.insert('end', self.buying_agreementsample_cash, 'tag-center')
self.agreemanttext.config(state='disabled')
self.exportbutton=Button(self.agreemantpage,text="export as pdf",command=self.export)
self.exportbutton.place(x=80,y=600)
but I got this error:
p = self.pages[n].encode("latin1") if PY3K else self.pages[n]
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 50-54: ordinal not in range(256)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您阅读了PDF规范(ISO 32000),您会发现PDF期望在合规软件中始终存在14个字体。
These fonts are:
The characters you can display in给定的字体取决于字体本身。例如,Helvetica中没有阿拉伯字符。尝试使用Helvetica展示阿拉伯语将不起作用。
所有这些字体仅编码西方字符。这意味着它们仅编码给定的Unicode Codepoint范围。
这就是给您上述错误的原因。表格允许人们输入随机输入(包括非西方字符),这些输入无法再显示在标准14个字体中。
可能的解决方案:
If you read the PDF specification (ISO 32000), you will find that PDF expects 14 fonts to always be present in compliant software.
These fonts are:
The characters you can display in a given font depends on the font itself. For example, there are no Arabic characters in Helvetica. Trying to display Arabic using Helvetica will not work.
All of these fonts only encode western characters. Which means they encode only a given range of unicode codepoints.
This is what is giving you the aforementioned error. The forms allows people to enter random input (including non-western characters) which can no longer be displayed in the standard 14 fonts.
Possible solution(s):