-
-
Save patbaumgartner/1fcd738638e1be6b9cf5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group='com.itextpdf', module='itextpdf', version='5.5.4') | |
@Grab(group='org.bouncycastle', module='bcpkix-jdk15on', version='1.49') | |
import com.itextpdf.text.Element | |
import com.itextpdf.text.Phrase | |
import com.itextpdf.text.Font | |
import com.itextpdf.text.Image | |
import com.itextpdf.text.pdf.ColumnText | |
import com.itextpdf.text.pdf.PdfContentByte | |
import com.itextpdf.text.pdf.PdfReader | |
import com.itextpdf.text.pdf.PdfStamper | |
import com.itextpdf.text.pdf.BaseFont | |
import org.apache.commons.cli.Option | |
def cli = new CliBuilder(usage: 'groovy stamp_pdf.groovy -s <signature image> -c <certificate_tempalte.pdf> -d <csvfile> <name>,<name1>,...') | |
cli.with { | |
s 'specify file for your written signature (png, jpg,...)', args:1 | |
d 'data file (one name per line)', args:1 | |
c 'certificat pdf document', args:1, required: true | |
h 'show help' | |
} | |
def options = cli.parse(args) | |
def extra = options.arguments() | |
/*println "h $options.h" | |
println "c $options.c" | |
println "ns $options.ns" | |
println "ex ${extra.size()}" | |
if (options.h || ( (!options.c) && (!options.ns) ) || (extra.size()!=1)) { | |
cli.usage() | |
System.exit(0) | |
}*/ | |
def certificateFile = options.c | |
assert certificateFile.endsWith(".pdf") | |
def signatureFile = options.s ?: "signature.png" | |
// build list of attendee names: -n args plus csv file contents | |
def names = [] | |
names.addAll(extra) | |
if (options.d) { | |
names += new File(options.d).readLines() | |
} | |
// produce certificate for each name | |
names.each { | |
def targetFileName = certificateFile[0..-5] + "_" + it.replaceAll(" ", "_") + ".pdf" | |
PdfReader reader = new PdfReader(certificateFile) | |
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(targetFileName)) | |
PdfContentByte canvas = stamper.getOverContent(1) | |
Font font = new Font(BaseFont.createFont(), 29) | |
font.setColor(64, 64, 65) | |
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase( it, font), 330, 226.5, 0); | |
Image image = Image.getInstance(signatureFile) | |
image.scaleToFit(200,60) | |
image.setAbsolutePosition(533,66) | |
canvas.addImage(image) | |
stamper.close(); | |
reader.close(); | |
println "Done generation $targetFileName" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment