-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpdf.py
69 lines (61 loc) · 2.71 KB
/
pdf.py
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
import pandas as pd
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
# ===============
# PDF CONSTANTS
# ===============
# The number of columns and rows of data on each page
columns = 2
rows = 7
items_per_page = columns * rows
# The number of pixels per millimeter of page
ppm = 72 / 25.4
# Label positioning values (in mm)
page_width = 210
page_height = 297
margin_top = 15.15
margin_left = 7.75
label_width = 99.1
label_height = 38.1
# Text in label positioning values (in mm)
margin_text_top = 9.5
margin_text_left = 6.5
margin_text_inner = 4.75
def generate_pdf(input_data, out_file):
"""
Generates a pdf in the sticker print format
:param input_data: list of entries containing names and addresses, to be stored in a pdf
:param out_file: the output pdf file
"""
# Initialise PDF settings
print(f"Exporting {len(input_data)} addresses to {out_file}...")
pdf = canvas.Canvas(out_file, pagesize=(ppm * page_width, ppm * page_height))
pdf.setTitle("Thabloid Stickers")
pdfmetrics.registerFont(TTFont("cmunss", "resources/cmunss.ttf"))
pdf.setFont("cmunss", 11)
# Loop through all input entries
for i, entry in input_data.iterrows():
row = i % items_per_page // 2
column = (i % items_per_page) % columns
# Calculate text positions (note that we invert y because it starts at the bottom for some reason)
text_x = margin_left + column * label_width + margin_text_left
text_y = page_height - (margin_top + row * label_height + margin_text_top)
# Draw the name and address
pdf.drawString(ppm * text_x, ppm * text_y, entry["first_name"] + " " + entry["last_name"])
pdf.drawString(ppm * text_x, ppm * (text_y - margin_text_inner), entry["address"])
# Draw second address line, postcode and town. Depends on whether a second address line is set
postal_code_and_city = entry["postal_code"] + " " + entry["city"]
if len(entry["country"]) != 0 and entry["country"].lower() != "netherlands":
postal_code_and_city = postal_code_and_city + " (" + entry["country"] + ")"
if len(entry["address_2"]) != 0:
pdf.drawString(ppm * text_x, ppm * (text_y - 2 * margin_text_inner), entry["address_2"])
pdf.drawString(ppm * text_x, ppm * (text_y - 3 * margin_text_inner), postal_code_and_city)
else:
pdf.drawString(ppm * text_x, ppm * (text_y - 2 * margin_text_inner), postal_code_and_city)
# If we reached a new page, print the current page and re-set the font
if (i + 1) % items_per_page == 0:
pdf.showPage()
pdf.setFont("cmunss", 11)
# Save the pdf
pdf.save()