Skip to content

Instantly share code, notes, and snippets.

@domix
Forked from timyates/arrowdoc.groovy
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domix/04e9e619ac98129e0a39 to your computer and use it in GitHub Desktop.
Save domix/04e9e619ac98129e0a39 to your computer and use it in GitHub Desktop.
@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