|
| 1 | +from io import BytesIO |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pymupdf |
| 5 | +from pypdf import PdfReader, PdfWriter |
| 6 | +from reportlab.lib import colors |
| 7 | +from reportlab.lib.pagesizes import A4 |
| 8 | +from reportlab.lib.units import inch |
| 9 | +from reportlab.pdfgen import canvas |
| 10 | + |
| 11 | +# param to describe margin for exam generation text |
| 12 | +BORDER = 20 |
| 13 | + |
| 14 | +def create_watermark( |
| 15 | + computing_id: str, |
| 16 | + density: int = 5 |
| 17 | +) -> BytesIO: |
| 18 | + """ |
| 19 | + Returns a PDF with one page containing the watermark as text. |
| 20 | + """ |
| 21 | + # Generate the tiling watermark |
| 22 | + stamp_buffer = BytesIO() |
| 23 | + stamp_pdf = canvas.Canvas(stamp_buffer, pagesize=A4) |
| 24 | + stamp_pdf.translate(density / 3 * inch, -density / 3 * inch) |
| 25 | + stamp_pdf.setFillColor(colors.grey, alpha=0.5) |
| 26 | + stamp_pdf.setFont("Helvetica", 100 / density) |
| 27 | + stamp_pdf.rotate(45) |
| 28 | + |
| 29 | + width, height = A4 |
| 30 | + |
| 31 | + for i in range(1, 4 * int(width), int(width/density)): |
| 32 | + for j in range(1, 4 * int(height), int(height/density)): |
| 33 | + stamp_pdf.drawCentredString(i, j, computing_id) |
| 34 | + stamp_pdf.save() |
| 35 | + stamp_buffer.seek(0) |
| 36 | + |
| 37 | + # Generate the warning text in corner |
| 38 | + warning_buffer = BytesIO() |
| 39 | + warning_pdf = canvas.Canvas(warning_buffer, pagesize=A4) |
| 40 | + warning_pdf.setFillColor(colors.grey, alpha=0.75) |
| 41 | + warning_pdf.setFont("Helvetica", 14) |
| 42 | + |
| 43 | + from datetime import datetime |
| 44 | + warning_pdf.drawString(BORDER, BORDER, f"This exam was generated by {computing_id} at {datetime.now()}") |
| 45 | + |
| 46 | + warning_pdf.save() |
| 47 | + warning_buffer.seek(0) |
| 48 | + |
| 49 | + # Merge into watermark |
| 50 | + watermark_pdf = PdfWriter() |
| 51 | + stamp_pdf = PdfReader(stamp_buffer) |
| 52 | + warning_pdf = PdfReader(warning_buffer) |
| 53 | + # Destructively merges in place |
| 54 | + stamp_pdf.pages[0].merge_page(warning_pdf.pages[0]) |
| 55 | + watermark_pdf.add_page(stamp_pdf.pages[0]) |
| 56 | + |
| 57 | + watermark_buffer = BytesIO() |
| 58 | + watermark_pdf.write(watermark_buffer) |
| 59 | + watermark_buffer.seek(0) |
| 60 | + return watermark_buffer |
| 61 | + |
| 62 | +def apply_watermark( |
| 63 | + pdf_path: Path | str, |
| 64 | + # expect a BytesIO instance (at position 0), accept a file/path |
| 65 | + stamp: BytesIO | Path | str, |
| 66 | +) -> BytesIO: |
| 67 | + # process file |
| 68 | + stamp_page = PdfReader(stamp).pages[0] |
| 69 | + |
| 70 | + writer = PdfWriter() |
| 71 | + reader = PdfReader(pdf_path) |
| 72 | + writer.append(reader) |
| 73 | + for pdf_page in writer.pages: |
| 74 | + pdf_page.transfer_rotation_to_content() |
| 75 | + pdf_page.merge_page(stamp_page) |
| 76 | + |
| 77 | + watermarked_pdf = BytesIO() |
| 78 | + writer.write(watermarked_pdf) |
| 79 | + watermarked_pdf.seek(0) |
| 80 | + |
| 81 | + return watermarked_pdf |
| 82 | + |
| 83 | +def raster_pdf( |
| 84 | + pdf_path: BytesIO, |
| 85 | + dpi: int = 300 |
| 86 | +) -> BytesIO: |
| 87 | + raster_buffer = BytesIO() |
| 88 | + # adapted from https://github.com/pymupdf/PyMuPDF/discussions/1183 |
| 89 | + with pymupdf.open(stream=pdf_path) as doc: |
| 90 | + page_count = doc.page_count |
| 91 | + with pymupdf.open() as target: |
| 92 | + for page, _dpi in zip(doc, [dpi] * page_count, strict=False): |
| 93 | + zoom = _dpi / 72 |
| 94 | + mat = pymupdf.Matrix(zoom, zoom) |
| 95 | + pix = page.get_pixmap(matrix=mat) |
| 96 | + tarpage = target.new_page(width=page.rect.width, height=page.rect.height) |
| 97 | + tarpage.insert_image(tarpage.rect, stream=pix.pil_tobytes("PNG")) |
| 98 | + |
| 99 | + target.save(raster_buffer) |
| 100 | + raster_buffer.seek(0) |
| 101 | + return raster_buffer |
| 102 | + |
| 103 | +def raster_pdf_from_path( |
| 104 | + pdf_path: Path | str, |
| 105 | + dpi: int = 300 |
| 106 | +) -> BytesIO: |
| 107 | + raster_buffer = BytesIO() |
| 108 | + # adapted from https://github.com/pymupdf/PyMuPDF/discussions/1183 |
| 109 | + with pymupdf.open(filename=pdf_path) as doc: |
| 110 | + page_count = doc.page_count |
| 111 | + with pymupdf.open() as target: |
| 112 | + for page, _dpi in zip(doc, [dpi] * page_count, strict=False): |
| 113 | + zoom = _dpi / 72 |
| 114 | + mat = pymupdf.Matrix(zoom, zoom) |
| 115 | + pix = page.get_pixmap(matrix=mat) |
| 116 | + tarpage = target.new_page(width=page.rect.width, height=page.rect.height) |
| 117 | + tarpage.insert_image(tarpage.rect, stream=pix.pil_tobytes("PNG")) |
| 118 | + |
| 119 | + target.save(raster_buffer) |
| 120 | + raster_buffer.seek(0) |
| 121 | + return raster_buffer |
0 commit comments