I want to append the contents of a Tkinter widget to the end of an existing pdf.
First, I capture the widget to an PIL image. Then, it seems it is required when using PyPDF2 to create an intermediate temporary file from the image, which feels unnecessary. Instead I would like to append the image directly, or at least convert the image to something that can be appended without the need to be written to a file.
In the code snippet below I save the image to a temporary pdf, then open the pdf and append. This works, but is not a very elegant solution.
import tkinter as tk
from PIL import ImageGrab
import os
import PyPDF2
def process_pdf(widget, pdf_filepath):
""""Append Tkinter widget to pdf"""
# capture the widget
img = capture_widget(widget)
# create pdf merger object
merger = PyPDF2.PdfMerger()
# creating a pdf file object of original pdf and add to output
pdf_org = open(pdf_filepath, 'rb')
merger.append(pdf_org)
# NOTE I want to get rid of this step
# create temporary file, read it, append it to output, delete it.
temp_filename = pdf_filepath[:-4] + "_temp.pdf"
img.save(temp_filename)
pdf_temp = open(temp_filename, 'rb')
merger.append(pdf_temp)
pdf_temp.close()
# write
outputfile = open(pdf_filepath, "wb")
merger.write(outputfile)
# clean up
pdf_org.close()
outputfile.close()
os.remove(temp_filename)
def capture_widget(widget):
"""Take screenshot of the passed widget"""
x0 = widget.winfo_rootx()
y0 = widget.winfo_rooty()
x1 = x0 + widget.winfo_width()
y1 = y0 + widget.winfo_height()
img = ImageGrab.grab((x0, y0, x1, y1))
return img
Does someone have a more elegant solution not requiring an intermediate file while retaining the flexibility PyPDF2 provides?
Thanks.