Skip to main content
Known Participant
June 16, 2015
Question

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

  • June 16, 2015
  • 0 replies
  • 1185 views

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>

     </cffunction>

</cfcomponent>

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>

     </cffunction>

</cfcomponent>

This topic has been closed for replies.