2

I have function that will generate cfdocument pdf file. Then I would like to return the document and server to the browser. Here is example of the function that generates the pdf file.

<cftry>
    <cfdocument format="PDF" filename="file.pdf" overwrite="Yes">
        <table>
           <tr>
              <td>Test</td>
           </tr>
        </table>
    </cfdocument>

    <cfset local.fnResults = {file: //Here I'm not sure what I should return}>         

    <cfcatch type="any">
        <cfset local.fnResults = {status : 400, message : "Error! Something went wrong."}>
    </cfcatch>
</cftry>

<cfreturn fnResults>

Function above should genrate the file and return PDF in fnResults structure. Then on serve.cfm I have this logic:

<cfset local.Results = genertePDF()>

<cfif structKeyExists(local.Results, "FILEPATH")>
        <cfheader name="content-disposition" value="attachment;filename=#local.Results["FILENAME"]#"/>

        <!---Add the file content to the output stream:--->
        <cfcontent file="#local.Results["FILEPATH"]#" type="application/octet-stream" reset="true"/>

        <!---Exit immediately after adding the file content to avoid corrupting it:--->
        <cfabort/>
    <cfif>

You can ignore the structure of RESULTS since I haven't modified everything. My only problem here is to figure out how to return cfdocument content? If anyone knows how to get this to work please let me know. Thank you.

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193
  • Are you saving all of these files or are they just transient? How many files do you expect to generate? – Shawn Dec 24 '18 at 16:41
  • @Shawn This feature generates PDF file once user clicks on the button. This page is front end content that should be converted to PDF. I have tried using `jspdf` but a lot of things are not working or hard to adjust. This approach seems like a good option so far. I can have multiple users on the same page at the same time using this feature. I'm not sure if this approach is good way to handle generating PDF documents? – espresso_coffee Dec 24 '18 at 16:54
  • Just take out the filename and it will stream the content straight back to the browser. – Shawn Dec 24 '18 at 16:58
  • @Shawn If I take out file name file didn't show up. – espresso_coffee Dec 24 '18 at 16:59
  • It should. Unless maybe it's being used differently than we're thinking? Could you please post a small example showing how the code is invoked? – SOS Dec 29 '18 at 01:58
  • If you read my question you will find the example on how I call the function. – espresso_coffee Dec 29 '18 at 02:06

1 Answers1

1

Change:

<cfheader name="content-disposition" value="attachment;filename=#local.Results["FILENAME"]#"/>

To:

<cfheader name="content-disposition" value="inline;filename=#local.Results["FILENAME"]#"/>

Reference

PeterKA
  • 24,158
  • 5
  • 26
  • 48