Exporting tkinter Canvas as PDF

Viewed 1583

I have a Canvas which I would like to store as PDF. I'm creating the canvas in the following way:

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

With create_image I'm just setting a background image to the canvas. The add_line method is drawing some stuff in the canvas (create_line). After the drawing is finished I try to create a PDF in the following way:

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

The PDF gets created successfully but only part of the canvas is captured. It looks like that the canvas is stored in 1200x1920 instead of 1920x1200.

How can I fix it?

0 Answers
Related