diff --git a/README.md b/README.md index 0bb23a8..ec6769a 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,11 @@ The tkPDFViewer is python library, which allows you to embed the PDF file in you > with customizable width and height > worked with python 2.7+. +# Update by Constant206462 + + - New -dpi option + - You can zoom in / zoom out by pressing control button and scrolling the mouse wheel or pressing "+" and "-" on keyboard + ### Installation @@ -93,6 +98,7 @@ load --> after or before, Through this attribute you can decide that , when your - PyMuPDF - Thread - math + - Pillow License @@ -103,4 +109,4 @@ MIT # Author - [Roshan Paswan](https://github.com/Roshanpaswan/) - \ No newline at end of file + diff --git a/tkPDFViewer/tkPDFViewer.py b/tkPDFViewer/tkPDFViewer.py index 4ff98ef..64bc42c 100644 --- a/tkPDFViewer/tkPDFViewer.py +++ b/tkPDFViewer/tkPDFViewer.py @@ -1,65 +1,68 @@ try: - from tkinter import* + import tkinter as tk import fitz - from tkinter.ttk import Progressbar + from tkinter import ttk from threading import Thread import math + from PIL import Image, ImageTk + import platform except Exception as e: print(f"This error occured while importing neccesary modules or library {e}") class ShowPdf(): - img_object_li = [] + tkimg_object_li = [] - def pdf_view(self,master,width=1200,height=600,pdf_location="",bar=True,load="after"): + def pdf_view(self, master, width=1200, height=600, pdf_location="", bar=True, load="after", dpi=100): - self.frame = Frame(master,width= width,height= height,bg="white") + self.frame = tk.Frame(master, width=width, height=height, bg="white") - scroll_y = Scrollbar(self.frame,orient="vertical") - scroll_x = Scrollbar(self.frame,orient="horizontal") + scroll_y = ttk.Scrollbar(self.frame, orient="vertical") + scroll_x = ttk.Scrollbar(self.frame, orient="horizontal") - scroll_x.pack(fill="x",side="bottom") - scroll_y.pack(fill="y",side="right") + scroll_x.pack(fill="x", side="bottom") + scroll_y.pack(fill="y", side="right") percentage_view = 0 - percentage_load = StringVar() + percentage_load = tk.StringVar() if bar==True and load=="after": - self.display_msg = Label(textvariable=percentage_load) + self.display_msg = ttk.Label(textvariable=percentage_load) self.display_msg.pack(pady=10) - loading = Progressbar(self.frame,orient= HORIZONTAL,length=100,mode='determinate') - loading.pack(side = TOP,fill=X) + loading = ttk.Progressbar(self.frame, orient=tk.HORIZONTAL, length=100, mode='determinate') + loading.pack(side=tk.TOP, fill=tk.X) - self.text = Text(self.frame,yscrollcommand=scroll_y.set,xscrollcommand= scroll_x.set,width= width,height= height) - self.text.pack(side="left") + self.text = tk.Text(self.frame, yscrollcommand=scroll_y.set, xscrollcommand=scroll_x.set, width=width, height=height) + self.text.pack(fill="x") scroll_x.config(command=self.text.xview) scroll_y.config(command=self.text.yview) - def add_img(): precentage_dicide = 0 open_pdf = fitz.open(pdf_location) for page in open_pdf: - pix = page.getPixmap() - pix1 = fitz.Pixmap(pix,0) if pix.alpha else pix - img = pix1.getImageData("ppm") - timg = PhotoImage(data = img) - self.img_object_li.append(timg) + pix = page.get_pixmap(dpi=dpi) + mode = "RGBA" if pix.alpha else "RGB" + img = Image.frombytes(mode, [pix.width, pix.height], pix.samples) + self.img_object_li.append(img) + self.tkimg_object_li.append(ImageTk.PhotoImage(img)) if bar==True and load=="after": precentage_dicide = precentage_dicide + 1 percentage_view = (float(precentage_dicide)/float(len(open_pdf))*float(100)) loading['value'] = percentage_view percentage_load.set(f"Please wait!, your pdf is loading {int(math.floor(percentage_view))}%") + self.orig_size = self.tkimg_object_li[0].width() + if bar==True and load=="after": loading.pack_forget() self.display_msg.pack_forget() - for i in self.img_object_li: - self.text.image_create(END,image=i) - self.text.insert(END,"\n\n") + for im in self.tkimg_object_li: + self.text.image_create(tk.END, image=im) + self.text.insert(tk.END, "\n") self.text.configure(state="disabled") def start_pack(): @@ -67,21 +70,57 @@ def start_pack(): t1.start() if load=="after": - master.after(250,start_pack) + master.after(250, start_pack) else: start_pack() + + def zoom_in(event=None): + self.text.configure(state="normal") + self.text.delete(1.0, tk.END) + scroll_x_state = scroll_x.get() + scroll_y_state = scroll_y.get() + res = 1 + 0.1*self.orig_size/self.tkimg_object_li[0].width() + for i, im in enumerate(self.img_object_li): + self.tkimg_object_li[i] = ImageTk.PhotoImage(im.resize((int(res*self.tkimg_object_li[i].width()), int(res*self.tkimg_object_li[i].height())), Image.ANTIALIAS)) + self.text.image_create(tk.END, image=self.tkimg_object_li[i]) + self.text.insert(tk.END, "\n") + self.text.update() + self.text.xview_moveto(scroll_x_state[0]) + self.text.yview_moveto(scroll_y_state[0]) + self.text.update() + self.text.configure(state="disabled") + + def zoom_out(event=None): + self.text.configure(state="normal") + self.text.delete(1.0, tk.END) + scroll_x_state = scroll_x.get() + scroll_y_state = scroll_y.get() + res = 1 - 0.1*self.orig_size/self.tkimg_object_li[0].width() + for i, im in enumerate(self.img_object_li): + self.tkimg_object_li[i] = ImageTk.PhotoImage(im.resize((int(res*self.tkimg_object_li[i].width()), int(res*self.tkimg_object_li[i].height())), Image.ANTIALIAS)) + self.text.image_create(tk.END, image=self.tkimg_object_li[i]) + self.text.insert(tk.END, "\n") + self.text.update() + self.text.xview_moveto(scroll_x_state[0]) + self.text.yview_moveto(scroll_y_state[0]) + self.text.update() + self.text.configure(state="disabled") + def zooming(event=None): + if event.delta > 0: + zoom_in(event) + else: + zoom_out(event) + + if platform.system() == 'Windows' or platform.system() == 'Darwin': + self.text.bind('', zoom_in) + self.text.bind('', zoom_out) + self.text.bind('', zooming) + elif platform.system() == 'Linux': + # cant bind neither control + nor control - events on linux for some reason + self.text.bind('', zoom_in) + self.text.bind('', zoom_out) + else: + pass + return self.frame - - - - -def main(): - root = Tk() - root.geometry("700x780") - d = ShowPdf().pdf_view(root,pdf_location=r"D:\DELL\Documents\Encyclopedia GUI.pdf",width=50,height=200) - d.pack() - root.mainloop() - -if __name__ == '__main__': - main()