简体   繁体   中英

Rename page/tab title of the page that renders PDF in Grails

I'm using Grails. I need to render a PDF in browser. Here is the working code:

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

PDF is rendered correctly, but the tab/page title is method name. Eg for PDF rendering method pdfController/downloadPdf is used. Then the tab/page title is 'downloadPdf'. I'd like it to be the '${fileName}.pdf'. Is it possible?

Thanks in advance

You have to set content disposition as Attachment. My example also forces download in browser instead of opening it in tab.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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