如何将文本框保存为PDF文件

发布于 2025-02-04 03:51:52 字数 1251 浏览 3 评论 0原文

我正在尝试将文本框文本保存为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高速公鹿 2025-02-11 03:51:52

如果您阅读了PDF规范(ISO 32000),您会发现PDF期望在合规软件中始终存在14个字体。

These fonts are:

  • Courier
  • Courier-Bold
  • Courier-Bold-Oblique
  • Courier-Oblique
  • Helvetica
  • Helvetica-Bold
  • Helvetica-Bold-Oblique
  • Helvetica-Oblique
  • Symbol
  • Times-Bold
  • Times-Bold-Italic
  • Times-Italic
  • Times-Roman
  • ZapfDingbats

The characters you can display in给定的字体取决于字体本身。例如,Helvetica中没有阿拉伯字符。尝试使用Helvetica展示阿拉伯语将不起作用。

所有这些字体仅编码西方字符。这意味着它们仅编码给定的Unicode Codepoint范围。

这就是给您上述错误的原因。表格允许人们输入随机输入(包括非西方字符),这些输入无法再显示在标准14个字体中。

可能的解决方案:

  • 扔掉任何您无法
  • 用智能方式在字体上决定的内容
  • ,请使用Google noto字体之一确保它可以编码用户投掷的任何内容(noto代表“ no tofu”,书呆子讲话:“每当某种东西都试图代表一个超出范围的角色时,您会看到的那些盒子”)

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:

  • Courier
  • Courier-Bold
  • Courier-Bold-Oblique
  • Courier-Oblique
  • Helvetica
  • Helvetica-Bold
  • Helvetica-Bold-Oblique
  • Helvetica-Oblique
  • Symbol
  • Times-Bold
  • Times-Bold-Italic
  • Times-Italic
  • Times-Roman
  • ZapfDingbats

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):

  • throw away anything you can not represent
  • decide on a font in an intelligent way
  • use one of the google Noto fonts to ensure it can encode whatever the user throws at it (Noto stands for "No Tofu", which is nerd-talk for "those boxes you see whenever something is trying to represent a character that is out of range")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文