-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.venv/ | ||
venv/ | ||
*.egg-info/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters