Skip to main content

Issues faced when trying to render two PDF files

I am trying to code a program that will take in two files: a question paper and an answer paper. The two files should be rendered in the same window and have a vertical scroll bar that can be used.

Below is my code:

import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
from tkinter import *
from PIL import Image, ImageTk
import os
from pdf2image import convert_from_path

class PDFViewerApp:
    def __init__(self, root):
        self.root = root
        self.root.title("PDF Question Cropper")

        self.frame = ttk.Frame(self.root)
        self.frame.pack(fill="both", expand=True)

        self.question_file = None
        self.answer_file = None

        self.question_label = ttk.Label(self.frame, text="No question file selected.")
        self.question_label.pack()

        self.select_question_button = ttk.Button(self.frame, text="Select Question PDF", command=self.select_question_pdf)
        self.select_question_button.pack()

        self.answer_label = ttk.Label(self.frame, text="No answer file selected.")
        self.answer_label.pack()

        self.select_answer_button = ttk.Button(self.frame, text="Select Answer PDF", command=self.select_answer_pdf)
        self.select_answer_button.pack()

        self.confirm_button = ttk.Button(self.frame, text="Confirm and Process", command=self.process_pdfs)
        self.confirm_button.pack()

        self.new_window = None
        self.pdf_canvas1 = None
        self.pdf_canvas2 = None

    def select_question_pdf(self):
        self.question_file = filedialog.askopenfilename(title="Select Question PDF")
        if self.question_file:
            self.question_label.config(text=f"Question PDF: {os.path.basename(self.question_file)}")

    def select_answer_pdf(self):
        self.answer_file = filedialog.askopenfilename(title="Select Answer PDF")
        if self.answer_file:
            self.answer_label.config(text=f"Answer PDF: {os.path.basename(self.answer_file)}")

    def process_pdfs(self):
        if not self.question_file or not self.answer_file:
            return
        self.open_new_window()

    def open_new_window(self):
        self.new_window = tk.Toplevel(self.root)
        self.new_window.title("Question Cropper")

        self.pdf_viewer_frame = ttk.Frame(self.new_window)
        self.pdf_viewer_frame.pack(fill=tk.BOTH, expand=True)

        self.pdf_canvas1 = tk.Canvas(self.pdf_viewer_frame, xscrollincrement=1)
        self.pdf_canvas2 = tk.Canvas(self.pdf_viewer_frame, xscrollincrement=1)

        self.scrollbar1 = ttk.Scrollbar(self.pdf_viewer_frame, orient="vertical", command=self.pdf_canvas1.yview)
        self.scrollbar2 = ttk.Scrollbar(self.pdf_viewer_frame, orient="vertical", command=self.pdf_canvas2.yview)

        self.scrollbar1.pack(side="right", fill="y")
        self.scrollbar2.pack(side="right", fill="y")

        self.pdf_canvas1.config(yscrollcommand=self.scrollbar1.set)
        self.pdf_canvas2.config(yscrollcommand=self.scrollbar2.set)

        self.pdf_canvas1.pack(side="left", fill="both", expand=True)
        self.pdf_canvas2.pack(side="right", fill="both", expand=True)

        self.file_label = ttk.Label(self.new_window, text="Processing...")
        self.file_label.pack()

        self.display_pdfs()

    def display_pdfs(self):
        if self.question_file and self.answer_file:
            question_pages = convert_from_path(self.question_file)
            answer_pages = convert_from_path(self.answer_file)

            if question_pages and answer_pages:
                max_width = max(page.width for page in question_pages + answer_pages)
                max_height = max(page.height for page in question_pages + answer_pages)

                for page in range(min(len(question_pages), len(answer_pages))):
                    img1 = question_pages[page]
                    img2 = answer_pages[page]

                    img1_blank = Image.new('RGB', (max_width, max_height), (255, 255, 255))
                    img2_blank = Image.new('RGB', (max_width, max_height), (255, 255, 255))

                    img1_blank.paste(img1, (0, 0))
                    img2_blank.paste(img2, (0, 0))

                    photo1 = ImageTk.PhotoImage(master=self.new_window, image=img1_blank)
                    photo2 = ImageTk.PhotoImage(master=self.new_window, image=img2_blank)

                    canvas_width = max_width
                    canvas_height = max_height

                    self.pdf_canvas1.config(scrollregion=(0, 0, canvas_width, canvas_height), width=canvas_width, height=canvas_height)
                    self.pdf_canvas2.config(scrollregion=(0, 0, canvas_width, canvas_height), width=canvas_width, height=canvas_height)

                    self.pdf_canvas1.create_image(0, 0, image=photo1, anchor=tk.NW)
                    self.pdf_canvas2.create_image(0, 0, image=photo2, anchor=tk.NW)

                    self.pdf_canvas1.photo = photo1
                    self.pdf_canvas2.photo = photo2

                    self.file_label.config(text=f"Question File: {os.path.basename(self.question_file)}\nAnswer File: {os.path.basename(self.answer_file)} - Page {page + 1}")

if __name__ == "__main__":
    root = tk.Tk()
    app = PDFViewerApp(root)
    root.mainloop()

It will be great if anyone can help.

Currently, I am facing three main issues:

  1. The two files are not taking up the same amount of space in the window; the question paper, which is placed on the left, appears larger.

  2. The two scroll bars are squeezed to the right and not positioned at the relative right side of each displayed file.

  3. The two files cannot be fully rendered. In my testing with the current files, only the third page seems to be displayed.



source https://stackoverflow.com/questions/77353994/issues-faced-when-trying-to-render-two-pdf-files

Comments

Popular posts from this blog

Confusion between commands.Bot and discord.Client | Which one should I use?

Whenever you look at YouTube tutorials or code from this website there is a real variation. Some developers use client = discord.Client(intents=intents) while the others use bot = commands.Bot(command_prefix="something", intents=intents) . Now I know slightly about the difference but I get errors from different places from my code when I use either of them and its confusing. Especially since there has a few changes over the years in discord.py it is hard to find the real difference. I tried sticking to discord.Client then I found that there are more features in commands.Bot . Then I found errors when using commands.Bot . An example of this is: When I try to use commands.Bot client = commands.Bot(command_prefix=">",intents=intents) async def load(): for filename in os.listdir("./Cogs"): if filename.endswith(".py"): client.load_extension(f"Cogs.{filename[:-3]}") The above doesnt giveany response from my Cogs ...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...