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

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

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to split a rinex file if I need 24 hours data

Trying to divide rinex file using the command gfzrnx but getting this error. While doing that getting this error msg 'gfzrnx' is not recognized as an internal or external command Trying to split rinex file using the command gfzrnx. also install'gfzrnx'. my doubt is I need to run this program in 'gfzrnx' or in 'cmdprompt'. I am expecting a rinex file with 24 hrs or 1 day data.I Have 48 hrs data in RINEX format. Please help me to solve this issue. source https://stackoverflow.com/questions/75385367/how-to-split-a-rinex-file-if-i-need-24-hours-data