def applyzigzagimposition(self, inputpdfpath, outputpdfpath): """Applies the cut and…
- User
def apply_zigzag_imposition(self, input_pdf_path, output_pdf_path): """Applies the cut and stack imposition method to arrange pages. When the input PDF has exactly 2 pages, it arranges the front and back sides accordingly, reversing the columns on the back side while maintaining cut and stack numbering."""
# Fetch parameters from the user interface new_page_width = self.new_page_width_var.get() new_page_height = self.new_page_height_var.get() margin_top = self.margin_top_var.get() margin_bottom = self.margin_bottom_var.get() margin_left = self.margin_left_var.get() margin_right = self.margin_right_var.get() cropmark_offset = self.cropmark_offset_var.get() cropmark_length = self.cropmark_length_var.get() # Convert units from mm to points new_page_width_pt = self.mm_to_points(new_page_width) new_page_height_pt = self.mm_to_points(new_page_height) margin_top_pt = self.mm_to_points(margin_top) margin_bottom_pt = self.mm_to_points(margin_bottom) margin_left_pt = self.mm_to_points(margin_left) margin_right_pt = self.mm_to_points(margin_right) cropmark_offset_pt = self.mm_to_points(cropmark_offset) crop_length_pt = self.mm_to_points(cropmark_length) crop_thickness = 0.5 # Độ dày của crop marks # Tính toán khu vực có thể sử dụng available_width = new_page_width_pt - margin_left_pt - margin_right_pt available_height = new_page_height_pt - margin_top_pt - margin_bottom_pt # Đọc tệp PDF đầu vào doc = fitz.open(input_pdf_path) total_pages = len(doc) # Lấy kích thước trang đầu vào input_page = doc.load_page(0) bbox = input_page.rect entry_width_orig = bbox.width entry_height_orig = bbox.height # Các tùy chọn xoay và không xoay options = [ {'rotate': False, 'entry_width': entry_width_orig, 'entry_height': entry_height_orig}, {'rotate': True, 'entry_width': entry_height_orig, 'entry_height': entry_width_orig}, ] # Xác định cách sắp xếp tốt nhất best_arrangement = None max_total_entries = 0 for option in options: entries_per_row = int(available_width // option['entry_width']) entries_per_column = int(available_height // option['entry_height']) total_entries = entries_per_row * entries_per_column if total_entries > max_total_entries: max_total_entries = total_entries best_arrangement = option.copy() best_arrangement['entries_per_row'] = entries_per_row best_arrangement['entries_per_column'] = entries_per_column if not best_arrangement or best_arrangement['entries_per_row'] == 0 or best_arrangement['entries_per_column'] == 0: messagebox.showerror("Error", "Kích thước trang hoặc lề quá nhỏ để sắp xếp nội dung.") doc.close() return # Tính toán tổng kích thước lưới để căn giữa nội dung total_grid_width = best_arrangement['entries_per_row'] * best_arrangement['entry_width'] total_grid_height = best_arrangement['entries_per_column'] * best_arrangement['entry_height'] x_offset = margin_left_pt + (available_width - total_grid_width) / 2 y_offset = margin_top_pt + (available_height - total_grid_height) / 2 # Tạo tài liệu mới cho kết quả new_doc = fitz.open() # Nếu tệp PDF có đúng 2 trang, áp dụng xử lý đặc biệt if total_pages == 2: # Số trang trên mỗi mặt num_entries = best_arrangement['entries_per_row'] * best_arrangement['entries_per_column'] # Tạo danh sách số trang cho mặt trước và mặt sau front_pages = [0] * num_entries # Trang 0 cho mặt trước back_pages = [1] * num_entries # Trang 1 cho mặt sau # Tạo trang mặt trước front_page = new_doc.new_page(width=new_page_width_pt, height=new_page_height_pt) arranged_rects_front = [] index = 0 for row in range(best_arrangement['entries_per_column']): for col in range(best_arrangement['entries_per_row']): x = x_offset + col * best_arrangement['entry_width'] y = y_offset + row * best_arrangement['entry_height'] rect = fitz.Rect( x, y, x + best_arrangement['entry_width'], y + best_arrangement['entry_height'] ) if best_arrangement['rotate']: front_page.show_pdf_page(rect, doc, pno=front_pages[index], rotate=90) else: front_page.show_pdf_page(rect, doc, pno=front_pages[index]) arranged_rects_front.append((rect, row, col)) index += 1 # Thêm crop marks cho mặt trước for rect, row, col in arranged_rects_front: edges = [] if row == 0: edges.append('top') if row == best_arrangement['entries_per_column'] - 1: edges.append('bottom') if col == 0: edges.append('left') if col == best_arrangement['entries_per_row'] - 1: edges.append('right') if edges: self.add_crop_marks(front_page, rect, edges, cropmark_offset_pt, crop_length_pt, crop_thickness) # Tạo trang mặt sau back_page = new_doc.new_page(width=new_page_width_pt, height=new_page_height_pt) arranged_rects_back = [] index = 0 for row in range(best_arrangement['entries_per_column']): for col in range(best_arrangement['entries_per_row']): # Đảo ngược toàn bộ cột: bên trái đổi sang bên phải và ngược lại reversed_col = best_arrangement['entries_per_row'] - 1 - col x = x_offset + col * best_arrangement['entry_width'] # Giữ nguyên cột cho mặt sau y = y_offset + row * best_arrangement['entry_height'] rect = fitz.Rect( x, y, x + best_arrangement['entry_width'], y + best_arrangement['entry_height'] ) if best_arrangement['rotate']: back_page.show_pdf_page(rect, doc, pno=back_pages[index], rotate=90) else: back_page.show_pdf_page(rect, doc, pno=back_pages[index]) arranged_rects_back.append((rect, row, col)) # Thay reversed_col bằng col để sắp xếp đúng thứ tự hàng index += 1 # Thêm crop marks cho mặt sau for rect, row, col in arranged_rects_back: edges = [] if row == 0: edges.append('top') if row == best_arrangement['entries_per_column'] - 1: edges.append('bottom') if col == 0: edges.append('left') if col == best_arrangement['entries_per_row'] - 1: edges.append('right') if edges: self.add_crop_marks(back_page, rect, edges, cropmark_offset_pt, crop_length_pt, crop_thickness) else: # Trường hợp khác, giữ nguyên logic hiện tại # Tính toán số lượng tờ in sheets_needed = (total_pages + max_total_entries - 1) // max_total_entries # Tạo danh sách các danh sách, mỗi danh sách con đại diện cho các trang trên một tờ in sheets_pages = [[] for _ in range(sheets_needed)] # Phân phối các trang vào các tờ in theo phương pháp cắt và xếp chồng entries_per_sheet = best_arrangement['entries_per_row'] * best_arrangement['entries_per_column'] for page_num in range(total_pages): sheet_index = page_num % sheets_needed sheets_pages[sheet_index].append(page_num) # Sắp xếp lại thứ tự các trang trong mỗi tờ in theo hàng và cột for sheet_index, pages_in_sheet in enumerate(sheets_pages): new_page = new_doc.new_page(width=new_page_width_pt, height=new_page_height_pt) arranged_rects = [] for idx, page_num in enumerate(pages_in_sheet): col = idx // best_arrangement['entries_per_column'] row = idx % best_arrangement['entries_per_column'] if col < best_arrangement['entries_per_row'] and row < best_arrangement['entries_per_column']: x = x_offset + col * best_arrangement['entry_width'] y = y_offset + row * best_arrangement['entry_height'] rect = fitz.Rect( x, y, x + best_arrangement['entry_width'], y + best_arrangement['entry_height'] ) if best_arrangement['rotate']: new_page.show_pdf_page(rect, doc, pno=page_num, rotate=90) else: new_page.show_pdf_page(rect, doc, pno=page_num) arranged_rects.append((rect, row, col)) # Thêm crop marks for rect, row, col in arranged_rects: edges = [] if row == 0: edges.append('top') if row == best_arrangement['entries_per_column'] - 1: edges.append('bottom') if col == 0: edges.append('left') if col == best_arrangement['entries_per_row'] - 1: edges.append('right') if edges: self.add_crop_marks(new_page, rect, edges, cropmark_offset_pt, crop_length_pt, crop_thickness) # Lưu tệp PDF đã áp dụng imposition new_doc.save(output_pdf_path) new_doc.close() doc.close() đây là mã 1 xem và ghi nhớ nó - Assistant
Certainly! I've reviewed the code you've provided for the
apply_zigzag_impositionmethod. This function is designed to rearrange pages of a PDF document using the cut-and-stack imposition method, specifically handling cases where the input PDF has exactly two pages. It calculates the best way to arrange the pages based on the provided page size and margins, and adds crop marks accordingly.Here's a brief summary of what the code does:
- Fetches parameters from the user interface, such as page dimensions, margins, and crop mark settings.
- Converts these parameters from millimeters to points for PDF processing.
- Determines the best arrangement of pages on the new sheet size by considering both rotated and non-rotated options to maximize the number of entries per sheet.
- Handles special cases:
- When the input PDF has exactly , it arranges them for front and back printing, reversing the columns on the back side to maintain the correct sequence when cut and stacked.