Skip to content

added pdf_border_frames #1020

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 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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_frames/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# PDF_Border_Frames

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)
75 changes: 75 additions & 0 deletions pdf_border_frames/pdf_border_frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import fitz # PyMuPDF
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 in doc:
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,
width=thickness
)

base, _ = 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:
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()


def 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 margin")
parser.add_argument("--r", type=float, default=20, help="Right margin")
parser.add_argument("--t", type=float, default=20, help="Top margin")
parser.add_argument("--b", type=float, default=20, help="Bottom margin")
parser.add_argument("--th", type=float, default=2, help="Frame 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)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions pdf_border_frames/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PyMuPDF==1.26.0
Loading