Commit 38bcd35a authored by PizZaKatZe's avatar PizZaKatZe

Implement PDF generator proof-of-concept

parent b7c4a1ed
Pipeline #740 failed with stages
in 0 seconds
include jht_flyergen/template.tex
# Junghackertag flyer generator
A Python module that reads information about self-organized sessions for kids from the congress
wiki and turns it into a PDF using
* Jinja2
* LaTeX
and black magic.
## Requirements
To use this module, you need at least:
* Python 3
* [latex](
* LaTeX
* [koma-script](
* [qrcode](
## CLI
For convenience and testing purposes, a simple CLI is included:
usage: jht_flyergen [-h] [-p PRINT] (-u [URL] | -f PATH) [output]
positional arguments:
output path to output file; if omitted raw PDF data is
written to stdout
optional arguments:
-h, --help show this help message and exit
-p PRINT, --print PRINT
print filtered data of session with index PRINT,
starting at 1; 0 prints all sessions
data sources:
-u [URL], --url [URL]
URL to fetch JSON data from; if URL is omitted, a
hard-coded URL returning test data is used
-f PATH, --file PATH path to file containing JSON data
......@@ -20,8 +20,9 @@
from argparse import ArgumentParser
from json import dumps
from sys import argv
from sys import stdout
from jht_flyergen.engine import fetch_data, read_data
from jht_flyergen.engine import fetch_data, filter_session, generate_flyer, read_data
TEST_URL = '' # pylint: disable=line-too-long
......@@ -29,8 +30,11 @@ def parse_arguments(args):
"""Parses command-line arguments.
parser = ArgumentParser()
parser.add_argument('-v', dest='verbose', action='store_true',
help='enable verbose output (useful for debugging)')
parser.add_argument('-p', '--print',
help='print filtered data of session with index PRINT, starting at 1; ' +
'0 prints all sessions')
parser.add_argument('output', nargs='?',
help='path to output file; if omitted raw PDF data is written to stdout')
sources = parser.add_argument_group('data sources').add_mutually_exclusive_group(required=True)
sources.add_argument('-u', '--url', nargs='?', const=TEST_URL,
......@@ -49,9 +53,20 @@ def main():
"""Program entry point.
args = parse_arguments(argv[1:])
sessions = None
if args.url:
sessions = fetch_data(args.url)
elif args.path:
sessions = read_data(args.path)
sessions = list(map(filter_session, sessions))
if args.print:
end = int(args.print)
pretty_print(sessions[0 : end if end else -1])
if args.output:
with open(args.output, 'wb') as output:
......@@ -19,10 +19,15 @@
from datetime import datetime
from json import load
from os.path import dirname
from re import sub
from typing import Dict, List
from urllib.request import urlopen
from jinja2 import FileSystemLoader
from latex import build_pdf # type: ignore; pylint: disable=import-error
from latex.jinja2 import make_env # type: ignore; pylint: disable=import-error
def fetch_data(url: str) -> List[Dict]:
"""Fetches given URL and returns self-organized session data.
......@@ -72,3 +77,11 @@ def read_data(path: str) -> List[Dict]:
with open(path) as data_file:
return load(data_file)
def generate_flyer(sessions: List[Dict]) -> bytes:
"""Generates a PDF from self-organized session data.
env = make_env(loader=FileSystemLoader(dirname(__file__)))
template = env.get_template('template.tex')
pdf = build_pdf(template.render(sessions=sessions))
return pdf.__bytes__()
\newcommand{\thetitle}{Junghackertag: Angebote}
\newcommand{\theauthor}{Chaos macht Schule}
pdftitle = {\thetitle},
pdfauthor = {\theauthor},
pdfsubject = {Junghackertag},
pdfborder = {0 0 0},
%- for session in sessions
\section{\VAR{session.title | e}}
%- if session.description
\VAR{session.description | e}
\ \\[1em]
%- endif
%- if session.url
\qrcode{\VAR{session.url | e}}
\ \\[1em]
%- endif
%- if session.providers
%- for provider in session.providers
\item \VAR{ | e} %\qrcode{\VAR{provider.url | e}}
%- endfor
%- endif
%- if session.starttimes
%- for time in session.starttimes
\item \VAR{time | e}
%- endfor
%- endif
%- endfor
......@@ -10,10 +10,12 @@ setup(
'jht_flyergen = jht_flyergen.cli:main',
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment