-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (39 loc) · 1.54 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import fitz # PyMuPDF
from reportlab.lib.pagesizes import A0
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw() # Hide the main tkinter window
# Open file dialog for selecting the input PDF
input_pdf_path = filedialog.askopenfilename(
title="Select input PDF file", filetypes=[("PDF files", "*.pdf")]
)
# Open file dialog for selecting the output PDF location
output_pdf_path = filedialog.asksaveasfilename(
title="Save output PDF as",
defaultextension=".pdf",
filetypes=[("PDF files", "*.pdf")],
)
# Open the input PDF
input_pdf = fitz.open(input_pdf_path)
# Create a new PDF for the output
output_pdf = fitz.open()
# Parameters for A2 size (seems to be a mistake in the comment, corrected here)
a4_width, a4_height = A0
for page_num in range(len(input_pdf)):
page = input_pdf[page_num]
rect = page.rect
y_offset = 0
while y_offset < rect.height:
# Create a new blank page with A4 size
new_page = output_pdf.new_page(width=a4_width, height=a4_height)
# Define the visible area of the long page to copy
visible_area = fitz.Rect(
0, y_offset, rect.width, min(y_offset + a4_height, rect.height)
)
# Copy the visible area from the long page to the new A4 page
# Copy the visible area from the long page to the new A4 page
new_page.show_pdf_page(new_page.rect, input_pdf, page_num, clip=visible_area)
# Increment y_offset by the height of an A4 page
y_offset += a4_height
output_pdf.save(output_pdf_path)