Skip to content

Commit

Permalink
Squash commits
Browse files Browse the repository at this point in the history
Update README.md

smaller doc -> letter size conversion CLI

Remove ConvertToLetterSize.egg-info directory from repo

Update README.md

Update README.md

Update README.md

Update README.md

Update git ignore
  • Loading branch information
cpparnell committed Feb 27, 2024
1 parent 94c00a6 commit 602dc74
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
venv/
*.egg-info/
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# a4tolettersize
Convert A4 document to letter size while maintaining size and proportion
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.
Content will maintain its absolute size and proportions when printed.

This tool was created to convert old sewing pattern PDFs to US letter size, allowing me to print them at a US print shop.

## how to use
To use for yourself, first clone this repository and `cd` to it:
```cmd
git clone https://github.com/cpparnell/converttolettersize.git
cd converttolettersize
```
Then, install the command line tool:
```cmd
pip install .
```
Finally, you can convert from smaller formats to letter size using:
```cmd
converttolettersize /path/to/input/file.pdf /path/to/output/file.pdf
```
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .convert import convert_to_lettersize
Binary file added __pycache__/convert.cpython-311.pyc
Binary file not shown.
44 changes: 44 additions & 0 deletions 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()

15 changes: 15 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from setuptools import setup

setup(
name='ConvertToLetterSize',
version='0.1',
py_modules=['converttolettersize'],
install_requires=[
'Click',
'PyMuPDF',
],
entry_points='''
[console_scripts]
converttolettersize=convert:convert_to_lettersize
''',
)

0 comments on commit 602dc74

Please sign in to comment.