Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import fitz # PyMuPDF | |
| class PDFTransformer: | |
| def __init__(self, pdf_path): | |
| self.pdf_path = pdf_path | |
| self.doc = fitz.open(pdf_path) | |
| def scale_content_to_fit(self, width, height): | |
| for page in self.doc: | |
| rect = page.rect | |
| original_width, original_height = rect.width, rect.height | |
| # Calculate the scaling factor to keep the aspect ratio | |
| scale_factor = min(width / original_width, height / original_height) | |
| # New dimensions based on scale factor | |
| scaled_width = original_width * scale_factor | |
| scaled_height = original_height * scale_factor | |
| # Calculate offsets to center the content on the new page size | |
| x_offset = (width - scaled_width) / 2 | |
| y_offset = (height - scaled_height) / 2 | |
| # Set the mediabox and cropbox to the target dimensions | |
| page.set_mediabox(fitz.Rect(0, 0, width, height)) | |
| page.set_cropbox(fitz.Rect(0, 0, width, height)) | |
| # Adjust cropbox to center scaled content | |
| page.set_cropbox(fitz.Rect(x_offset, y_offset, x_offset + scaled_width, y_offset + scaled_height)) | |
| def save_to_pdf(self, output_path): | |
| self.doc.save(output_path, garbage=4, deflate=True, clean=True) | |
| self.doc.close() | |
| return output_path | |
| def process_pdf(self, width, height, brightness, text_size, line_thickness): | |
| self.scale_content_to_fit(width, height) | |
| output_path = self.pdf_path.replace('.pdf', '_output.pdf') | |
| return self.save_to_pdf(output_path) | |
| # Gradio interface | |
| def transform_pdf(pdf_file, width, height, brightness, text_size, line_thickness): | |
| transformer = PDFTransformer(pdf_file.name) | |
| output_pdf = transformer.process_pdf(width, height, brightness, text_size, line_thickness) | |
| return output_pdf | |
| interface = gr.Interface( | |
| fn=transform_pdf, | |
| inputs=[ | |
| gr.File(label="Upload PDF"), | |
| gr.Number(label="Width (inches)", value=8.5), | |
| gr.Number(label="Height (inches)", value=11), | |
| gr.Slider(1, 5, step=0.1, label="Brightness", value=1.0), # Placeholder, not used | |
| gr.Slider(5, 50, label="Text Size", value=12), # Placeholder, not used | |
| gr.Slider(0.1, 5, step=0.1, label="Line Thickness", value=1.0), # Placeholder, not used | |
| ], | |
| outputs=gr.File(label="Transformed PDF"), | |
| title="Editable PDF Transformation for CorelDRAW Compatibility", | |
| description="Upload a technical PDF, adjust dimensions, and output an editable PDF compatible with CorelDRAW." | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() | |