-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.py
More file actions
72 lines (60 loc) · 1.91 KB
/
document.py
File metadata and controls
72 lines (60 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import io
import os
import sys
import tempfile
import webbrowser
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from reportlab.lib import colors
import textwrap
def generate_spy_pad_pdf(pad_lines):
width, height = A4
buffer = io.BytesIO()
c = canvas.Canvas(buffer, pagesize=A4)
c.setFont("Helvetica-Bold", 28)
c.drawCentredString(width/2, height - 150, "STRICTLY CONFIDENTIAL")
c.setFont("Helvetica-Bold", 14)
c.drawCentredString(width/2, height - 200, "MINISTRY OF REXCHOPPERS")
c.setFont("Helvetica", 12)
# Watermark
c.setFillColorRGB(0.8, 0.8, 0.8)
c.setFont("Helvetica-Bold", 72)
c.rotate(45)
c.drawCentredString(height/2, -width/4, "TOP SECRET")
c.rotate(-45)
c.setFillColor(colors.black)
c.showPage()
# === Pad Pages ===
c.setFont("Courier", 12)
y = height - 50
page_num = 1
for i, row in enumerate(pad_lines, start=1):
grouped = " ".join(textwrap.wrap(row.replace(" ", ""), 5))
c.rect(30, y-3, 8, 8)
c.drawString(50, y, f"{i:03d} {grouped}")
y -= 15
if y < 50:
c.setFont("Helvetica", 8)
c.drawRightString(width - 30, 30, f"Page {page_num}")
c.showPage()
c.setFont("Courier", 12)
y = height - 50
page_num += 1
c.setFont("Helvetica", 8)
c.drawRightString(width - 30, 30, f"Page {page_num}")
c.save()
buffer.seek(0)
return buffer.getvalue()
def preview_pdf_external(pdf_bytes):
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as f:
f.write(pdf_bytes)
temp_path = f.name
if sys.platform == "win32":
os.startfile(temp_path)
elif sys.platform == "darwin":
os.system(f"open '{temp_path}'")
else:
try:
os.system(f"xdg-open '{temp_path}'")
except Exception:
webbrowser.open(f"file://{temp_path}")