-2

I have a canvas object in Tkinter, and I would like to export that canvas as a PDF file. I hear ghostscript can be used, but I don't know how to do so. Can someone please give an example of usage?

EDIT I've looked into the example below, but I get the error:

'ps2pdf' is not recognized as an internal or external command,operable program or batch file.

The code i used to test is:

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()
  • 1
    This question has been [asked before](https://stackoverflow.com/questions/17877495/python-tkinter-save-canvas-as-postscript-and-add-to-pdf). – Roland Smith Jan 13 '19 at 21:13
  • @RolandSmith I've tried that, but it says ps2pdf is not recognized – Osasenaga Emokpae Jan 13 '19 at 21:27
  • have you installed ps2pdf on your system? – Bryan Oakley Jan 13 '19 at 22:17
  • @BryanOakley how would I do that. I thought it was included in the ghostscript? – Osasenaga Emokpae Jan 13 '19 at 22:30
  • 1
    @OsasenagaEmokpae Note that if you are using ms-windows you should use `ps2pdf.bat` instead of `ps2pdf`. And you might need to call that with its full path in case its location isn't in your `$PATH`. Or you can just call ghostscript with the right parameters directly instead. – Roland Smith Jan 14 '19 at 00:11
  • @RolandSmith that's the problem, im not sure what parameters to place inside ghostscript, and there hasn't been much information online. – Osasenaga Emokpae Jan 14 '19 at 11:45
  • Ghostscript comes with its own documentation in ghostscript/doc, you can also find it at ghostscript.com. The PDF output device is called pdfwrite so you would do -sDEVICE=pdfwrite. Then you need to give it an output file, so -sOutputFile=/directory/file.pdf. Note that the Windows veraion of Ghostscript comes in 4 'flavours'; 32 or 64 bit, and window or command line version. So gswin32c, gswin64c, gswin32 or gswin64. – KenS Jan 14 '19 at 16:00

1 Answers1

0

You can use ghostscript directly.

My suggestion would be to use something like this:

args = ['gs', '-q', '-P-', '-dSAFER', '-dNOPAUSE', '-dBATCH',
        '-sDEVICE=pdfwrite', '-sOutputFile=result.pdf', '-c',
        '.setpdfwrite', '-f', 'tmp.ps']
process = subprocess.Popen(args)

Note that I'm not using shell=True here on purpose; it is not necessary in this case.

The arguments are based on reading the scripts that come with ghostscript, and the documentation.

On ms-windows you might want to use gs.exe instead of just gs. Additionally you might have to use the full path to the ghostscript executable too, depending on how ghostscript was installed.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94