栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python Tkinter将画布另存为Postscript并添加到pdf

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python Tkinter将画布另存为Postscript并添加到pdf

正如在此答案中提到的,可能的解决方法是打开一个子进程以使用ghostscript:

canvas.postscript(file="tmp.ps", colormode='color')process = subprocess.Popen(["ps2pdf", "tmp.ps", "result.pdf"], shell=True)

另一个解决方案是使用ReportLab,但是由于它

addPostscriptCommand
不是很可靠,我认为您必须使用Python
Imaging
Library

将PS文件首先转换为图像,然后再将其添加到ReportLab
Canvas中。但是,我建议使用ghostscript方法。

这是我用来查看是否有效的基本概念证明:

"""Setup for Ghostscript 9.07:Download it from http://www.ghostscript.com/GPL_Ghostscript_9.07.htmland add `/path/to/gs9.07/bin/` and `/path/to/gs9.07/lib/` to your path."""import Tkinter as tkimport subprocessimport osclass App(tk.Tk):    def __init__(self):        tk.Tk.__init__(self)        self.title("Canvas2PDF")        self.line_start = None        self.canvas = tk.Canvas(self, width=300, height=300, bg="white")        self.canvas.bind("<Button-1>", lambda e: self.draw(e.x, e.y))        self.button = tk.Button(self, text="Generate PDF",          command=self.generate_pdf)        self.canvas.pack()        self.button.pack(pady=10)    def draw(self, x, y):        if self.line_start: x_origin, y_origin = self.line_start self.canvas.create_line(x_origin, y_origin, x, y) self.line_start = None        else: self.line_start = (x, y)    def generate_pdf(self):        self.canvas.postscript(file="tmp.ps", colormode='color')        process = subprocess.Popen(["ps2pdf", "tmp.ps", "result.pdf"], shell=True)        process.wait()        os.remove("tmp.ps")        self.destroy()app = App()app.mainloop()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483896.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号