From 981f6dd4c651307bb8e7ed73799dd359a34e413d Mon Sep 17 00:00:00 2001 From: awwaawwa <8493196+awwaawwa@users.noreply.github.com> Date: Fri, 21 Feb 2025 21:39:31 +0800 Subject: [PATCH] feat: Implement side-by-side PDF generation for translations - Add functionality to create dual-page PDF with original and translated content - Support side-by-side page rendering using PyMuPDF - Implement page width and height calculation for optimal layout - Add support for configurable page order in dual PDF generation --- babeldoc/document_il/backend/pdf_creater.py | 51 +++++++++++++++++---- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/babeldoc/document_il/backend/pdf_creater.py b/babeldoc/document_il/backend/pdf_creater.py index 683119ac..3d996588 100644 --- a/babeldoc/document_il/backend/pdf_creater.py +++ b/babeldoc/document_il/backend/pdf_creater.py @@ -385,7 +385,48 @@ def write(self, translation_config: TranslationConfig) -> TranslateResult: f"{basename}{debug_suffix}.{translation_config.lang_out}.dual.pdf", ) translation_config.raise_if_cancelled() - dual = pymupdf.open(self.original_pdf_path) + original_pdf = pymupdf.open(self.original_pdf_path) + translated_pdf = pdf + + # Create a new PDF for side-by-side pages + dual = pymupdf.open() + page_count = min(original_pdf.page_count, translated_pdf.page_count) + + for page_id in range(page_count): + # Get pages from both PDFs + orig_page = original_pdf[page_id] + trans_page = translated_pdf[page_id] + + # Calculate total width and use max height + total_width = orig_page.rect.width + trans_page.rect.width + max_height = max(orig_page.rect.height, trans_page.rect.height) + + # Create new page with combined width + dual_page = dual.new_page(width=total_width, height=max_height) + + # Define rectangles for left and right sides + rect_left = pymupdf.Rect(0, 0, orig_page.rect.width, max_height) + rect_right = pymupdf.Rect( + orig_page.rect.width, + 0, + total_width, + max_height, + ) + + # Show original page on left and translated on right + dual_page.show_pdf_page( + rect_left, + original_pdf, + page_id, + keep_proportion=True, + ) + dual_page.show_pdf_page( + rect_right, + translated_pdf, + page_id, + keep_proportion=True, + ) + if translation_config.debug: translation_config.raise_if_cancelled() try: @@ -395,13 +436,7 @@ def write(self, translation_config: TranslationConfig) -> TranslateResult: "Failed to write debug info to dual PDF", exc_info=True, ) - dual.insert_file(pdf) - page_count = pdf.page_count - for page_id in range(page_count): - if translation_config.dual_translate_first: - dual.move_page(page_count + page_id, page_id * 2) - else: - dual.move_page(page_count + page_id, page_id * 2 + 1) + dual.save( dual_out_path, garbage=3,