Created
August 25, 2011 11:31
-
-
Save osima/1170468 to your computer and use it in GitHub Desktop.
a code that convert from fo to pdf in groovy
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='org.apache.xmlgraphics', module='fop', version='1.0') | |
import org.apache.fop.apps.* | |
import javax.xml.transform.* | |
import javax.xml.transform.sax.SAXResult | |
import javax.xml.transform.stream.StreamSource | |
def fo = new File(args[0]) | |
def pdf = new File(args[1]) | |
def fopFactory = FopFactory.newInstance() | |
def foUserAgent = fopFactory.newFOUserAgent() | |
def out = new BufferedOutputStream( new FileOutputStream(pdf) ) | |
def fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out) | |
def factory = TransformerFactory.newInstance() | |
def transformer = factory.newTransformer() // identity transformer | |
def src = new StreamSource(fo) | |
def res = new SAXResult(fop.getDefaultHandler()) | |
transformer.transform(src, res) | |
// Result processing | |
def foResults = fop.results | |
foResults.getPageSequences().each{ pageSequenceResults-> | |
def log = ''<<'' | |
log << 'PageSequence ' | |
log << (String.valueOf(pageSequenceResults.getID()).length() > 0 ? pageSequenceResults.getID() : "<no id>") | |
log << ' generated ' + pageSequenceResults.getPageCount() + ' pages.' | |
println log.toString() | |
} | |
println "Generated ${foResults.getPageCount()} pages in total." | |
out.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment