My question is that how can we create image form byte array without response. currently I am using .
response.setHeader('Content-length', image.imageSize)
response.contentType = image.imageFormat // or the appropriate image content type
response.outputStream << image.imageData
response.outputStream.flush()
but it gave error because we done have an request object as I am running this by back end job
Don't know how you are creating your pdf files at the moment, but one way would be using the Grails Rendering Plugin for pdf generation. With this, you could use any view/template to generate a pdf file.
To send these pdfs from backend you'll need:
A View:
<html>
<head>...</head>
<body>
<h1>Your PDF Content</h2>
<g:each in="${images}" var="image">
<img src="${createLink(action:'displayImage', id:image.id)}" alt="${image.name}"/>
</g:each>
</body>
</html>
An action in your controller:
def displayImage = {
def image = Image.get(params.id)
response.setHeader("Content-disposition", "attachment; filename=${image.name}")
response.contentType = image?.mimeType
response.contentLength = image?.data.length
response.outputStream.write(attachment?.data)
}
A grails job that sends the multipart mail (using the mail plugin ) with the rendered pdf:
def execute() {
def pdfBytes = pdfRenderingService.render(template: '/path/to/your/template', model: [images: yourImages]).toByteArray()
sendMail {
multipart true
to "yourmail"
subject "yoursubject"
body (view: "/path/to/your/mailview", model: yourModel) attachBytes "yourTitle.pdf", CH.config.grails.mime.types['pdf'], pdfBytes
}
}
The code is not complete, it just demonstrate the basics.
Hope that helps!