Scripting Ghost4J with Groovy

Gilles Grousset
Hack Like a Pirate
Published in
2 min readAug 13, 2013

Writing a simple command line tool in Java can be a bit boring…

Especially when an external library is needed: you always end up with your code (.java), some libraries (.jar) and probably a build script (ant, maven, gradle…).

That is a lot for a ‘simple command line’!

Hopefully, Groovy is here to help your script fit in one single file and then bringing Ghost4j features to the command line becomes fairly easy.

Here is a script to analyze a PDF file:

// A simple Groovy script using Ghost4J to perform PDF document analyzing (page count, font report, ink coverage) 
// The script expects a single argument: a PDF filename or URL @GrabResolver(name='ghost4j', root='http://repo.ghost4j.org/maven2/snapshots/') @Grab(group='org.ghost4j', module='ghost4j', version='0.5.1-SNAPSHOT') import org.ghost4j.analyzer.AnalysisItem
import org.ghost4j.analyzer.FontAnalyzer
import org.ghost4j.document.PDFDocument
import org.ghost4j.analyzer.InkAnalyzer
// Parse arguments
def filename
if (this.args.length < 1) {
println 'Error: no file name or URL provided'
System.exit(0)
} else { filename = args[0] }
// Open or download PDF
def file
if (filename.startsWith('http:')) {
file = new File('tmp.pdf')
file << new URL(filename).openStream()
} else { file = new File(filename) }
// Load document
def document = new PDFDocument()
document.load(file)
// Print page count
println "----------------- PAGE COUNT ----------------"
println document.getPageCount()
// Analyze fonts and print results
println "-------------------- FONTS ------------------"
def fontAnalyzer = new FontAnalyzer() fontAnalyzer.analyze(document).each {it -> println(it) }
// Analyze ink coverage and print results
println "--------------- INK COVERAGE ----------------"
def inkAnalyzer = new InkAnalyzer() inkAnalyzer.analyze(document).each {it -> println(it) }
// Delete temp.pdf
if (filename.startsWith('http:')) { file.delete() }

The script can be found as a Gist here.

The following command:

groovy pdfreport.groovy myfile.pdf

Will output something like this:

----------------- PAGE COUNT ---------------- 
2
-------------------- FONTS ------------------
Helvetica-Oblique: EMBEDDED SUB_SET ArialMT: EMBEDDED SUB_SET Helvetica: EMBEDDED SUB_SET Consolas: EMBEDDED SUB_SET TimesNewRomanPSMT: EMBEDDED SUB_SET GillSans-Bold: EMBEDDED SUB_SET Helvetica-Bold: EMBEDDED SUB_SET GillSans: EMBEDDED SUB_SET
--------------- INK COVERAGE ----------------
Page 1 C: 0.03081 M: 0.04687 Y: 0.04687 K: 0.06261
Page 2 C: 0.08096 M: 0.08096 Y: 0.07645 K: 0.08151

Originally published at https://blog.grousset.fr on August 13, 2013.

--

--