Skip to content

Commit 9bf2459

Browse files
committed
fix: add watermark to first page
1 parent caa7e60 commit 9bf2459

66 files changed

Lines changed: 106 additions & 78 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

celerybeat-schedule-shm

32 KB
Binary file not shown.

celerybeat-schedule-wal

354 KB
Binary file not shown.

config/krb5.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
tjhsst.edu = CSL.TJHSST.EDU
1818
.tjhsst.edu = CSL.TJHSST.EDU
1919
csl.tjhsst.edu = CSL.TJHSST.EDU
20-
.csl.tjhsst.edu = CSL.TJHSST.EDU
20+
.csl.tjhsst.edu = CSL.TJHSST.EDU

config/scripts/README.md

Lines changed: 1 addition & 1 deletion

intranet/apps/printing/views.py

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
from django.core.cache import cache
1616
from django.shortcuts import redirect, render
1717
from django.template.loader import get_template
18-
from django.utils import timezone
1918
from django.utils.text import slugify
19+
from pypdf import PdfReader, PdfWriter
20+
from reportlab.pdfgen import canvas
2021
from sentry_sdk import add_breadcrumb, capture_exception
2122
from xhtml2pdf import pisa
2223

@@ -340,6 +341,41 @@ def html_to_pdf(template_src, filename, context=None):
340341
return None
341342

342343

344+
def add_watermark(pdf_path: str, obj) -> str:
345+
reader = PdfReader(pdf_path)
346+
writer = PdfWriter()
347+
348+
out_fd, out_path = tempfile.mkstemp(suffix="_wm.pdf")
349+
i = 0
350+
351+
for page in reader.pages:
352+
if i == 0: # Checks if page is the first page to be printed
353+
packet = BytesIO()
354+
c = canvas.Canvas(packet)
355+
356+
text = f"{obj.user.username} , {obj.id}"
357+
358+
c.setFont("Helvetica", 6)
359+
c.setFillGray(0.2)
360+
361+
page_width = float(page.mediabox.width)
362+
c.drawString(page_width - 150, 12, text)
363+
364+
c.save()
365+
packet.seek(0)
366+
367+
overlay = PdfReader(packet).pages[0]
368+
page.merge_page(overlay) # Overlays the watermark on the first page
369+
i += 1
370+
371+
writer.add_page(page)
372+
373+
with open(out_path, "wb") as f:
374+
writer.write(f)
375+
376+
return out_path
377+
378+
343379
def print_job(obj: PrintJob, do_print: bool = True):
344380
printer = obj.printer
345381
all_printers = get_printers()
@@ -369,6 +405,12 @@ def print_job(obj: PrintJob, do_print: bool = True):
369405
dest.write(chunk)
370406

371407
final_filename = convert_file(tmpfile_name, filebase)
408+
409+
if not final_filename:
410+
raise Exception("Error converting file to PDF for printing")
411+
412+
final_filename = add_watermark(final_filename, obj)
413+
delete_filenames.add(final_filename)
372414
if final_filename is not None:
373415
delete_filenames.add(final_filename)
374416

@@ -474,21 +516,7 @@ def print_job(obj: PrintJob, do_print: bool = True):
474516
if obj.fit:
475517
args.extend(["-o", "fit-to-page"])
476518

477-
title_page = html_to_pdf(
478-
"printing/title_page.html",
479-
final_filename,
480-
{
481-
"obj": obj,
482-
"filename": filebase,
483-
"time": timezone.now(),
484-
"pages": num_pages,
485-
},
486-
)
487-
488-
delete_filenames.add(title_page)
489-
490519
try:
491-
subprocess.check_output(["lpr", "-P", printer, title_page], stderr=subprocess.STDOUT, universal_newlines=True)
492520
subprocess.check_output(args, stderr=subprocess.STDOUT, universal_newlines=True)
493521
except subprocess.CalledProcessError as e:
494522
if "is not accepting jobs" in e.output:

intranet/static/css/admin.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ div.readonly {
44

55
div.field-request_json {
66
font-family: monospace;
7-
}
7+
}

intranet/static/css/announcements.request.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ ol li {
4242

4343
div.cke_chrome {
4444
margin: 10px 0;
45-
}
45+
}

intranet/static/css/bus.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,4 @@ svg text {
329329
transform: rotate(180deg) translate(87%, 18%);
330330
transform-origin: center;
331331
}
332-
*/
332+
*/
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.enrichment hr {
22
border: 1px solid rgb(132, 132, 132);
3-
}
3+
}

intranet/static/css/enrichment.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@
118118
font-size: 50pt;
119119
}
120120
}
121-
}
121+
}

0 commit comments

Comments
 (0)