首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >grails -在pdf中使用控制器中的图像

grails -在pdf中使用控制器中的图像
EN

Stack Overflow用户
提问于 2016-09-28 08:33:44
回答 2查看 747关注 0票数 0

我正在创建一个动态的pdf与普惠制,我想添加一个图像到它,但我不知道如何做这个,因为简单地使用<img src="${resource(dir: 'images/vip', file: 'heading.png')}"/>在普惠制似乎是行不通的。在创建电子邮件时,您可以在inline 'pic1', 'image/jpg', resourceLoader.getResource("/images/bawHeader3.png").getFile()中使用.sendMail,所以我想知道是否可以对pdf进行类似的操作。到目前为止,我得到的是:

代码语言:javascript
复制
def downloadBooking() {
    def result = Booking.findById(params.id)
    renderPdf(template: "/pdf/booking/vipConfirmation", model : result)
}

上面的工作很好,我只是不知道如何添加一个图像到它。另外,请告诉我如何访问gsp内部的图像请。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-28 09:28:21

您需要将图像的字节码发送到"pdfRenderingService“。下面是配置。

代码语言:javascript
复制
//convert image to bytecode and pass it to the padf rendring service.  
def imageBytes=grailsResourceLocator.findResourceForURI('/images/user.png').file.bytes

将值传递给pdfrendering服务。

代码语言:javascript
复制
 ByteArrayOutputStream reportBytes = pdfRenderingService.render(template: "/pdf/booking/vipConfirmation", model: [result: result, "imageBytes": imageBytes])

在模板中访问图像

代码语言:javascript
复制
<div> <rendering:inlinePng bytes="${imageBytes}" class="header-img"/> </div>
票数 2
EN

Stack Overflow用户

发布于 2019-03-12 03:43:21

Grails 3.3.9

代码语言:javascript
复制
import org.springframework.core.io.Resource

def assetResourceLocator
Resource resource = assetResourceLocator.findAssetForURI('logo.png')
ByteArrayOutputStream bytes = pdfRenderingService.render(template: "/pdfs/invoice", model: [imageBytes: resource.getInputStream().bytes]) as ByteArrayOutputStream
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39742076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档