Skip to content

Commit

Permalink
modify css, add pdf scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
squi-ddy committed Jan 1, 2024
1 parent 9c8318f commit f09c777
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/node_modules/
.idea

.venv
scripts/*.pdf
14 changes: 11 additions & 3 deletions files/assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@
display: flex;
flex-direction: column;
overflow-y: hidden;
width: 100vw;
height: 95vh;
width: 100%;
height: 100%;
}

html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

.navbar {
Expand All @@ -17,7 +26,6 @@
color: white;
padding: 8px;
text-decoration: none;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}

.nav-link:visited {
Expand Down
54 changes: 54 additions & 0 deletions scripts/add_home.py
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)
11 changes: 11 additions & 0 deletions scripts/optimize.py
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)))
8 changes: 8 additions & 0 deletions scripts/requirements.txt
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

0 comments on commit f09c777

Please sign in to comment.