下面是我的代码-
ApplicationTagLib applicationTagLib = new ApplicationTagLib()
String html = applicationTagLib.include(controller: 'survey', action: 'pdf', params: [id: id])
html = html.replaceAll('&', '&')
html = html.replaceAll('&#', '&#')
html = html.replaceAll(' ', ' ')
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String baseUrl = "http://localhost:8080/"
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(content);
renderer.layout();
renderer.createPDF(baos);
byte[] data = baos.toByteArray();
response.setHeader("Content-disposition", "attachment;filename=report.pdf")
response.setContentType("application/pdf")
OutputStream out = response.getOutputStream();
out.write(data);
out.flush();
out.close()
在调查 Controller 的 pdf 操作中-
render(view: 'download')
下载.gsp
<g:img dir="assets/common" file="pointer.jpg"/>
<img src="assets/common/pin.png"/>
pdf 我们得到除了这些图像之外的所有数据
注意 - 当我直接点击调查 Controller 的 pdf 操作时,这是渲染图像。[localhost:8080/survey/pdf
]
最佳答案
生成 PDF 时,相对 path/source
不适用于静态资源。您必须为它们提供绝对的 path/source
。
您不应该对您的 baseUrl
进行硬编码。您可以注入(inject) grailsLinkGenerator
bean,然后 baseUrl
将在 grailsLinkGenerator.serverBaseURL
中可用。
所以你可以通过grailsLinkGenerator
获取controller中的baseUrl
,然后通过model
传递给你的view。然后你的 img
标签看起来像下面这样
<img src="${baseUrl}/assets/common/pin.png"/>
关于java - 无法在 pdf :grails 中渲染图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32687836/