2024-04-28 12:55:34 发布
网友
我有一个简单的python tkinter绘制程序(用户使用鼠标在画布上绘制)。我的目标是保存最终的图纸并将其与其他内容一起放入pdf文件中。
环顾四周,我意识到我只能将画布绘图保存为这样的postscript文件
postscript
canvas.postscript(file="file_name.ps", colormode='color')
所以,我想知道是否有任何方法(任何python模块?)这将允许我将postscript文件作为图像插入到pdf文件中。
有可能吗?
正如在this answer中所提到的,可能的演练是打开要使用ghostscript的子流程:
canvas.postscript(file="tmp.ps", colormode='color') process = subprocess.Popen(["ps2pdf", "tmp.ps", "result.pdf"], shell=True)
另一个解决方案是使用ReportLab,但是由于它的^{}不太可靠,我认为您必须使用Python Imaging Library将PS文件转换为图像,然后将其添加到ReportLabCanvas。不过,我还是建议你用鬼脚本的方法。
这是一个基本的概念证明,我过去常常看它是否有效:
""" 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()
正如在this answer中所提到的,可能的演练是打开要使用ghostscript的子流程:
另一个解决方案是使用ReportLab,但是由于它的^{} 不太可靠,我认为您必须使用Python Imaging Library将PS文件转换为图像,然后将其添加到ReportLabCanvas。不过,我还是建议你用鬼脚本的方法。
这是一个基本的概念证明,我过去常常看它是否有效:
相关问题 更多 >
编程相关推荐