Skip to content

Instantly share code, notes, and snippets.

@aleroddepaz
Last active February 9, 2024 21:02
Show Gist options
  • Save aleroddepaz/6089708 to your computer and use it in GitHub Desktop.
Save aleroddepaz/6089708 to your computer and use it in GitHub Desktop.
Proof of concept: How to export the content of a Tkinter Canvas to a PDF with ghostscript.
"""
Setup for Ghostscript:
Download it from http://www.ghostscript.com/ and
add `/path/to/gs9.07/bin/` and `/path/to/gs9.07/lib/` to your path.
"""
import os
import subprocess
import tkinter as tk
class App(object):
def __init__(self, root):
self.line_start = None
self.canvas = tk.Canvas(root, width=300, height=300, bg="white")
self.canvas.bind("<Button-1>", lambda e: self.draw(e.x, e.y))
self.button = tk.Button(root, 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")
if __name__ == '__main__':
root = tk.Tk()
root.title("Canvas2PDF")
app = App(root)
root.mainloop()
@ashvinmanoj
Copy link

I tried this code and received an error "'ps2pdf' is not recognized as an internal or external command, operable program or batch file."

I added the path variables and also installed ghostscript using pip. Still no success.

@aleroddepaz
Copy link
Author

Looks like the lib directory is not added to the path, run where ps2pdf on the command line to check it.

@thein-htay-aung
Copy link

I want to create with A5 size. Default size is A4. How can I do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment