\documentclass[a5paper]{article}

\usepackage[ngerman]{babel}
\usepackage{hyphenat}
\hyphenation{Mathe-matik wieder-gewinnen}

\usepackage{fontspec}
\setmainfont{Liberation Sans}

\usepackage[table]{xcolor}

\usepackage[portrait,margin=0cm]{geometry} % ,showframe
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{array}
\usepackage{tabularx}
\usepackage{csvsimple}
\usepackage{adjustbox}
\usepackage{booktabs}
\usepackage{intcalc}
\usepackage{pgffor}
\usepackage{hyperref}
%\usepackage{fancyhdr}

\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper,landscape,border shrink=1cm]

\usepackage{atbegshi}
\AtBeginShipout{
	\pgfpagesshipoutlogicalpage{1}\copy\AtBeginShipoutBox
	\pgfpagesshipoutlogicalpage{2}\box\AtBeginShipoutBox
	\pgfshipoutphysicalpage
}

\pagestyle{empty}

%\pagestyle{fancy}
%\fancyhf{}
%\renewcommand{\headrulewidth}{0pt}
%\fancyfoot[C]{Powered by \LaTeX}

\setlength{\parindent}{0pt}

\def\persiderowcount{22}

\newcommand{\stimmzettel}[2]{
	\noindent
	\begin{minipage}[t]{0.49\textwidth}
		\vspace{-\fboxsep}
		\includegraphics[width=\textwidth]{img/ohg.pdf}
	\end{minipage}
	\begin{minipage}[t]{0.49\textwidth}
		\vspace{-\fboxsep}
		\hspace*{0pt}\hfill
		\includegraphics[width=0.3\textwidth]{img/smv_neu.pdf}
	\end{minipage}
	
	\medskip\medskip\medskip
	
	\begin{center}
		\textbf{\LARGE Stimmzettel} \\
		{\large für die \underline{\textbf{#1:in}}-Wahl des Otto-Hahn-Gymnasiums} \\
		
		\medskip\medskip
		
		{\large Sie haben \textbf{\LARGE 1} Stimme}
	\end{center}
	
	\medskip

	\csvreader{#2}{}{}
	\edef\totalrows{\thecsvrow}

	\edef\colcount{\intcalcDiv{\totalrows}{\persiderowcount}}
	\edef\colcount{\intcalcAdd{\colcount}{\ifnum\intcalcMod{\totalrows}{\persiderowcount}>0 1 \else 0 \fi}}
	\edef\colwidth{
		\ifnum\colcount=0
			0
		\else\ifnum\colcount=1
			1
		\else\ifnum\colcount=2
			0.49
		\else
			0
		\fi\fi\fi
	}

	\ifnum\colcount>0
		\foreach \n in {0,...,\intcalcSub{\colcount}{1}}{
			\noindent
			\begin{minipage}[t]{\colwidth\textwidth}
				\vspace{-\fboxsep}
				\def\arraystretch{2}
	  			

	  			\ifnum\totalrows>2
	  				\rowcolors{2}{gray!10}{white}
	  			\fi
	  			
				\edef\rowcountstart{\intcalcMul{\n}{\persiderowcount}}
				\begin{tabularx}{\linewidth}{|r|X|l|>{\centering\arraybackslash}c|}			
					\rowcolor{white}
					\toprule
					\multicolumn{1}{r}{\large\textbf{\#}} &
					\multicolumn{1}{l}{\large\textbf{Kandidaten}} &
					\multicolumn{1}{l}{\large\textbf{Klasse}} &
					\multicolumn{1}{c}{
					\adjustbox{valign=t}{	
						\begin{tikzpicture}
								\draw (0,0.4) circle(2mm);
								\draw (-0.1,0.5) -- (0.1,0.3);
								\draw (-0.1,0.3) -- (0.1,0.5);
						\end{tikzpicture}}
					}
					\\
					
					\rowcolor{white}
					\bottomrule
					\multicolumn{1}{r}{} &
					\multicolumn{1}{l}{} &
					\multicolumn{1}{l}{} &
					\multicolumn{1}{l}{}
					\\
					
					\hline
					\csvreader[
						head to column names,
						late after line={\\ \hline},
						filter expr={
								test{\ifnumgreater{\thecsvinputline-1}{\rowcountstart}}
							and	test{\ifnumless{\thecsvinputline-1}{\rowcountstart+\persiderowcount+1}}
						}
					]{#2}{}
					{
						\large\textbf{\the\numexpr\rowcountstart+\thecsvrow-\n\relax.} &
						\large\ifthenelse{\equal{\name}{}}{\givenname}{\givenname~\name} &
						\large\year~\class &
						\adjustbox{valign=c}{
							\begin{tikzpicture}[inner sep=0pt, outer sep=0pt]
								\draw[black, fill=white] (0,0) circle(2mm);
							\end{tikzpicture}
						}
					}
				\end{tabularx}
			\end{minipage}
			\hfill
		}
	\fi
}

\begin{document}
	\stimmzettel{Unterstufensprecher}{data/unterstufensprecher.csv}
	\newpage
	
	\stimmzettel{Mittelstufensprecher}{data/mittelstufensprecher.csv}
	\newpage
	
	\stimmzettel{Realschulsprecher}{data/realschulsprecher.csv}
	\newpage
	
	\stimmzettel{Schulsprecher}{data/schulsprecher.csv}
	\newpage
	
	\begin{center}
		\topskip0pt
		\vspace*{\fill}
		\textbf{\Large\href{https://git.dergrimm.net/dergrimm/smv-stimmzettel.git}{https://git.dergrimm.net/dergrimm/smv-stimmzettel.git}}
		\vspace*{\fill}
	\end{center}
\end{document}
