0 Replies Latest reply on Jun 16, 2015 7:29 AM by Dave Miller

    How can I return a PDF file from server using restful api

    Dave Miller Level 1

      Wondering how to return a PDF file from the server using the restful api.


      A document id is provided, which retrieves a record that has the file path and file name information.

      From there the file is retrieved into a variable.


      I have tried returning as Binary Base64 with a returntype = binary in the api


      <cfcomponent restpath="document" rest="true" output="false" extends="cfc.document">

           <cffunction name="getDocument" access="remote" output="false" httpmethod="get" returntype="binary" restpath="view/{documentId: \d+}" >

                <cfargument name="documentId" required="true" type="numeric" restargsource="path">


                <cfset REQUEST.sessionObj = REQUEST.securityObj.checkSession(REQUEST.sessionKey)>

                <cfset LOCAL.documentData = getDocumentData(REQUEST.sessionObj.userId, arguments.documentId, REQUEST.sessionObj.locale, REQUEST.sessionObj.languageId)>


                <cfreturn documentData>




      Next file cfc.document

      <cfcomponent name="document" output="false" extends="cfc.metadata.document">

           <cffunction name="getDocumentData" output="no" returntype="any">

                <cfargument name="userId" type="numeric" required="yes">

                <cfargument name="documentId" type="numeric" required="yes">

                <cfargument name="locale" type="string" required="yes">

                <cfargument name="languageId" type="numeric" required="yes">


                <cfset pdfFileContent = fileRead("C:\PDFFiles\PDFSample.pdf")>

                <cfset documentData = ToBinary(ToBase64(pdfFileContent))>


                <cfreturn documentData>