-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf-convert.groovy
39 lines (29 loc) · 901 Bytes
/
pdf-convert.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
@Grab(group = 'com.itextpdf', module = 'itextpdf', version = '5.5.13')
import com.itextpdf.text.*
import com.itextpdf.text.pdf.*
class PdfConvert {
String title
Document document
void createFile(def title) {
this.title = title
this.document = new Document()
PdfWriter.getInstance(document, new FileOutputStream("${title}.pdf"))
document.open()
}
void addFile(File file) {
println file
Image img = Image.getInstance(file.toURL())
float scaler = ((PageSize.A4.width - document.leftMargin()
- document.rightMargin()) / img.getWidth()) * 100
img.scalePercent(scaler)
document.add(img)
}
void addFile(String newFileName, File file) {
this.addFile(file)
}
void close() {
document.close()
println("Wrote ${title}.pdf")
}
}
return new PdfConvert()