I’m using the WebDev module in 8.1 to get a PDF from a database and send it to both a Perspective client, and possibly just a regular browser.
When sending the data back from the WebDev module I’m doing the following:
return {'contentType':'application/pdf','bytes':reportPDF}
Which works splendidly for returning the PDF. However when I access this doGet from a web browser it end up being returned as the name of the resource, in this case GetTicket, rather than something I can specify.
Is there something I can return in the dict that will provide the client web browser with a suggested name for this .pdf file?
The “modern” way is to specify a download
attribute (which system.perspective.download
does automatically when you specify a filename): https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-download
However, at the server level you could try adding a well-formed Content-Disposition
header:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
So from the webdev side how do I add the Content-Disposition header? I tried this:
return {'contentType':'application/pdf','contentDisposition': 'attachment; filename="{}"'.format(ticketNum),'bytes':reportPDF}
And no dice. ticketNum is the identifier of each PDF in the database.
This would be just coming from the WebDev side as we may be distributing links in e-mails etc…
Try this:
request["servletResponse"].addHeader('contentDisposition', 'attachment; filename="{}"'.format(ticketNum))
return {'contentType':'application/pdf','bytes':reportPDF}
You got me down the right path.
request["servletResponse"].addHeader('Content-Disposition', 'attachment; filename="{}.pdf"'.format(ticketNum))
Time to read up on the HttpServletResponse object in case this customer wants anything else. Sigh.
Thanks for the help!
2 Likes
c-w-t
September 21, 2021, 6:49pm
6
Awesome stuff, saved me some time friends!!