setting default filename for API response with chrome-pdf
-
Is it possible in a Handlebars/Chrome-PDF to set the default filename that would be offered when calling the template via API (I'd like to be able to modify the filename based on some of the data that's retrieved to build the report
-
You can dynamically change response content-disposition using beforeRender hook
https://jsreport.net/learn/api#content-disposition-and-report-name
https://jsreport.net/learn/scriptsfunction beforeRender(req, res) { req.options.reportName = 'aaa' }
-
it seems not to be worked as expected...
I added that to a script module, and attached the script module to the template (it's getting called because I have a console.log in there that writes the value of req.options.reportName to the console after I set it) but ... in postman I always get "report" as the default filename it tries to save, and in the response headers a Content-Disposition of "inline;report 02.pdf" (report 02 is the name of the template)
If I run (Download) locally from the editor then it does try and save it as "report 02.pdf"
Does it matter where the reportName is being set?
-
Ups, sorry. It should be like this
function beforeRender(req, res) { res.meta.reportName = 'aaa' }
-
I must be doing something dumb, as still no change - https://playground.jsreport.net/w/OffBeatMammal/ZpNfUQ3L
-
Hm. This looks like some kind of strange playground/jsreportonline bug.
The same works for me on my local jsreport.
You just click download in studio or sent request from postman and you get the modified content disposition and file name.The workaround for playground/jsreportonline is using
afterRender
hook
https://playground.jsreport.net/w/anon/ozORijeqfunction afterRender(req, res) { res.meta.reportName = 'aaa' }
-
works perfectly in the afterRender, thanks!