【问题标题】:Rename page/tab title of the page that renders PDF in Grails重命名在 Grails 中呈现 PDF 的页面的页面/选项卡标题
【发布时间】:2018-03-16 10:46:48
【问题描述】:

我正在使用 Grails。我需要在浏览器中呈现 PDF。这是工作代码:

    response.setContentType("application/pdf")
    response.setHeader("Content-disposition", "inline;filename='${fileName}.pdf'")
    response.outputStream << pdfFile.newInputStream()

PDF 正确呈现,但选项卡/页面标题是方法名称。例如。对于 PDF 渲染方法,使用 pdfController/downloadPdf。然后标签/页面标题是“downloadPdf”。我希望它是“${fileName}.pdf”。有可能吗?

提前致谢

【问题讨论】:

    标签: pdf grails


    【解决方案1】:

    您必须将内容处置设置为附件。我的示例还强制在浏览器中下载,而不是在选项卡中打开它。

    Document documentInstance = Document.get(id) //get your document from somewhere
    response.setContentType("APPLICATION/OCTET-STREAM")
    response.setHeader("Content-Disposition", "Attachment;Filename=\"${documentInstance.filename}\"") 
    def outputStream = response.getOutputStream() 
    outputStream << documentInstance.filedata outputStream.flush() 
    outputStream.close() }
    

    【讨论】:

    • 如果我现在下载这个文件,它的名字是正确的,例如'${fileName}.pdf',所以在这里我没有问题。不幸的是,强制下载对我来说不是一种选择。
    • 我认为重要的一行是response.setHeader("Content-Disposition", "Attachment;Filename=\"${documentInstance.filename}\"")
    • 但是附件而不是内联参数会强制下载,而我需要首先在浏览器中呈现pdf。
    猜你喜欢
    • 2012-02-29
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多