我正在尝试将GSP模板呈现为PDF并通过电子邮件发送,但我遇到了一个奇怪的问题。我使用Grails findBy*
方法获取数据,
def fax(Integer id) {
def transcriptInstance = Transcript.findById(id)
def userInstance = User.findById(transcriptInstance.submitter)
def m = [userInstance: userInstance, transcriptInstance: transcriptInstance]
ByteArrayOutputStream bytes = pdfRenderingService.render(template: "/transcript/_pdf", model: m)
mailService.sendMail {
to <my email> // for testing
from <my email> // for testing
subject "PDF Attachment";
attachBytes "transcript.pdf", "application/pdf", bytes.toByteArray()
body "PDF attachment"
}
}
但是用户实例返回User : (unsaved)
,我在行Cannot invoke method render() on null object
上收到错误ByteArrayOutputStream bytes = pdfRenderingService.render(template: "/transcript/_pdf", model: m)
。用户肯定保存在数据库中,这是我第一次遇到这个问题。知道出了什么问题吗?
编辑:
此代码现在生成一个pdf,但我无法通过mailService发送它。
def fax(Integer id) {
def transcriptInstance = Transcript.findById(id)
def userInstance = User.findById(transcriptInstance.submitter)
def m = [userInstance: userInstance, transcriptInstance: transcriptInstance]
ByteArrayOutputStream pdfBytes = pdfRenderingService.render(template: '/transcript/pdf', model: m)
new File('test.pdf').withOutputStream {
it.write pdfBytes.toByteArray()
}
mailService.sendMail {
multipart true
to <my email> // for testing
from <my email> // for testing
subject "PDF Attachment";
attachBytes "transcript.pdf", "application/pdf", <WHAT DO I PUT HERE?>
body "PDF attachment"
}
}
我在现场尝试了一些东西,但没有任何作用。有什么想法吗?
答案 0 :(得分:0)
获得ByteArrayOutputStream之后,您可以调用ByteArray()来获取mailService所需的字节。
def fax(Integer id) {
def transcriptInstance = Transcript.findById(id)
def userInstance = User.findById(transcriptInstance.submitter)
def m = [userInstance: userInstance, transcriptInstance: transcriptInstance]
ByteArrayOutputStream pdfBytes = pdfRenderingService.render(template: '/transcript/pdf', model: m)
mailService.sendMail {
multipart true
to <my email> // for testing
from <my email> // for testing
subject "PDF Attachment";
attachBytes "transcript.pdf", "application/pdf", pdfBytes.toByteArray()
body "PDF attachment"
}
}