-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiller.groovy
134 lines (110 loc) · 3.65 KB
/
filler.groovy
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
@Grab('org.slf4j:slf4j-api:1.7.13')
@Grab('com.itextpdf:kernel:7.0.0')
@Grab('com.itextpdf:io:7.0.0')
@Grab('com.itextpdf:layout:7.0.0')
import java.text.SimpleDateFormat
import com.itextpdf.io.font.FontConstants
import com.itextpdf.kernel.font.PdfFont
import com.itextpdf.kernel.font.PdfFontFactory
import com.itextpdf.kernel.pdf.PdfDocument
import com.itextpdf.kernel.pdf.PdfReader
import com.itextpdf.kernel.pdf.PdfWriter
import com.itextpdf.kernel.pdf.canvas.PdfCanvas
class Adresse
{
String strasse
String hausnummer
String plz
String stadt
void setZipCode(String plz)
{
assert plz ==~ /\d{5}/
this.plz = plz
}
Adresse(Properties p)
{
strasse = p.strasse
hausnummer = p.hausnummer
stadt = p.stadt
setZipCode p.plz
}
@Override
String toString(){ return "$strasse $hausnummer $plz $stadt"}
}
class Filler
{
final String FORM_PATH = 'form.pdf'
String studiengang
String seminargruppe
String nachname
String vorname
Adresse adresse
String semester
Filler(Properties p)
{
studiengang = p.studiengang
seminargruppe = p.seminargruppe
vorname = p.vorname
nachname = p.nachname
semester = p.semester
adresse = new Adresse(p)
}
void init()
{
File form = new File(FORM_PATH)
if (form.exists())
return
def fos = new FileOutputStream(form)
def bos = new BufferedOutputStream(fos)
def url = new URL('http://www.ba-dresden.de/de/service/dokumente/dokumente-fuer-alle-studierenden.html?eID=dam_frontend_push&docID=5047')
bos << url.openStream()
bos.close()
}
void fill(String dest, String modulID, String pruefungsteile, String bestanden) throws FileNotFoundException, IOException
{
init()
PdfDocument pdfDocument = new PdfDocument(new PdfReader(FORM_PATH), new PdfWriter(dest))
PdfFont font = PdfFontFactory.createFont FontConstants.TIMES_ROMAN
PdfCanvas pdfCanvas = new PdfCanvas(pdfDocument.getFirstPage())
pdfCanvas.setFontAndSize(font, 12)
def entries = []
entries << [140, 665, studiengang]
entries << [475, 665, seminargruppe]
entries << [110, 642, nachname]
entries << [350, 642, vorname]
entries << [220, 619, adresse.toString()]
entries << [ 85, 395, semester]
entries << [130, 395, modulID]
entries << [350, 395, pruefungsteile]
entries << [450, 395, bestanden]
entries << [110, 349, (new SimpleDateFormat("dd.MM.yyyy")).format(Calendar.getInstance().getTime()).toString()]
entries.each { x, y, text ->
pdfCanvas.setTextMatrix(x, y)
pdfCanvas.showText(text)
}
pdfCanvas.endText()
pdfCanvas.fill()
pdfCanvas.release()
pdfDocument.close()
}
}
Properties getPropertiesFromPath(String path)
{
Properties properties = new Properties()
File propertiesFile = new File(path)
propertiesFile.withReader'UTF8', { properties.load(it) }
return properties
}
static void main(String[] args)
{
def modulID = System.getProperty('modulID') ?: ''
def pruefungsteile = System.getProperty('pruefungsteile') ?: ''
def bestanden = System.getProperty('bestanden') ?: ''
if (args =~ /-?-?h(elp)?/)
{
println '''usage:
<script_name> [-DmodulID="12345"] [-Dpruefungsteile="1"] [-Dbestanden="ja"]'''
}
Filler filler = new Filler(getPropertiesFromPath('form.properties'))
filler.fill 'output.pdf', modulID, pruefungsteile, bestanden
}