首页 > 解决方案 > 将 tkinter Canvas 导出为 PDF

问题描述

我有一个想要存储为 PDF 的画布。我正在通过以下方式创建画布:

from tkinter import *
root = Tk()
root.geometry('1920x1200')
canvas = Canvas(master=root, width=1920, height=1200, bg='white')
canvas.place(x=0, y=0)
canvas.pack(expand=YES, fill=BOTH)
image = ImageTk.PhotoImage(file=picture_path)
canvas.create_image(0, 0, image=image, anchor=NW)
canvas.bind("<Button-1>", getorigin)
canvas.after(10000, add_line, canvas)
root.mainloop()

使用 create_image 我只是为画布设置背景图像。add_line 方法在画布(create_line)中绘制一些东西。绘图完成后,我尝试通过以下方式创建 PDF:

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

PDF 已成功创建,但仅捕获了画布的一部分。看起来画布存储在 1200x1920 而不是 1920x1200 中。

我该如何解决?

标签: pythoncanvastkintertkinter-canvas

解决方案


推荐阅读