Skip to content

added pdf_border_frame #1019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions pdf_border_frame/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Package/Script Name

Add configurable frame/borders to all pages in your PDF

## Setup instructions

1. Install PyMuPDF pypi package:
```bash
pip install PyMuPDF
```

2. Run with default frame config:
```bash
python pdf_border_frame.py <path_to_pdf>
```

<br>
3. Run with custom config: <br>

| Flag | Description | Default |
|-----------|---------------------|---------|
| `--l` | Left margin (pt) | 20 |
| `--r` | Right margin (pt) | 20 |
| `--t` | Top margin (pt) | 20 |
| `--b` | Bottom margin (pt) | 20 |
| `--th` | Frame thickness (pt)| 2 |


```bash
python pdf_border_frame.py --t 20 --b 20 --l 20 --r 20 --th 2 <path_to_pdf>
```


## Author
[sk5268](github.com/sk5268)
67 changes: 67 additions & 0 deletions pdf_border_frame/pdf_border_frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import fitz
import argparse
import sys
import os

def add_frame(input_pdf_path, left=20, right=20, top=20, bottom=20, thickness=2):
try:
doc = fitz.open(input_pdf_path)

for page_num in range(len(doc)):
page = doc[page_num]
page_rect = page.rect

frame_rect = fitz.Rect(
left, # left
top, # top
page_rect.width - right, # right
page_rect.height - bottom # bottom
)

page.draw_rect(
frame_rect, # rectangle coordinates
width=thickness # frame thickness
)

# Set output filename if not provided

base, ext = os.path.splitext(input_pdf_path)
output_pdf_path = f"{base}_framed.pdf"

doc.save(output_pdf_path)
print(f"PDF with rectangle frame saved to {output_pdf_path}")

except UnicodeDecodeError as e:
print("Error: Input file path encoding issue. Please ensure the file path is UTF-8 encoded.")
except Exception as e:
print(f"An error occurred: {e}")
finally:
if 'doc' in locals():
doc.close()

if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Add a rectangle frame to each page of a PDF document.\n"
"Flags: --l (left), --r (right), --t (top), --b (bottom), --th (thickness)",
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument("input_pdf", help="Path to the input PDF file")
parser.add_argument("--l", type=float, default=20, help="Left")
parser.add_argument("--r", type=float, default=20, help="Right")
parser.add_argument("--t", type=float, default=20, help="Top")
parser.add_argument("--b", type=float, default=20, help="Bottom")
parser.add_argument("--th", type=float, default=2, help="Thickness")
try:
args = parser.parse_args()
add_frame(
args.input_pdf,
left=args.l,
right=args.r,
top=args.t,
bottom=args.b,
thickness=args.th
)
except Exception as e:
print(f"Error: {e}\n")
parser.print_usage()
sys.exit(1)
Loading