Grails 渲染插件不渲染图像
Grails render-plugin does not render images
我现在已经设置了渲染插件,我得到了很好的 PDF 文件,但我还需要将图像放入我的 table 中的几列中。这些图像由实例的数据字段有条件地选择。
我在 assets/images 文件夹中有图像,我认为是正确的位置。
我在模板中使用以下 GSP 行,渲染器将使用该行来创建 PDF。
<td><g:if test="${od?.priceFSC > 0.1}"><asset:image src="checkOut16x16.png" width="16" height="16"/></g:if></td>
作为 HTML 查看图像打印完美,但在渲染 PDF 时它们丢失了。
我查看了文档并尝试了示例中的代码:
控制器:
def createPDF() {
def file = new File("asets/CheckOut16x16.png")
def OfferHeader offerHeader = OfferHeader.get(params.id)
[offerHeader: offerHeader])
renderPdf(template: "/stocknote/Stocknote", model: [offerHeader: offerHeader,imageBytes: file.bytes], filename: "Stocknote-"+params.id+".pdf")
}
观点:
<rendering:inlinePng bytes="${imageBytes}" class="some-class" />
我不关心这里的情况,我只是想看看它是否会被打印出来,但这不是因为视图崩溃:
URI
/stocknote/editStocknote/32
Class
org.grails.taglib.GrailsTagException
Message
Request processing failed; nested exception is org.grails.gsp.GroovyPagesException: Error processing GroovyPageView: [views/stocknote/editStocknote.gsp:32] [views/stocknote/_StocknoteDetail.gsp:3] 'bytes' is required
Caused by
[views/stocknote/editStocknote.gsp:32] [views/stocknote/_StocknoteDetail.gsp:3] 'bytes' is required
我不知道我做错了什么,但错误消息似乎令人困惑,"Bytes is required"但我有 bytes="${imageBytes}"。
希望有人能给我一些帮助或解释。
听起来你的文件路径不正确,试试:
控制器:
def assetResourceLocator
def createPDF() {
def file = assetResourceLocator.findAssetForURI( 'CheckOut16x16.png' )
def OfferHeader offerHeader = OfferHeader.get(params.id)
[offerHeader: offerHeader])
renderPdf(template: "/stocknote/Stocknote", model: [offerHeader: offerHeader,imageBytes: file.getByteArray()], filename: "Stocknote-"+params.id+".pdf")
}
视图应该没问题。
犯了一个大错误>_<,用于创建 PDF 的模板也被您订购 PDF 的视图使用,而此时控制器尚未创建图像。
所以为了通过,我必须在渲染之前检查图像的值。
<g:if test="${imageBytes!= null}"> <rendering:inlinePng bytes="${imageBytes}" /></g:if>
这就是所需要的。
我现在已经设置了渲染插件,我得到了很好的 PDF 文件,但我还需要将图像放入我的 table 中的几列中。这些图像由实例的数据字段有条件地选择。
我在 assets/images 文件夹中有图像,我认为是正确的位置。
我在模板中使用以下 GSP 行,渲染器将使用该行来创建 PDF。
<td><g:if test="${od?.priceFSC > 0.1}"><asset:image src="checkOut16x16.png" width="16" height="16"/></g:if></td>
作为 HTML 查看图像打印完美,但在渲染 PDF 时它们丢失了。
我查看了文档并尝试了示例中的代码:
控制器:
def createPDF() {
def file = new File("asets/CheckOut16x16.png")
def OfferHeader offerHeader = OfferHeader.get(params.id)
[offerHeader: offerHeader])
renderPdf(template: "/stocknote/Stocknote", model: [offerHeader: offerHeader,imageBytes: file.bytes], filename: "Stocknote-"+params.id+".pdf")
}
观点:
<rendering:inlinePng bytes="${imageBytes}" class="some-class" />
我不关心这里的情况,我只是想看看它是否会被打印出来,但这不是因为视图崩溃:
URI
/stocknote/editStocknote/32
Class
org.grails.taglib.GrailsTagException
Message
Request processing failed; nested exception is org.grails.gsp.GroovyPagesException: Error processing GroovyPageView: [views/stocknote/editStocknote.gsp:32] [views/stocknote/_StocknoteDetail.gsp:3] 'bytes' is required
Caused by
[views/stocknote/editStocknote.gsp:32] [views/stocknote/_StocknoteDetail.gsp:3] 'bytes' is required
我不知道我做错了什么,但错误消息似乎令人困惑,"Bytes is required"但我有 bytes="${imageBytes}"。 希望有人能给我一些帮助或解释。
听起来你的文件路径不正确,试试:
控制器:
def assetResourceLocator
def createPDF() {
def file = assetResourceLocator.findAssetForURI( 'CheckOut16x16.png' )
def OfferHeader offerHeader = OfferHeader.get(params.id)
[offerHeader: offerHeader])
renderPdf(template: "/stocknote/Stocknote", model: [offerHeader: offerHeader,imageBytes: file.getByteArray()], filename: "Stocknote-"+params.id+".pdf")
}
视图应该没问题。
犯了一个大错误>_<,用于创建 PDF 的模板也被您订购 PDF 的视图使用,而此时控制器尚未创建图像。 所以为了通过,我必须在渲染之前检查图像的值。
<g:if test="${imageBytes!= null}"> <rendering:inlinePng bytes="${imageBytes}" /></g:if>
这就是所需要的。