Home > Net >  cfdocument: open download prompt to ask user where to save pdf
cfdocument: open download prompt to ask user where to save pdf

Time:12-08

I'm using cfdocument to create a pdf. Displaying the pdf in the browser works fine, but I would like to just open a download prompt when the user opens that url.

<cfdocument format="PDF" localUrl="yes" margintop="0.25" orientation="landscape">

What I tried: I followed this and added a filename: https://community.adobe.com/t5/coldfusion-discussions/how-to-use-cfdocument-create-a-pdf-file-and-save-the-file-in-server/td-p/966276

But it just saves that file in a lucee temp folder.

What I want is that it opens a prompt and asks the user, where he wants to save that file.

CodePudding user response:

I'm pretty sure you can't prompt the "save as" directly. The cause is that nowadays most browsers save the file immediately/directly to the "download" folder by default. For example in Chrome the user can deactivate this behaviour in "Settings -> Downloads -> Ask where to save each file before downloading". A similar setting exists in Firefox.

However, if you don't want to display the PDF in the browser but start a direct download, just add the http headers used for that. This is an example how to achieve it ( I've tested this on Lucee only but should work for ACF the same way):

<cfheader name="Content-Transfer-Encoding" value="binary">
<cfheader name="Content-Disposition" value="attachment; filename=""somefilename.pdf""">
<cfheader name="Content-Type" value="application/pdf">
<cfdocument format = "PDF" pagetype="A4" orientation="portrait">
    Just a test for download!
</cfdocument>

If you omit the attribute filename="somefilename.pdf" of the Content-Disposition response header, the name of the url will be taken. Alternatively you could ask the user to specify a name in a form input field and populate that variable. But it still will be downloaded to the "download" directory as specified in the users browser setting.

  • Related