简体   繁体   English

无法在 pdf 中渲染图像:grails

[英]unable to render image in pdf :grails

below is my code-下面是我的代码-

    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()

In pdf action of survey Contrller-在调查控制者的pdf行动中-

render(view: 'download')

download.gsp下载.gsp

<g:img dir="assets/common" file="pointer.jpg"/>
<img src="assets/common/pin.png"/>

pdf we are getting have all data except these image我们得到的 pdf 拥有除这些图像之外的所有数据

Note-while I am hitting direct pdf action of survey contrller this is renderring image fine.[ localhost:8080/survey/pdf ]注意 - 当我点击调查控制器的直接 pdf 操作时,这渲染图像很好。[ localhost:8080/survey/pdf ]

When generating PDF the relative path/source does not work for static resources.生成 PDF 时,相对path/source不适用于静态资源。 You have to provide the absolute path/source for them.您必须为它们提供绝对path/source

You should not hard code your baseUrl .您不应该对baseUrl硬编码。 You can inject the grailsLinkGenerator bean then the baseUrl will be available at grailsLinkGenerator.serverBaseURL .您可以注入grailsLinkGenerator豆那么baseUrl将可在grailsLinkGenerator.serverBaseURL

So you can get the baseUrl in controller through grailsLinkGenerator and then pass it through model to your view.所以,你可以得到baseUrl通过控制器grailsLinkGenerator ,然后通过它传递model到您的视图。 Then your img tag can look like following然后你的img标签看起来像下面这样

    <img src="${baseUrl}/assets/common/pin.png"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM