@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') } } } }