Last active
February 9, 2024 21:02
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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() |
Looks like the lib
directory is not added to the path, run where ps2pdf
on the command line to check it.
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
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.