Python tkinter将画布另存为postscript并添加到pdf
我有一个简单的python tkinter绘制程序(用户使用鼠标在画布上绘制)。我的目标是保存最终图纸,并将其与其他内容一起放入pdf文件中 环顾四周后,我意识到我只能像这样将画布图形保存为Python tkinter将画布另存为postscript并添加到pdf,python,pdf,canvas,tkinter,postscript,Python,Pdf,Canvas,Tkinter,Postscript,我有一个简单的python tkinter绘制程序(用户使用鼠标在画布上绘制)。我的目标是保存最终图纸,并将其与其他内容一起放入pdf文件中 环顾四周后,我意识到我只能像这样将画布图形保存为postscript文件 canvas.postscript(file=“file\u name.ps”,colormode='color') 因此,我想知道是否有任何方法(任何python模块?)允许我将postscript文件作为图像插入pdf文件 可能吗?如中所述,可能的演练是打开子流程以使用: 另一个
postscript
文件
canvas.postscript(file=“file\u name.ps”,colormode='color')
因此,我想知道是否有任何方法(任何python模块?)允许我将postscript文件作为图像插入pdf文件
可能吗?如中所述,可能的演练是打开子流程以使用:
另一个解决方案是使用,但由于其不太可靠,我认为您必须首先使用将PS文件转换为图像,然后将其添加到ReportLab。然而,我建议使用幽灵脚本方法
这是一个基本的概念证明,我用来看看它是否有效:
"""
Setup for Ghostscript 9.07:
Download it from http://www.ghostscript.com/GPL_Ghostscript_9.07.html
and add `/path/to/gs9.07/bin/` and `/path/to/gs9.07/lib/` to your path.
"""
import Tkinter as tk
import subprocess
import os
class 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()
“”“
Ghostscript 9.07的设置:
从下载http://www.ghostscript.com/GPL_Ghostscript_9.07.html
并将“/path/to/gs9.07/bin/”和“/path/to/gs9.07/lib/”添加到您的路径中。
"""
将Tkinter作为tk导入
导入子流程
导入操作系统
类应用程序(tk.tk):
定义初始化(自):
tk.tk.\uuuuu初始化(self)
self.title(“Canvas2PDF”)
self.line\u start=无
self.canvas=tk.canvas(self,宽度=300,高度=300,bg=“白色”)
self.canvas.bind(“,lambda e:self.draw(e.x,e.y))
self.button=tk.button(self,text=“生成PDF”,
command=self.generate\u(pdf)
self.canvas.pack()
自身按钮包(pady=10)
def牵引(自、x、y):
如果self.line\u启动:
x_原点,y_原点=self.line_起点
self.canvas.create_线(x_原点,y_原点,x,y)
self.line\u start=无
其他:
self.line_start=(x,y)
def生成pdf(自我):
self.canvas.postscript(file=“tmp.ps”,colormode='color')
process=subprocess.Popen([“ps2pdf”、“tmp.ps”、“result.pdf”],shell=True)
process.wait()
删除操作系统(“tmp.ps”)
自我毁灭
app=app()
app.mainloop()
关于可以实现这一点的模块的信息,请参阅问题。祝你好运不过,这是可行的,在生成postscript之前,我必须在画布上调用update()方法,否则postscript将生成1x1图像。
"""
Setup for Ghostscript 9.07:
Download it from http://www.ghostscript.com/GPL_Ghostscript_9.07.html
and add `/path/to/gs9.07/bin/` and `/path/to/gs9.07/lib/` to your path.
"""
import Tkinter as tk
import subprocess
import os
class 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()