-
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
87 additions
and
3 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,5 @@ | ||
/node_modules/ | ||
.idea | ||
|
||
.venv | ||
scripts/*.pdf |
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
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,54 @@ | ||
from pypdf import PdfWriter, PdfReader, Transformation | ||
from pypdf.generic import RectangleObject | ||
from pypdf.annotations import Link | ||
from copy import copy | ||
|
||
# constants | ||
FILE_NAME = "Yearbook 2021.pdf" | ||
ICON_NAME = "house.pdf" | ||
FILE_OUT = "merged-pdf.pdf" | ||
PAGES = range(6, 103, 2) | ||
HOME_PAGE_INDEX = 2 | ||
MARGIN = 5 | ||
|
||
# get the base file | ||
reader_base = PdfReader(FILE_NAME) | ||
|
||
# get the icon | ||
reader = PdfReader(ICON_NAME) | ||
page_icon = reader.pages[0] | ||
|
||
# get the icon size | ||
icon_width: float = page_icon.mediabox.width | ||
icon_height: float = page_icon.mediabox.height | ||
|
||
# result writer | ||
writer = PdfWriter(clone_from=reader_base) | ||
|
||
for page in range(len(writer.pages)): | ||
page_base = writer.pages[page] | ||
|
||
if page in PAGES: | ||
# get the page size | ||
page_width: float = page_base.mediabox.width | ||
page_height: float = page_base.mediabox.height | ||
|
||
# transform the icon to the right, with a small margin | ||
trans = Transformation().translate(tx=page_width - icon_width - MARGIN, ty=MARGIN) | ||
page_icon_copy = copy(page_icon) | ||
page_icon_copy.mediabox = RectangleObject((0, 0, page_width, page_height)) | ||
page_icon_copy.add_transformation(trans) | ||
|
||
page_base.merge_page(page_icon_copy, over=True) | ||
|
||
# add a link annotation to page 2 | ||
annotation = Link( | ||
rect=(page_width - icon_width - MARGIN, MARGIN, page_width - MARGIN, icon_height + MARGIN), | ||
target_page_index=HOME_PAGE_INDEX | ||
) | ||
writer.add_annotation(page_number=page, annotation=annotation) | ||
else: | ||
writer.add_page(page_base) | ||
|
||
with open("merged-pdf.pdf", "wb") as fp: | ||
writer.write(fp) |
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,11 @@ | ||
from pikepdf import Pdf, ObjectStreamMode, settings | ||
from tqdm import tqdm | ||
|
||
settings.set_flate_compression_level(9) | ||
|
||
INPUT_PDF = "Yearbook-2020.pdf" | ||
OUTPUT_PDF = "Yearbook-2020-opt.pdf" | ||
|
||
with Pdf.open(INPUT_PDF) as pdf: | ||
with tqdm(total=100, unit="%") as pbar: | ||
pdf.save(OUTPUT_PDF, recompress_flate=True, object_stream_mode=ObjectStreamMode.generate, linearize=True, progress=(lambda x: pbar.update(x - pbar.n))) |
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,8 @@ | ||
Deprecated==1.2.14 | ||
lxml==5.0.0 | ||
packaging==23.2 | ||
pikepdf==8.11.2 | ||
Pillow==10.1.0 | ||
pypdf==3.17.4 | ||
tqdm==4.66.1 | ||
wrapt==1.16.0 |