Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

def applyCutandStackimposition(self, inputpdfpath, outputpdfpath): """Applies the cut…

  1. User

    def apply_CutandStack_imposition(self, input_pdf_path, output_pdf_path): """Applies the cut and stack imposition method to arrange pages according to the cut and stack algorithm."""

        # 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()
    
        # Tách các trang mặt trước và mặt sau
        front_pages = [i for i in range(total_pages) if i % 2 == 0]  # Trang lẻ (0-based index)
        back_pages = [i for i in range(total_pages) if i % 2 != 0]   # Trang chẵn
    
        # Tính toán số lượng tờ in
        entries_per_sheet = best_arrangement['entries_per_row'] * best_arrangement['entries_per_column']
        num_sheets = (len(front_pages) + entries_per_sheet - 1) // entries_per_sheet
    
        # Số stack (cột)
        num_stacks = best_arrangement['entries_per_row']
        pages_per_stack = len(front_pages) // num_stacks
    
        for sheet_index in range(num_sheets):
            # Sắp xếp mặt trước
            front_page = new_doc.new_page(width=new_page_width_pt, height=new_page_height_pt)
            arranged_rects_front = []
    
            for row in range(best_arrangement['entries_per_column']):
                for col in range(best_arrangement['entries_per_row']):
                    # Tính toán index trong stack
                    index_in_stack = sheet_index * best_arrangement['entries_per_column'] + row
                    page_number = index_in_stack + col * pages_per_stack
                    if page_number >= len(front_pages):
                        continue
                    page_num = front_pages[page_number]
    
                    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=page_num, rotate=90)
                    else:
                        front_page.show_pdf_page(rect, doc, pno=page_num)
                    arranged_rects_front.append((rect, row, col))
    
            # 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)
    
            # Sắp xếp mặt sau
            if sheet_index < num_sheets:
                back_page = new_doc.new_page(width=new_page_width_pt, height=new_page_height_pt)
                arranged_rects_back = []
    
                for row in range(best_arrangement['entries_per_column']):
                    for col in range(best_arrangement['entries_per_row']):
                        # Tính toán index trong stack
                        index_in_stack = sheet_index * best_arrangement['entries_per_column'] + row
                        page_number = index_in_stack + col * pages_per_stack
                        if page_number >= len(back_pages):
                            continue
                        page_num = back_pages[page_number]
    
                        # Đảo ngược cột để tạo hiệu ứng đối xứng
                        reversed_col = best_arrangement['entries_per_row'] - 1 - col
                        x = x_offset + reversed_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']:
                            back_page.show_pdf_page(rect, doc, pno=page_num, rotate=90)
                        else:
                            back_page.show_pdf_page(rect, doc, pno=page_num)
                        arranged_rects_back.append((rect, row, col))
    
                # 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)
    
        # Lưu tệp PDF đã áp dụng imposition
        new_doc.save(output_pdf_path)
        new_doc.close()
        doc.close()   kiểm tra mã này giữ nguyên mọi thứ chỉnh lại phần đặt cropmark cho trang chẵn sao cho nó đặt crop mark xung quanh các đối tượng như trang lè
    
  2. Assistant

    Certainly! The code you've provided handles the placement of crop marks on the front pages correctly, but there's an issue with how crop marks are determined for the back pages due to the column reversal when creating the mirror effect.

    When arranging the back pages, you reverse the columns to achieve the cut and stack imposition. However, when adding crop marks to the back pages, you need to adjust the logic to account for this reversal. Specifically, the conditions that determine whether a content rectangle is on the leftmost or rightmost edge need to be swapped.

    Here's the adjusted code for the back pages where the crop marks are added. I've added comments to explain the changes:

    # Add crop marks for the back pages
    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')
        
        # Adjust left and right edges due to column reversal
        if col == 0:
            edges.append('right')  # In reversed order, col 0 is on the right edge
        if col == best_arrangement['entries_per_row'] - 1:
            edges.append('left')   # In reversed order, last col is on the left edge
        
        if edges:
            self.add_crop_marks(back_page, rect, edges, cropmark_offset_pt, crop_length_pt, crop_thickness)
    

    By swapping 'left' and 'right' in the conditions for the back pages, you ensure that the crop marks are placed around the content rectangles just like on the front pages.

    Here's the complete adjusted section within your method:

    # Add crop marks for the back pages
    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')
        
        # Adjust left and right edges due to column reversal
        if col == 0:
            edges.append('right')  # In reversed order, col 0 is on the right edge
        if col == best_arrangement['entries_per_row'] - 1:
            edges.append('left')   # In reversed order, last col is on the left edge
        
        if edges:
            self.add_crop_marks(back_page, rect, edges, cropmark_offset_pt, crop_length_pt, crop_thickness)
    

    With this adjustment, your code should now place crop marks around the objects on the even pages in the same way as on the odd pages, ensuring consistency across your imposed document.