grails - 在控制器中访问图像以在pdf

时间:2016-09-28 08:33:45

标签: image pdf grails

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

def downloadBooking() {
    def result = Booking.findById(params.id)
    renderPdf(template: "/pdf/booking/vipConfirmation", model : result)
}

以上工作正常,我只是不知道如何将图像添加到其中。另请告诉我如何访问gsp中的图像。

2 个答案:

答案 0 :(得分:2)

您需要将图像的字节码发送到&#34; pdfRenderingService&#34;。这是下面的配置。

//convert image to bytecode and pass it to the padf rendring service.  
def imageBytes=grailsResourceLocator.findResourceForURI('/images/user.png').file.bytes

将值传递给pdfrendering服务。

 ByteArrayOutputStream reportBytes = pdfRenderingService.render(template: "/pdf/booking/vipConfirmation", model: [result: result, "imageBytes": imageBytes])

访问模板中的图像

<div> <rendering:inlinePng bytes="${imageBytes}" class="header-img"/> </div>

答案 1 :(得分:0)

Grails 3.3.9

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