Skip to main content
Participant
December 5, 2022
Answered

cfdocument: open download prompt to ask user where to save pdf

  • December 5, 2022
  • 2 replies
  • 438 views

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.

 

I also tried using a header like this:

<cfdocument format="PDF" localUrl="yes" margintop="0.25" orientation="landscape">
  ...
  Your PDF content goes here...
</cfdocument>

<cfheader name="Content-Disposition" value="attachment; filename=your-file-name.pdf">
<cfcontent type="application/pdf" file="#expandPath('your-file-path.pdf')#">

But it also doesn't open a download prompt.

 

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

    This topic has been closed for replies.
    Correct answer BKBK

    I think you're almost there. Try something like

     

    <cfdocument format="PDF" localUrl="yes" margintop="0.25" orientation="landscape" filename="#expandPath('your-file-path.pdf')#">
      Your PDF content goes here...
    </cfdocument>
    
    <cfheader name="Content-Disposition" value="attachment; filename=your-file-name.pdf">
    <cfcontent type="application/pdf" file="#expandPath('your-file-path.pdf')#">
    

     

     

    2 replies

    BKBK
    Community Expert
    Community Expert
    December 14, 2022

    Did that solve the problem?

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    December 6, 2022

    I think you're almost there. Try something like

     

    <cfdocument format="PDF" localUrl="yes" margintop="0.25" orientation="landscape" filename="#expandPath('your-file-path.pdf')#">
      Your PDF content goes here...
    </cfdocument>
    
    <cfheader name="Content-Disposition" value="attachment; filename=your-file-name.pdf">
    <cfcontent type="application/pdf" file="#expandPath('your-file-path.pdf')#">
    

     

     

    Userx105Author
    Participant
    December 20, 2022

    Sorry for the late reply. 

    Yes it solved the issue, but I need to add that also a browser setting might overwrite the save/show decision, based on the pdf settings (at least in Chrome). 

    Thanks for your help!