|
15 | 15 | from django.core.cache import cache |
16 | 16 | from django.shortcuts import redirect, render |
17 | 17 | from django.template.loader import get_template |
18 | | -from django.utils import timezone |
19 | 18 | from django.utils.text import slugify |
| 19 | +from pypdf import PdfReader, PdfWriter |
| 20 | +from reportlab.pdfgen import canvas |
20 | 21 | from sentry_sdk import add_breadcrumb, capture_exception |
21 | 22 | from xhtml2pdf import pisa |
22 | 23 |
|
@@ -340,6 +341,41 @@ def html_to_pdf(template_src, filename, context=None): |
340 | 341 | return None |
341 | 342 |
|
342 | 343 |
|
| 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 | + |
343 | 379 | def print_job(obj: PrintJob, do_print: bool = True): |
344 | 380 | printer = obj.printer |
345 | 381 | all_printers = get_printers() |
@@ -369,6 +405,12 @@ def print_job(obj: PrintJob, do_print: bool = True): |
369 | 405 | dest.write(chunk) |
370 | 406 |
|
371 | 407 | 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) |
372 | 414 | if final_filename is not None: |
373 | 415 | delete_filenames.add(final_filename) |
374 | 416 |
|
@@ -474,21 +516,7 @@ def print_job(obj: PrintJob, do_print: bool = True): |
474 | 516 | if obj.fit: |
475 | 517 | args.extend(["-o", "fit-to-page"]) |
476 | 518 |
|
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 | | - |
490 | 519 | try: |
491 | | - subprocess.check_output(["lpr", "-P", printer, title_page], stderr=subprocess.STDOUT, universal_newlines=True) |
492 | 520 | subprocess.check_output(args, stderr=subprocess.STDOUT, universal_newlines=True) |
493 | 521 | except subprocess.CalledProcessError as e: |
494 | 522 | if "is not accepting jobs" in e.output: |
|
0 commit comments