Skip to content

Instantly share code, notes, and snippets.

@timyates
Created April 11, 2015 09:10
Show Gist options
  • Save timyates/99725e049fa9413f7e09 to your computer and use it in GitHub Desktop.
Save timyates/99725e049fa9413f7e09 to your computer and use it in GitHub Desktop.
ascii-image and document-builder sitting in a tree
@Grab(group='com.craigburke.document', module='pdf', version='0.3.1')
@Grab(group='com.craigburke.document', module='word', version='0.3.1')
@Grab('com.bloidonia:ascii-image:1.1')
import com.bloidonia.asciiimage.AsciImageProcessor
import java.awt.image.BufferedImage
import java.awt.RenderingHints
import java.awt.Color
import javax.imageio.ImageIO
import com.craigburke.document.builder.PdfDocumentBuilder
import com.craigburke.document.builder.WordDocumentBuilder
byte[] imageData = new ByteArrayOutputStream().with { baos ->
AsciImageProcessor.fromLines([
'. . . . . . .',
'. . 3 . . . .',
'. 1 2 . . . .',
'. . . . . 4 .',
'. 7 6 . . . .',
'. . 5 . . . .',
'. . . . . . .'
]).asBufferedImage { ctx ->
ctx.type(BufferedImage.TYPE_INT_ARGB)
.scale(10)
.graphics { g ->
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
g.paint = Color.BLACK
}
}.render().with { img ->
try {
ImageIO.write(img, 'png', baos)
baos.flush()
baos.toByteArray()
}
finally { baos.close() }
}
}
[new WordDocumentBuilder(new File('/tmp/example.docx')),
new PdfDocumentBuilder(new File('/tmp/example.pdf'))].each { builder ->
builder.create {
document {
paragraph {
image(data: imageData, name: 'groovy.png')
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment