Skip to main content
Participant
Dec 5, 2022 2:34 PM UTC
Answered

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

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 is closed to new replies. Start a new post to keep the conversation going.
    BKBK
    BKBKCorrect answer
    Community Expert
    Dec 6, 2022 2:28 PM UTC

    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')#">
    

     

     

    3 replies

    BKBK
    Community Expert
    Community Expert
    Dec 14, 2022 2:11 PM UTC

    Did that solve the problem?

    Translate
    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    Dec 6, 2022 2:28 PM UTC

    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')#">
    

     

     

    Translate
    Userx105Author
    Participant
    Dec 20, 2022 3:35 PM UTC

    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!

    Translate