Browse Source

Started writing code for PDF generation

main
parent
commit
6c20b60900
No known key found for this signature in database GPG Key ID: B9ECA7B2228FC760
4 changed files with 80 additions and 0 deletions
  1. +6
    -0
      config.yaml
  2. +72
    -0
      main.py
  3. +2
    -0
      requirements.txt
  4. BIN
      res/img/logo.png

+ 6
- 0
config.yaml View File

@ -0,0 +1,6 @@
---
sender:
full_name: Aayla Semyonova
title: Script & Web Developer
email_address: [email protected]
forum_url: talk.socialistra.org

+ 72
- 0
main.py View File

@ -15,3 +15,75 @@
# You should have received a copy of the GNU General Public License
# along with right-of-access. If not, see <http://www.gnu.org/licenses/>.
import time
import yaml
import json
from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.lib.colors import Color, HexColor
# Load config file
with open("config.yaml", "r") as file:
config = yaml.load(file, yaml.FullLoader)
sender = config["sender"]
doc = SimpleDocTemplate("right_of_access.pdf",pagesize=letter,
rightMargin=72,leftMargin=72,
topMargin=72,bottomMargin=18)
Story=[]
# Add SRA logo to page
logo = "res/img/logo.png"
im = Image(logo, 2*inch, 2*inch)
Story.append(im)
# Add timestamp to page
styles=getSampleStyleSheet()
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
styles.add(ParagraphStyle(name='Heading', fontSize=14, textColor=HexColor("#980000")))
styles.add(ParagraphStyle(name='Subheading', fontSize=12, textColor=HexColor("#980000")))
ptext = time.ctime()
Story.append(Paragraph(ptext, styles["Normal"]))
Story.append(Spacer(1, 12))
# Generate Introduction
text = [
"Comrade,",
"Below is a copy of your personal information as retained by the Socialist Rifle Association in response to your Right to Access request as well as information about the data collected and what we use it for.",
f"This information is also included in the machine-readable JSON format. If you feel any of this information is incorrect, you can manage your membership info at https://www.joinit.org/o/socialist-rifle-association/ or contact {sender['email_address']}. "
]
for item in text:
Story.append(Paragraph(item, styles["Normal"]))
Story.append(Spacer(1, 12))
def add_section(section_name: str, section_value: str):
Story.append(Paragraph(f"{section_name}", styles["Subheading"]))
Story.append(Spacer(1, 12))
Story.append(Paragraph(section_value, styles["Normal"]))
Story.append(Spacer(1, 12))
with open("data.json", "r") as file:
data = json.load(file)["[email protected]"]
for i in data:
# Convert data to string
if type(data[i]) == str:
add_section(i, data[i])
else:
try:
add_section(i, str(data[i]))
except:
add_section(i, "No Data")
# Create contact info
ptext = 'Sincerely,'
Story.append(Paragraph(ptext, styles["Normal"]))
for key in sender:
value = sender[key]
ptext = '%s' % value.strip()
Story.append(Paragraph(ptext, styles["Normal"]))
doc.build(Story)

+ 2
- 0
requirements.txt View File

@ -0,0 +1,2 @@
reportlab
pyyaml

BIN
res/img/logo.png View File

Before After
Width: 800  |  Height: 800  |  Size: 244 KiB

Loading…
Cancel
Save