Skip to content

Commit

Permalink
Small updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cpparnell committed Mar 19, 2024
1 parent 602dc74 commit ad9e749
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.venv/
venv/
*.egg-info/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# a4tolettersize
# converttolettersize
Convert smaller format pdf document to letter size while maintaining size and proportion of content.

This will resize the pages in the document to letter size and center the existing content.
Expand Down
Binary file modified __pycache__/convert.cpython-311.pyc
Binary file not shown.
44 changes: 44 additions & 0 deletions build/lib/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
import click
import fitz # Import PyMuPDF

# Define the conversion function with click decorators for CLI integration
@click.command()
@click.argument('input_file', type=click.Path(exists=True))
@click.argument('output_file', type=click.Path())
def convert_to_lettersize(input_file: str, output_file: str) -> None:
"""
Converts PDF document to Letter size, preserving the content's size and proportions.
"""
letter_size = [612, 792] # Letter size in points (8.5 x 11 inches)
new_doc = fitz.open() # Create a new empty document

with fitz.open(input_file) as doc:
for page in doc:
original_rect = page.rect
scale = 1 # Default scale is 1 (no scaling)

# Calculate the new width and height based on scaling
new_width = original_rect.width * scale
new_height = original_rect.height * scale

# Calculate translation to center the content
translate_x = (letter_size[0] - new_width) / 2
translate_y = (letter_size[1] - new_height) / 2

# Create a new page with Letter dimensions
new_page = new_doc.new_page(width=letter_size[0], height=letter_size[1])

# Define the target rectangle for the scaled and centered content
target_rect = fitz.Rect(translate_x, translate_y, translate_x + new_width, translate_y + new_height)

# Insert the scaled and centered content of the current page into the new page
new_page.show_pdf_page(target_rect, doc, page.number)

# Save the new document
new_doc.save(output_file)
click.echo(f"Converted file saved to {output_file}")

if __name__ == '__main__':
convert_to_lettersize()

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name='ConvertToLetterSize',
version='0.1',
py_modules=['converttolettersize'],
py_modules=['convert'],
install_requires=[
'Click',
'PyMuPDF',
Expand Down

0 comments on commit ad9e749

Please sign in to comment.